Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionBubbleChartView.cs @ 3376

Last change on this file since 3376 was 3376, checked in by swagner, 14 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.MainForm.WindowsForms;
31using HeuristicLab.MainForm;
32using System.Windows.Forms.DataVisualization.Charting;
33using HeuristicLab.Common;
34using HeuristicLab.Core;
35
36namespace HeuristicLab.Optimization.Views {
37  [View("RunCollection BubbleChart")]
38  [Content(typeof(RunCollection), false)]
39  public partial class RunCollectionBubbleChartView : AsynchronousContentView {
40    private Dictionary<int, Dictionary<object, double>> categoricalMapping;
41
42    public RunCollectionBubbleChartView() {
43      InitializeComponent();
44      Caption = "Run Collection Bubble Chart";
45      this.categoricalMapping = new Dictionary<int, Dictionary<object, double>>();
46      base.ReadOnly = true;
47    }
48
49    public RunCollectionBubbleChartView(RunCollection content)
50      : this() {
51      Content = content;
52    }
53
54    public new RunCollection Content {
55      get { return (RunCollection)base.Content; }
56      set { base.Content = value; }
57    }
58    public override bool ReadOnly {
59      get { return base.ReadOnly; }
60      set { /*not needed because results are always readonly */}
61    }
62
63    protected override void RegisterContentEvents() {
64      base.RegisterContentEvents();
65      Content.Reset += new EventHandler(Content_Reset);
66      Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
67    }
68
69    protected override void DeregisterContentEvents() {
70      base.DeregisterContentEvents();
71      Content.Reset -= new EventHandler(Content_Reset);
72      Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
73    }
74
75    protected override void OnContentChanged() {
76      base.OnContentChanged();
77      this.UpdateComboBoxes();
78    }
79
80    private void Content_ColumnNamesChanged(object sender, EventArgs e) {
81      if (InvokeRequired)
82        Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
83      else
84        UpdateComboBoxes();
85    }
86
87    private void UpdateComboBoxes() {
88      this.xAxisComboBox.Items.Clear();
89      this.yAxisComboBox.Items.Clear();
90      this.xAxisComboBox.Items.AddRange(Content.ColumnNames.ToArray());
91      this.yAxisComboBox.Items.AddRange(Content.ColumnNames.ToArray());
92    }
93
94    private void Content_Reset(object sender, EventArgs e) {
95      if (InvokeRequired)
96        Invoke(new EventHandler(Content_Reset), sender, e);
97      else {
98        this.categoricalMapping.Clear();
99        UpdateDataPoints();
100      }
101    }
102
103    private void UpdateDataPoints() {
104      Series series = this.chart.Series[0];
105      series.Points.Clear();
106
107      double? xValue;
108      double? yValue;
109      for (int row = 0; row < Content.Count; row++) {
110        xValue = GetValue(row, xAxisComboBox.SelectedIndex);
111        yValue = GetValue(row, yAxisComboBox.SelectedIndex);
112        if (xValue.HasValue && yValue.HasValue)
113          series.Points.Add(new DataPoint(xValue.Value, yValue.Value));
114      }
115    }
116
117    private void AxisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
118      UpdateDataPoints();
119      UpdateAxisLabels();
120    }
121
122    private double? GetValue(int row, int column) {
123      if (column < 0 || row < 0)
124        return null;
125
126      string value = Content.GetValue(row, column);
127      double d;
128      double? ret = null;
129      if (double.TryParse(value, out d))
130        ret = d;
131      else if (value != null)
132        ret = GetCategoricalValue(column, value);
133      return ret;
134    }
135    private double GetCategoricalValue(int dimension, object c) {
136      if (!this.categoricalMapping.ContainsKey(dimension))
137        this.categoricalMapping[dimension] = new Dictionary<object, double>();
138      if (!this.categoricalMapping[dimension].ContainsKey(c)) {
139        if (this.categoricalMapping[dimension].Values.Count == 0)
140          this.categoricalMapping[dimension][c] = 1.0;
141        else
142          this.categoricalMapping[dimension][c] = this.categoricalMapping[dimension].Values.Max() + 1.0;
143      }
144      return this.categoricalMapping[dimension][c];
145    }
146
147    private void UpdateAxisLabels() {
148      Axis xAxis = this.chart.ChartAreas[0].AxisX;
149      Axis yAxis = this.chart.ChartAreas[0].AxisY;
150      SetAxisLabels(xAxis, xAxisComboBox.SelectedIndex);
151      SetAxisLabels(yAxis, yAxisComboBox.SelectedIndex);
152    }
153    private void SetAxisLabels(Axis axis, int dimension) {
154      if (categoricalMapping.ContainsKey(dimension)) {
155        axis.CustomLabels.Clear();
156        CustomLabel label = null;
157        foreach (var pair in categoricalMapping[dimension]) {
158          label = axis.CustomLabels.Add(pair.Value - 0.5, pair.Value + 0.5, pair.Key.ToString());
159          label.GridTicks = GridTickTypes.TickMark;
160        }
161        axis.IsLabelAutoFit = false;
162        axis.LabelStyle.Enabled = true;
163        axis.LabelStyle.Angle = 0;
164        axis.LabelStyle.TruncatedLabels = true;
165      }
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.