#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), false)]
public partial class BubbleChartView : ItemView {
private enum SizeDimension { Constant = 0 }
private enum AxisDimension { Index = 0 }
private readonly Dictionary itemToIndexMapping = new Dictionary();
private readonly Dictionary> categoricalMapping = new Dictionary>();
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;
public new RecursiveDataItem Content {
get { return (RecursiveDataItem)base.Content; }
set { base.Content = value; }
}
public BubbleChartView() {
InitializeComponent();
}
protected override void OnContentChanged() {
base.OnContentChanged();
UpdateTreeView();
UpdateLevelControl();
UpdateComboBoxes();
UpdateDataPoints();
//UpdateCaption();
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
levelNumericUpDown.Enabled = Content != null;
// ToDo
}
#region Tree Queries
private IEnumerable GetAvailableItems() {
return IterateCheckedNodes()
.Select(n => (RecursiveDataItem)n.Tag);
}
private IEnumerable GetAvailableKeys() {
return GetAvailableItems()
.SelectMany(n => n.Data.Keys)
.Distinct();
}
#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 != AxisDimension.Index.ToString();
chart.ChartAreas[0].AxisY.IsMarginVisible = YAxisValue != AxisDimension.Index.ToString();
if (Content != null) {
var items = GetAvailableItems();
foreach (var item in items) {
var x = GetValue(item, XAxisValue);
var y = GetValue(item, YAxisValue);
var s = GetValue(item, SizeAxisValue);
if (x.HasValue && y.HasValue && s.HasValue) {
var dataPoint = new DataPoint(x.Value, new[] { y.Value, s.Value });
series.Points.Add(dataPoint);
}
}
}
}
private double? GetValue(RecursiveDataItem item, string key) {
if (item == null || string.IsNullOrEmpty(key))
return null;
if (Enum.IsDefined(typeof(AxisDimension), key)) {
var axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), key);
return GetValue(item, axisDimension);
} else if (Enum.IsDefined(typeof(SizeDimension), key)) {
var sizeDimension = (SizeDimension)Enum.Parse(typeof(SizeDimension), key);
return GetValue(item, sizeDimension);
} else if (item.Data.ContainsKey(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(item, key, value.ToString());
return ret;
} else {
return null;
}
}
private double? GetCategoricalValue(RecursiveDataItem item, string key, string value) {
if (!categoricalMapping.ContainsKey(key)) {
categoricalMapping[key] = new Dictionary