Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3362 was 3350, checked in by mkommend, 15 years ago

implemented first version of View.ReadOnly and adapted some views to the new mechanism (ticket #973)

File size: 5.8 KB
RevLine 
[3349]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.Core;
34
35namespace HeuristicLab.Optimization.Views {
36  [View("RunCollection BubbleChart")]
37  [Content(typeof(RunCollection), false)]
38  public partial class RunCollectionBubbleChartView : AsynchronousContentView {
39    private Dictionary<int, Dictionary<object, double>> categoricalMapping;
40
41    public RunCollectionBubbleChartView() {
42      InitializeComponent();
43      Caption = "Run Collection Bubble Chart";
44      this.categoricalMapping = new Dictionary<int, Dictionary<object, double>>();
[3350]45      base.ReadOnly = true;
[3349]46    }
47
48    public RunCollectionBubbleChartView(RunCollection content)
49      : this() {
50      Content = content;
51    }
52
53    public new RunCollection Content {
54      get { return (RunCollection)base.Content; }
55      set { base.Content = value; }
56    }
[3350]57    public override bool ReadOnly {
58      get { return base.ReadOnly; }
59      set { /*not needed because results are always readonly */}
60    }
[3349]61
62    protected override void RegisterContentEvents() {
63      base.RegisterContentEvents();
64      Content.Reset += new EventHandler(Content_Reset);
65      Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
66    }
67
68    protected override void DeregisterContentEvents() {
69      base.DeregisterContentEvents();
70      Content.Reset -= new EventHandler(Content_Reset);
71      Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
72    }
73
74    protected override void OnContentChanged() {
75      base.OnContentChanged();
76      this.UpdateComboBoxes();
77    }
78
79    private void Content_ColumnNamesChanged(object sender, EventArgs e) {
80      if (InvokeRequired)
81        Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
82      else
83        UpdateComboBoxes();
84    }
85
86    private void UpdateComboBoxes() {
87      this.xAxisComboBox.Items.Clear();
88      this.yAxisComboBox.Items.Clear();
89      this.xAxisComboBox.Items.AddRange(Content.ColumnNames.ToArray());
90      this.yAxisComboBox.Items.AddRange(Content.ColumnNames.ToArray());
91    }
92
93    private void Content_Reset(object sender, EventArgs e) {
94      if (InvokeRequired)
95        Invoke(new EventHandler(Content_Reset), sender, e);
96      else {
97        this.categoricalMapping.Clear();
98        UpdateDataPoints();
99      }
100    }
101
102    private void UpdateDataPoints() {
103      Series series = this.chart.Series[0];
104      series.Points.Clear();
105
106      double? xValue;
107      double? yValue;
108      for (int row = 0; row < Content.Count; row++) {
109        xValue = GetValue(row, xAxisComboBox.SelectedIndex);
110        yValue = GetValue(row, yAxisComboBox.SelectedIndex);
111        if (xValue.HasValue && yValue.HasValue)
112          series.Points.Add(new DataPoint(xValue.Value, yValue.Value));
113      }
114    }
115
116    private void AxisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
117      UpdateDataPoints();
118      UpdateAxisLabels();
119    }
120
121    private double? GetValue(int row, int column) {
122      if (column < 0 || row < 0)
123        return null;
124
125      string value = Content.GetValue(row, column);
126      double d;
127      double? ret = null;
128      if (double.TryParse(value, out d))
129        ret = d;
130      else if (value != null)
131        ret = GetCategoricalValue(column, value);
132      return ret;
133    }
134    private double GetCategoricalValue(int dimension, object c) {
135      if (!this.categoricalMapping.ContainsKey(dimension))
136        this.categoricalMapping[dimension] = new Dictionary<object, double>();
137      if (!this.categoricalMapping[dimension].ContainsKey(c)) {
138        if (this.categoricalMapping[dimension].Values.Count == 0)
139          this.categoricalMapping[dimension][c] = 1.0;
140        else
141          this.categoricalMapping[dimension][c] = this.categoricalMapping[dimension].Values.Max() + 1.0;
142      }
143      return this.categoricalMapping[dimension][c];
144    }
145
146    private void UpdateAxisLabels() {
147      Axis xAxis = this.chart.ChartAreas[0].AxisX;
148      Axis yAxis = this.chart.ChartAreas[0].AxisY;
149      SetAxisLabels(xAxis, xAxisComboBox.SelectedIndex);
150      SetAxisLabels(yAxis, yAxisComboBox.SelectedIndex);
151    }
152    private void SetAxisLabels(Axis axis, int dimension) {
153      if (categoricalMapping.ContainsKey(dimension)) {
154        axis.CustomLabels.Clear();
155        CustomLabel label = null;
156        foreach (var pair in categoricalMapping[dimension]) {
157          label = axis.CustomLabels.Add(pair.Value - 0.5, pair.Value + 0.5, pair.Key.ToString());
158          label.GridTicks = GridTickTypes.TickMark;
159        }
160        axis.IsLabelAutoFit = false;
161        axis.LabelStyle.Enabled = true;
162        axis.LabelStyle.Angle = 0;
163        axis.LabelStyle.TruncatedLabels = true;
164      }
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.