#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
namespace HeuristicLab.Optimization.Views {
[View("RunCollection BubbleChart")]
[Content(typeof(RunCollection), false)]
public partial class RunCollectionBubbleChartView : AsynchronousContentView {
private enum SizeDimension { Constant = 0 }
private enum AxisDimension { Index = 0 }
private string xAxisValue;
private string yAxisValue;
private string sizeAxisValue;
private readonly Dictionary> runToDataPointMapping = new Dictionary>();
private readonly Dictionary runToIndexMapping = new Dictionary();
private readonly Dictionary> categoricalMapping = new Dictionary>();
private readonly Dictionary xJitter = new Dictionary();
private readonly Dictionary yJitter = new Dictionary();
private readonly HashSet selectedRuns = new HashSet();
private readonly Random random = new Random();
private double xJitterFactor = 0.0;
private double yJitterFactor = 0.0;
private bool isSelecting = false;
private bool suppressUpdates = false;
public RunCollectionBubbleChartView() {
InitializeComponent();
chart.ContextMenuStrip.Items.Insert(0, hideRunToolStripMenuItem);
chart.ContextMenuStrip.Items.Insert(1, openBoxPlotViewToolStripMenuItem);
chart.ContextMenuStrip.Items.Add(getDataAsMatrixToolStripMenuItem);
chart.ContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(ContextMenuStrip_Opening);
colorDialog.Color = Color.Black;
colorDialogButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
isSelecting = false;
chart.CustomizeAllChartAreas();
chart.ChartAreas[0].CursorX.Interval = 1;
chart.ChartAreas[0].CursorY.Interval = 1;
chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !this.isSelecting;
chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !this.isSelecting;
}
public new RunCollection Content {
get { return (RunCollection)base.Content; }
set { base.Content = value; }
}
public IStringConvertibleMatrix Matrix {
get { return this.Content; }
}
public IEnumerable SelectedRuns {
get { return selectedRuns; }
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.Reset += new EventHandler(Content_Reset);
Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsAdded);
Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsRemoved);
Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_CollectionReset);
Content.OptimizerNameChanged += new EventHandler(Content_AlgorithmNameChanged);
Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
RegisterRunEvents(Content);
}
protected override void DeregisterContentEvents() {
base.DeregisterContentEvents();
Content.Reset -= new EventHandler(Content_Reset);
Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsAdded);
Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsRemoved);
Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_CollectionReset);
Content.OptimizerNameChanged -= new EventHandler(Content_AlgorithmNameChanged);
Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
DeregisterRunEvents(Content);
}
protected virtual void RegisterRunEvents(IEnumerable runs) {
foreach (IRun run in runs)
run.Changed += new EventHandler(run_Changed);
}
protected virtual void DeregisterRunEvents(IEnumerable runs) {
foreach (IRun run in runs)
run.Changed -= new EventHandler(run_Changed);
}
private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) {
DeregisterRunEvents(e.OldItems);
RegisterRunEvents(e.Items);
}
private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) {
DeregisterRunEvents(e.Items);
}
private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) {
RegisterRunEvents(e.Items);
}
private void run_Changed(object sender, EventArgs e) {
if (suppressUpdates) return;
if (InvokeRequired)
this.Invoke(new EventHandler(run_Changed), sender, e);
else {
IRun run = (IRun)sender;
UpdateRun(run);
UpdateCursorInterval();
chart.ChartAreas[0].RecalculateAxesScale();
UpdateAxisLabels();
}
}
private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
if (InvokeRequired)
this.Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
else {
suppressUpdates = Content.UpdateOfRunsInProgress;
if (suppressUpdates) return;
foreach (var run in Content) UpdateRun(run);
UpdateMarkerSizes();
UpdateCursorInterval();
chart.ChartAreas[0].RecalculateAxesScale();
UpdateAxisLabels();
}
}
private void UpdateRun(IRun run) {
if (runToDataPointMapping.ContainsKey(run)) {
foreach (DataPoint point in runToDataPointMapping[run]) {
if (!run.Visible) {
this.chart.Series[0].Points.Remove(point);
continue;
}
if (selectedRuns.Contains(run)) {
point.Color = Color.Red;
point.MarkerStyle = MarkerStyle.Cross;
} else {
point.Color = Color.FromArgb(255 - transparencyTrackBar.Value, ((IRun)point.Tag).Color);
point.MarkerStyle = MarkerStyle.Circle;
}
}
if (!run.Visible) runToDataPointMapping.Remove(run);
} else {
AddDataPoint(run);
}
if (this.chart.Series[0].Points.Count == 0)
noRunsLabel.Visible = true;
else
noRunsLabel.Visible = false;
}
protected override void OnContentChanged() {
base.OnContentChanged();
this.categoricalMapping.Clear();
UpdateComboBoxes();
UpdateDataPoints();
UpdateCaption();
RebuildInverseIndex();
}
private void RebuildInverseIndex() {
if (Content != null) {
runToIndexMapping.Clear();
int i = 0;
foreach (var run in Content) {
runToIndexMapping.Add(run, i);
i++;
}
}
}
private void Content_ColumnNamesChanged(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
else
UpdateComboBoxes();
}
private void UpdateCaption() {
Caption = Content != null ? Content.OptimizerName + " Bubble Chart" : ViewAttribute.GetViewName(GetType());
}
private void UpdateComboBoxes() {
string selectedXAxis = (string)this.xAxisComboBox.SelectedItem;
string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
string selectedSizeAxis = (string)this.sizeComboBox.SelectedItem;
this.xAxisComboBox.Items.Clear();
this.yAxisComboBox.Items.Clear();
this.sizeComboBox.Items.Clear();
if (Content != null) {
string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
this.xAxisComboBox.Items.AddRange(additionalAxisDimension);
this.xAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
string[] additionalSizeDimension = Enum.GetNames(typeof(SizeDimension));
this.sizeComboBox.Items.AddRange(additionalSizeDimension);
this.sizeComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
this.sizeComboBox.SelectedItem = SizeDimension.Constant.ToString();
bool changed = false;
if (selectedXAxis != null && xAxisComboBox.Items.Contains(selectedXAxis)) {
xAxisComboBox.SelectedItem = selectedXAxis;
changed = true;
}
if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
yAxisComboBox.SelectedItem = selectedYAxis;
changed = true;
}
if (selectedSizeAxis != null && sizeComboBox.Items.Contains(selectedSizeAxis)) {
sizeComboBox.SelectedItem = selectedSizeAxis;
changed = true;
}
if (changed) {
UpdateDataPoints();
UpdateAxisLabels();
}
}
}
private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
else UpdateCaption();
}
private void Content_Reset(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_Reset), sender, e);
else {
this.categoricalMapping.Clear();
RebuildInverseIndex();
UpdateDataPoints();
UpdateAxisLabels();
}
}
private void UpdateDataPoints() {
Series series = this.chart.Series[0];
series.Points.Clear();
runToDataPointMapping.Clear();
selectedRuns.Clear();
chart.ChartAreas[0].AxisX.IsMarginVisible = xAxisValue != AxisDimension.Index.ToString();
chart.ChartAreas[0].AxisY.IsMarginVisible = yAxisValue != AxisDimension.Index.ToString();
if (Content != null) {
foreach (IRun run in this.Content)
this.AddDataPoint(run);
if (this.chart.Series[0].Points.Count == 0)
noRunsLabel.Visible = true;
else {
noRunsLabel.Visible = false;
UpdateMarkerSizes();
UpdateCursorInterval();
}
}
xTrackBar.Value = 0;
yTrackBar.Value = 0;
//needed to set axis back to automatic and refresh them, otherwise their values may remain NaN
var xAxis = chart.ChartAreas[0].AxisX;
var yAxis = chart.ChartAreas[0].AxisY;
SetAutomaticUpdateOfAxis(xAxis, true);
SetAutomaticUpdateOfAxis(yAxis, true);
chart.Refresh();
}
private void UpdateMarkerSizes() {
var series = chart.Series[0];
if (series.Points.Count <= 0) return;
var sizeValues = series.Points.Select(p => p.YValues[1]);
double minSizeValue = sizeValues.Min();
double maxSizeValue = sizeValues.Max();
double sizeRange = maxSizeValue - minSizeValue;
const int smallestBubbleSize = 5;
foreach (DataPoint point in series.Points) {
//calculates the relative size of the data point 0 <= relativeSize <= 1
double relativeSize = (point.YValues[1] - minSizeValue);
if (sizeRange > double.Epsilon) {
relativeSize /= sizeRange;
//invert bubble sizes if the value of the trackbar is negative
if (sizeTrackBar.Value < 0) relativeSize = Math.Abs(relativeSize - 1);
} else relativeSize = 1;
double sizeChange = Math.Abs(sizeTrackBar.Value) * relativeSize;
point.MarkerSize = (int)Math.Round(sizeChange + smallestBubbleSize);
}
}
private void UpdateDataPointJitter() {
var xAxis = this.chart.ChartAreas[0].AxisX;
var yAxis = this.chart.ChartAreas[0].AxisY;
SetAutomaticUpdateOfAxis(xAxis, false);
SetAutomaticUpdateOfAxis(yAxis, false);
double xAxisRange = xAxis.Maximum - xAxis.Minimum;
double yAxisRange = yAxis.Maximum - yAxis.Minimum;
foreach (DataPoint point in chart.Series[0].Points) {
IRun run = (IRun)point.Tag;
double xValue = GetValue(run, xAxisValue).Value;
double yValue = GetValue(run, yAxisValue).Value;
if (!xJitterFactor.IsAlmost(0.0))
xValue += 0.1 * GetXJitter(run) * xJitterFactor * (xAxisRange);
if (!yJitterFactor.IsAlmost(0.0))
yValue += 0.1 * GetYJitter(run) * yJitterFactor * (yAxisRange);
point.XValue = xValue;
point.YValues[0] = yValue;
}
}
// sets an axis to automatic or restrains it to its current values
// this is used that none of the set values is changed when jitter is applied, so that the chart stays the same
private void SetAutomaticUpdateOfAxis(Axis axis, bool enabled) {
if (enabled) {
axis.Maximum = double.NaN;
axis.Minimum = double.NaN;
axis.MajorGrid.Interval = double.NaN;
axis.MajorTickMark.Interval = double.NaN;
axis.LabelStyle.Interval = double.NaN;
} else {
axis.Minimum = axis.Minimum;
axis.Maximum = axis.Maximum;
axis.MajorGrid.Interval = axis.MajorGrid.Interval;
axis.MajorTickMark.Interval = axis.MajorTickMark.Interval;
axis.LabelStyle.Interval = axis.LabelStyle.Interval;
}
}
private void AddDataPoint(IRun run) {
double? xValue;
double? yValue;
double? sizeValue;
Series series = this.chart.Series[0];
xValue = GetValue(run, xAxisValue);
yValue = GetValue(run, yAxisValue);
sizeValue = GetValue(run, sizeAxisValue);
if (xValue.HasValue && yValue.HasValue && sizeValue.HasValue) {
xValue = xValue.Value;
yValue = yValue.Value;
if (run.Visible) {
DataPoint point = new DataPoint(xValue.Value, new double[] { yValue.Value, sizeValue.Value });
point.Tag = run;
series.Points.Add(point);
if (!runToDataPointMapping.ContainsKey(run)) runToDataPointMapping.Add(run, new List());
runToDataPointMapping[run].Add(point);
UpdateRun(run);
}
}
}
private double? GetValue(IRun run, string columnName) {
if (run == null || string.IsNullOrEmpty(columnName))
return null;
if (Enum.IsDefined(typeof(AxisDimension), columnName)) {
AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
return GetValue(run, axisDimension);
} else if (Enum.IsDefined(typeof(SizeDimension), columnName)) {
SizeDimension sizeDimension = (SizeDimension)Enum.Parse(typeof(SizeDimension), columnName);
return GetValue(run, sizeDimension);
} else {
int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
IItem value = Content.GetValue(run, columnIndex);
if (value == null)
return null;
DoubleValue doubleValue = value as DoubleValue;
IntValue intValue = value as IntValue;
TimeSpanValue timeSpanValue = value as TimeSpanValue;
double? ret = null;
if (doubleValue != null) {
if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
ret = doubleValue.Value;
} else if (intValue != null)
ret = intValue.Value;
else if (timeSpanValue != null) {
ret = timeSpanValue.Value.TotalSeconds;
} else
ret = GetCategoricalValue(columnIndex, value.ToString());
return ret;
}
}
private double GetCategoricalValue(int dimension, string value) {
if (!this.categoricalMapping.ContainsKey(dimension)) {
this.categoricalMapping[dimension] = new Dictionary