#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 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.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HeuristicLab.MainForm.WindowsForms;
using HeuristicLab.MainForm;
using System.Windows.Forms.DataVisualization.Charting;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using System.Threading;
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 Dictionary> categoricalMapping;
private Dictionary xJitter;
private Dictionary yJitter;
private double xJitterFactor = 0.0;
private double yJitterFactor = 0.0;
private Random random;
private bool isSelecting = false;
public RunCollectionBubbleChartView() {
InitializeComponent();
this.categoricalMapping = new Dictionary>();
this.xJitter = new Dictionary();
this.yJitter = new Dictionary();
this.random = new Random();
this.colorDialog.Color = Color.Black;
this.colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
this.isSelecting = false;
this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
this.chart.ChartAreas[0].CursorX.Interval = 1;
this.chart.ChartAreas[0].CursorY.Interval = 1;
this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !this.isSelecting;
this.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; }
}
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);
RegisterRunEvents(Content);
}
protected virtual void RegisterRunEvents(IEnumerable runs) {
foreach (IRun run in runs)
run.Changed += new EventHandler(run_Changed);
}
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);
DeregisterRunEvents(Content);
}
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 (InvokeRequired)
this.Invoke(new EventHandler(run_Changed), sender, e);
else {
IRun run = (IRun)sender;
UpdateRun(run);
}
}
private void UpdateRun(IRun run) {
DataPoint point = this.chart.Series[0].Points.Where(p => p.Tag == run).SingleOrDefault();
if (point != null) {
point.Color = run.Color;
if (!run.Visible)
this.chart.Series[0].Points.Remove(point);
} else
AddDataPoint(run);
UpdateCursorInterval();
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();
}
private void Content_ColumnNamesChanged(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
else
UpdateComboBoxes();
}
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();
}
}
private void Content_Reset(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_Reset), sender, e);
else {
this.categoricalMapping.Clear();
UpdateDataPoints();
}
}
private void UpdateDataPoints() {
Series series = this.chart.Series[0];
series.Points.Clear();
if (Content != null) {
foreach (IRun run in this.Content)
this.AddDataPoint(run);
//check to correct max bubble size
if (this.chart.Series[0].Points.Select(p => p.YValues[1]).Distinct().Count() == 1)
this.chart.Series[0]["BubbleMaxSize"] = "2";
else
this.chart.Series[0]["BubbleMaxSize"] = "7";
if (this.chart.Series[0].Points.Count == 0)
noRunsLabel.Visible = true;
else
noRunsLabel.Visible = false;
UpdateCursorInterval();
}
}
private void AddDataPoint(IRun run) {
double? xValue;
double? yValue;
double? sizeValue;
Series series = this.chart.Series[0];
int row = this.Content.ToList().IndexOf(run);
if (!xAxisComboBox.DroppedDown)
this.xAxisValue = (string)xAxisComboBox.SelectedItem;
if (!yAxisComboBox.DroppedDown)
this.yAxisValue = (string)yAxisComboBox.SelectedItem;
if (!sizeComboBox.DroppedDown)
this.sizeAxisValue = (string)sizeComboBox.SelectedItem;
xValue = GetValue(run, this.xAxisValue);
yValue = GetValue(run, this.yAxisValue);
sizeValue = GetValue(run, this.sizeAxisValue);
if (xValue.HasValue && yValue.HasValue && sizeValue.HasValue) {
xValue = xValue.Value;
if (!xJitterFactor.IsAlmost(0.0))
xValue += 0.1 * GetXJitter(run) * xJitterFactor * (this.chart.ChartAreas[0].AxisX.Maximum - this.chart.ChartAreas[0].AxisX.Minimum);
yValue = yValue.Value;
if (!yJitterFactor.IsAlmost(0.0))
yValue += 0.1 * GetYJitter(run) * yJitterFactor * (this.chart.ChartAreas[0].AxisY.Maximum - this.chart.ChartAreas[0].AxisY.Minimum);
if (run.Visible) {
DataPoint point = new DataPoint(xValue.Value, new double[] { yValue.Value, sizeValue.Value });
point.Tag = run;
point.Color = run.Color;
series.Points.Add(point);
}
}
}
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;
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
ret = GetCategoricalValue(columnIndex, value.ToString());
return ret;
}
}
private double GetCategoricalValue(int dimension, string value) {
if (!this.categoricalMapping.ContainsKey(dimension))
this.categoricalMapping[dimension] = new Dictionary