#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Core.Views;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
namespace HeuristicLab.Optimization.BubbleChart {
[View("Bubble Chart (Recursive)")]
[Content(typeof(RecursiveDataItem), true)]
public partial class BubbleChartView : ItemView {
private const string Separator = ":";
private enum SizeDimension { Constant = 0 }
private enum AxisDimension { Index = 0 }
private readonly Dictionary itemToIndexMapping = new Dictionary();
private readonly Dictionary> categoricalMapping = new Dictionary>();
private readonly Dictionary xJitter = new Dictionary();
private readonly Dictionary yJitter = new Dictionary();
private readonly Random random = new Random();
private string XAxisValue { get { return (string)xAxisComboBox.SelectedItem; } }
private string YAxisValue { get { return (string)yAxisComboBox.SelectedItem; } }
private string SizeAxisValue { get { return (string)sizeComboBox.SelectedItem; } }
private bool updating = false;
private double xJitterFactor = 0.0;
private double yJitterFactor = 0.0;
public new RecursiveDataItem Content {
get { return (RecursiveDataItem)base.Content; }
set { base.Content = value; }
}
public BubbleChartView() {
InitializeComponent();
chart.CustomizeAllChartAreas();
chart.ChartAreas[0].CursorX.Interval = 1;
chart.ChartAreas[0].CursorY.Interval = 1;
chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
}
protected override void OnContentChanged() {
base.OnContentChanged();
UpdateTreeView();
UpdateLevelControl();
UpdateComboBoxes();
UpdateDataPoints();
//UpdateCaption();
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
levelNumericUpDown.Enabled = Content != null;
xJitterTrackBar.Enabled = Content != null;
yJitterTrackBar.Enabled = Content != null;
xAxisComboBox.Enabled = Content != null;
yAxisComboBox.Enabled = Content != null;
}
#region Tree Queries
private IEnumerable GetAvailableItems() {
return IterateCheckedNodes()
.Select(n => (RecursiveDataItem)n.Tag);
}
private IEnumerable GetAvailableKeys() {
var collector = new List();
GetAvailableKeys(Content, collector);
return collector.Distinct();
}
private void GetAvailableKeys(RecursiveDataItem node, List collector) {
collector.AddRange(node.Data.Keys);
foreach (var child in node.Children)
GetAvailableKeys(child, collector);
}
#endregion
#region Update Controls
private void UpdateLevelControl() {
if (Content == null) return;
if (treeView.Nodes.Count > 0)
levelNumericUpDown.Maximum = IterateAllNodes().Max(t => t.Level);
else
levelNumericUpDown.Maximum = 0;
}
private void UpdateTreeView() {
treeView.Nodes.Clear();
if (Content != null)
treeView.Nodes.Add(CreateTreeNode(Content));
treeView.ExpandAll();
splitContainer.Panel1Collapsed = treeView.Nodes.Count == 0;
if (treeView.Nodes.Count > 0)
treeView.SelectedNode = treeView.Nodes[0];
}
private TreeNode CreateTreeNode(RecursiveDataItem item) {
var node = new TreeNode(item.Name) {
Tag = item,
Checked = true
};
foreach (var child in item.Children)
node.Nodes.Add(CreateTreeNode(child));
return node;
}
private void UpdateTreeViewCheckBoxes() {
int level = (int)levelNumericUpDown.Value;
bool includeChildren = includeChildrenCheckBox.Checked;
foreach (var node in IterateAllNodes()) {
bool @checked = includeChildren ? node.Level >= level : node.Level == level;
if (node.Checked != @checked)
node.Checked = @checked;
}
}
private void UpdateComboBoxes() {
var selectedXAxis = (string)xAxisComboBox.SelectedItem;
var selectedYAxis = (string)yAxisComboBox.SelectedItem;
var selectedSizeAxis = (string)sizeComboBox.SelectedItem;
xAxisComboBox.Items.Clear();
yAxisComboBox.Items.Clear();
sizeComboBox.Items.Clear();
if (Content != null) {
var axisNames = GetAvailableKeys().ToArray();
var additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
var additionalSizeDimension = Enum.GetNames(typeof(SizeDimension));
//xAxisComboBox.Items.AddRange(additionalAxisDimension);
xAxisComboBox.Items.AddRange(axisNames);
//yAxisComboBox.Items.AddRange(additionalAxisDimension);
yAxisComboBox.Items.AddRange(axisNames);
sizeComboBox.Items.AddRange(additionalSizeDimension);
sizeComboBox.Items.AddRange(axisNames);
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;
} else sizeComboBox.SelectedItem = SizeDimension.Constant.ToString();
if (changed) {
UpdateDataPoints();
UpdateAxisLabels();
}
}
}
private void UpdateDataPoints() {
var series = chart.Series[0];
series.Points.Clear();
itemToIndexMapping.Clear();
categoricalMapping.Clear();
RebuildInverseIndex();
chart.ChartAreas[0].AxisX.IsMarginVisible = !(XAxisValue != null && XAxisValue.Contains(AxisDimension.Index.ToString()));
chart.ChartAreas[0].AxisY.IsMarginVisible = !(YAxisValue != null && YAxisValue.Contains(AxisDimension.Index.ToString()));
if (Content != null && !string.IsNullOrEmpty(XAxisValue) && !string.IsNullOrEmpty(YAxisValue)) {
var xss = GetValues(XAxisValue);
var yss = GetValues(YAxisValue);
if (xss.Count == yss.Count) {
for (int i = 0; i < xss.Count; i++)
AddPoints(xss[i], yss[i], series);
} else if (xss.Count == 1 || yss.Count == 1) {
for (int i = 0; i < xss.Count; i++)
for (int j = 0; j < yss.Count; j++)
AddPoints(xss[i], yss[j], series);
}
}
if (chart.Series[0].Points.Count == 0) {
noDataLabel.Visible = true;
} else {
noDataLabel.Visible = false;
UpdateMarkerSizes();
UpdateCursorInterval();
}
xJitterTrackBar.Value = 0;
yJitterTrackBar.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();
}
void AddPoints(Tuple xs, Tuple ys, Series series) {
if (xs.Item1.Length != ys.Item1.Length) return;
for (int k = 0; k < xs.Item1.Length; k++) {
series.Points.Add(new DataPoint(xs.Item1[k], new[] { ys.Item1[k], 1.0 }) {
Tag = Tuple.Create(xs.Item1[k], ys.Item1[k], xs.Item2, ys.Item2)
});
}
}
private List> GetValues(string key) {
var collector = new List>();
GetValues(Content, key, collector);
return collector;
}
private void GetValues(RecursiveDataItem node, string key, List> collector) {
IItem item;
if (node.Data.TryGetValue(key, out item)) {
var value = ConvertToDoubles(node, key);
collector.Add(Tuple.Create(value, node));
}
foreach (var child in node.Children)
GetValues(child, key, collector);
}
private double[] ConvertToDoubles(RecursiveDataItem item, string key) {
IItem value = item.Data[key];
var doubleValue = value as DoubleValue;
var doubleArray = value as DoubleArray;
var intValue = value as IntValue;
var intArray = value as IntArray;
var timeSpanValue = value as TimeSpanValue;
if (doubleValue != null) return new double[1] { doubleValue.Value };
if (intValue != null) return new double[1] { intValue.Value };
if (timeSpanValue != null) return new double[1] { timeSpanValue.Value.TotalSeconds };
if (doubleArray != null) return doubleArray.ToArray();
if (intArray != null) return intArray.Select(v => (double)v).ToArray();
return new double[1] { GetCategoricalValue(key, value.ToString()).Value };
}
private IEnumerable> GetValues(IEnumerable items, string key) {
if (key == null) yield break;
var keyTokens = key.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
if (keyTokens.Length == 1) {
if (Enum.IsDefined(typeof(SizeDimension), key)) {
var sizeDimension = (SizeDimension)Enum.Parse(typeof(SizeDimension), key);
yield return GetValue(null, sizeDimension).ToEnumerable();
} else
foreach (var item in items) {
if (Enum.IsDefined(typeof(AxisDimension), key)) {
var axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), key);
yield return GetValue(item, axisDimension).ToEnumerable();
} else if (item.Data.ContainsKey(key)) {
yield return item.Data[key] as IEnumerable ?? ConvertToDouble(item, key).ToEnumerable();
}
}
} else if (keyTokens.Length == 2) {
string parentName = keyTokens[0];
key = keyTokens[1];
foreach (var item in items.Where(item => item.Name == parentName)) {
foreach (var child in item.Children.Where(child => child.Data.ContainsKey(key))) {
yield return child.Data[key] as IEnumerable ?? ConvertToDouble(child, key).ToEnumerable();
}
}
} else throw new InvalidOperationException("key either contains a single key or a child/data key");
}
private IEnumerable GetValues(RecursiveDataItem parent, string key) {
if (parent == null || string.IsNullOrEmpty(key))
return Enumerable.Empty();
var tokens = key.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length == 2) {
var parentKey = tokens[0];
var dataKey = tokens[1];
if (parent.Name == parentKey) {
return parent.Children
.Where(child => child.Data.ContainsKey(dataKey))
.Select(child => ConvertToDouble(child, dataKey));
}
return Enumerable.Empty();
} else {
var item = parent;
if (item == null || string.IsNullOrEmpty(key))
return Enumerable.Empty();
if (Enum.IsDefined(typeof(AxisDimension), key)) {
var axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), key);
return GetValue(item, axisDimension).ToEnumerable();
} else if (Enum.IsDefined(typeof(SizeDimension), key)) {
var sizeDimension = (SizeDimension)Enum.Parse(typeof(SizeDimension), key);
return GetValue(item, sizeDimension).ToEnumerable();
} else if (item.Data.ContainsKey(key)) {
return ConvertToDouble(item, key).ToEnumerable();
} else {
return Enumerable.Empty();
}
}
}
private double ConvertToDouble(RecursiveDataItem item, string key) {
IItem value = item.Data[key];
var doubleValue = value as DoubleValue;
var intValue = value as IntValue;
var 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(key, value.ToString());
return ret.Value;
}
private double? GetCategoricalValue(string key, string value) {
if (!categoricalMapping.ContainsKey(key)) {
categoricalMapping[key] = new Dictionary