Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Optimization.Views/3.3/RunCollectionBubbleChartView.cs @ 7259

Last change on this file since 7259 was 3764, checked in by mkommend, 14 years ago

adapted view captions (ticket #893)

File size: 21.6 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;
35using HeuristicLab.Data;
36using System.Threading;
37
38namespace HeuristicLab.Optimization.Views {
39  [View("RunCollection BubbleChart")]
40  [Content(typeof(RunCollection), false)]
41  public partial class RunCollectionBubbleChartView : AsynchronousContentView {
42    private enum SizeDimension { Constant = 0 }
43    private enum AxisDimension { Index = 0 }
44
45    private string xAxisValue;
46    private string yAxisValue;
47    private string sizeAxisValue;
48
49    private Dictionary<int, Dictionary<object, double>> categoricalMapping;
50    private Dictionary<IRun, double> xJitter;
51    private Dictionary<IRun, double> yJitter;
52    private double xJitterFactor = 0.0;
53    private double yJitterFactor = 0.0;
54    private Random random;
55    private bool isSelecting = false;
56
57    public RunCollectionBubbleChartView() {
58      InitializeComponent();
59
60      this.categoricalMapping = new Dictionary<int, Dictionary<object, double>>();
61      this.xJitter = new Dictionary<IRun, double>();
62      this.yJitter = new Dictionary<IRun, double>();
63      this.random = new Random();
64      this.colorDialog.Color = Color.Black;
65      this.colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
66      this.isSelecting = false;
67
68      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
69      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
70      this.chart.ChartAreas[0].CursorX.Interval = 1;
71      this.chart.ChartAreas[0].CursorY.Interval = 1;
72      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !this.isSelecting;
73      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !this.isSelecting;
74    }
75
76    public new RunCollection Content {
77      get { return (RunCollection)base.Content; }
78      set { base.Content = value; }
79    }
80
81    public IStringConvertibleMatrix Matrix {
82      get { return this.Content; }
83    }
84
85    protected override void RegisterContentEvents() {
86      base.RegisterContentEvents();
87      Content.Reset += new EventHandler(Content_Reset);
88      Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
89      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
90      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
91      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
92      RegisterRunEvents(Content);
93    }
94    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
95      foreach (IRun run in runs)
96        run.Changed += new EventHandler(run_Changed);
97    }
98    protected override void DeregisterContentEvents() {
99      base.DeregisterContentEvents();
100      Content.Reset -= new EventHandler(Content_Reset);
101      Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
102      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
103      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
104      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
105      DeregisterRunEvents(Content);
106    }
107    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
108      foreach (IRun run in runs)
109        run.Changed -= new EventHandler(run_Changed);
110    }
111
112    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
113      DeregisterRunEvents(e.OldItems);
114      RegisterRunEvents(e.Items);
115    }
116    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
117      DeregisterRunEvents(e.Items);
118    }
119    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
120      RegisterRunEvents(e.Items);
121    }
122    private void run_Changed(object sender, EventArgs e) {
123      if (InvokeRequired)
124        this.Invoke(new EventHandler(run_Changed), sender, e);
125      else {
126        IRun run = (IRun)sender;
127        UpdateRun(run);
128      }
129    }
130
131    private void UpdateRun(IRun run) {
132      DataPoint point = this.chart.Series[0].Points.Where(p => p.Tag == run).SingleOrDefault();
133      if (point != null) {
134        point.Color = run.Color;
135        if (!run.Visible)
136          this.chart.Series[0].Points.Remove(point);
137      } else
138        AddDataPoint(run);
139        UpdateCursorInterval();
140
141
142      if (this.chart.Series[0].Points.Count == 0)
143        noRunsLabel.Visible = true;
144      else
145        noRunsLabel.Visible = false;
146    }
147
148    protected override void OnContentChanged() {
149      base.OnContentChanged();
150      this.categoricalMapping.Clear();
151      UpdateComboBoxes();
152      UpdateDataPoints();
153    }
154    private void Content_ColumnNamesChanged(object sender, EventArgs e) {
155      if (InvokeRequired)
156        Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
157      else
158        UpdateComboBoxes();
159    }
160
161    private void UpdateComboBoxes() {
162      string selectedXAxis = (string)this.xAxisComboBox.SelectedItem;
163      string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
164      string selectedSizeAxis = (string)this.sizeComboBox.SelectedItem;
165      this.xAxisComboBox.Items.Clear();
166      this.yAxisComboBox.Items.Clear();
167      this.sizeComboBox.Items.Clear();
168      if (Content != null) {
169        string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
170        this.xAxisComboBox.Items.AddRange(additionalAxisDimension);
171        this.xAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
172        this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
173        this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
174        string[] additionalSizeDimension = Enum.GetNames(typeof(SizeDimension));
175        this.sizeComboBox.Items.AddRange(additionalSizeDimension);
176        this.sizeComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
177        this.sizeComboBox.SelectedItem = SizeDimension.Constant.ToString();
178
179        bool changed = false;
180        if (selectedXAxis != null && xAxisComboBox.Items.Contains(selectedXAxis)) {
181          xAxisComboBox.SelectedItem = selectedXAxis;
182          changed = true;
183        }
184        if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
185          yAxisComboBox.SelectedItem = selectedYAxis;
186          changed = true;
187        }
188        if (selectedSizeAxis != null && sizeComboBox.Items.Contains(selectedSizeAxis)) {
189          sizeComboBox.SelectedItem = selectedSizeAxis;
190          changed = true;
191        }
192        if (changed)
193          UpdateDataPoints();
194      }
195    }
196
197    private void Content_Reset(object sender, EventArgs e) {
198      if (InvokeRequired)
199        Invoke(new EventHandler(Content_Reset), sender, e);
200      else {
201        this.categoricalMapping.Clear();
202        UpdateDataPoints();
203      }
204    }
205
206    private void UpdateDataPoints() {
207      Series series = this.chart.Series[0];
208      series.Points.Clear();
209      if (Content != null) {
210        foreach (IRun run in this.Content)
211          this.AddDataPoint(run);
212
213        //check to correct max bubble size
214        if (this.chart.Series[0].Points.Select(p => p.YValues[1]).Distinct().Count() == 1)
215          this.chart.Series[0]["BubbleMaxSize"] = "2";
216        else
217          this.chart.Series[0]["BubbleMaxSize"] = "7";
218
219        if (this.chart.Series[0].Points.Count == 0)
220          noRunsLabel.Visible = true;
221        else
222          noRunsLabel.Visible = false;
223        UpdateCursorInterval();
224      }
225    }
226    private void AddDataPoint(IRun run) {
227      double? xValue;
228      double? yValue;
229      double? sizeValue;
230      Series series = this.chart.Series[0];
231      int row = this.Content.ToList().IndexOf(run);
232
233      if (!xAxisComboBox.DroppedDown)
234        this.xAxisValue = (string)xAxisComboBox.SelectedItem;
235      if (!yAxisComboBox.DroppedDown)
236        this.yAxisValue = (string)yAxisComboBox.SelectedItem;
237      if (!sizeComboBox.DroppedDown)
238        this.sizeAxisValue = (string)sizeComboBox.SelectedItem;
239
240      xValue = GetValue(run, this.xAxisValue);
241      yValue = GetValue(run, this.yAxisValue);
242      sizeValue = GetValue(run, this.sizeAxisValue);
243
244      if (xValue.HasValue && yValue.HasValue && sizeValue.HasValue) {
245        xValue = xValue.Value;
246        if (!xJitterFactor.IsAlmost(0.0))
247          xValue += 0.1 * GetXJitter(run) * xJitterFactor * (this.chart.ChartAreas[0].AxisX.Maximum - this.chart.ChartAreas[0].AxisX.Minimum);
248        yValue = yValue.Value;
249        if (!yJitterFactor.IsAlmost(0.0))
250          yValue += 0.1 * GetYJitter(run) * yJitterFactor * (this.chart.ChartAreas[0].AxisY.Maximum - this.chart.ChartAreas[0].AxisY.Minimum);
251        if (run.Visible) {
252          DataPoint point = new DataPoint(xValue.Value, new double[] { yValue.Value, sizeValue.Value });
253          point.Tag = run;
254          point.Color = run.Color;
255          series.Points.Add(point);
256        }
257      }
258    }
259    private double? GetValue(IRun run, string columnName) {
260      if (run == null || string.IsNullOrEmpty(columnName))
261        return null;
262
263      if (Enum.IsDefined(typeof(AxisDimension), columnName)) {
264        AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
265        return GetValue(run, axisDimension);
266      } else if (Enum.IsDefined(typeof(SizeDimension), columnName)) {
267        SizeDimension sizeDimension = (SizeDimension)Enum.Parse(typeof(SizeDimension), columnName);
268        return GetValue(run, sizeDimension);
269      } else {
270        int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
271        IItem value = Content.GetValue(run, columnIndex);
272        if (value == null)
273          return null;
274
275        DoubleValue doubleValue = value as DoubleValue;
276        IntValue intValue = value as IntValue;
277        double? ret = null;
278        if (doubleValue != null) {
279          if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
280            ret = doubleValue.Value;
281        } else if (intValue != null)
282          ret = intValue.Value;
283        else
284          ret = GetCategoricalValue(columnIndex, value.ToString());
285
286        return ret;
287      }
288    }
289    private double GetCategoricalValue(int dimension, string value) {
290      if (!this.categoricalMapping.ContainsKey(dimension))
291        this.categoricalMapping[dimension] = new Dictionary<object, double>();
292      if (!this.categoricalMapping[dimension].ContainsKey(value)) {
293        if (this.categoricalMapping[dimension].Values.Count == 0)
294          this.categoricalMapping[dimension][value] = 1.0;
295        else
296          this.categoricalMapping[dimension][value] = this.categoricalMapping[dimension].Values.Max() + 1.0;
297      }
298      return this.categoricalMapping[dimension][value];
299    }
300    private double GetValue(IRun run, AxisDimension axisDimension) {
301      double value = double.NaN;
302      switch (axisDimension) {
303        case AxisDimension.Index: {
304            value = Content.ToList().IndexOf(run);
305            break;
306          }
307        default: {
308            throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
309          }
310      }
311      return value;
312    }
313    private double GetValue(IRun run, SizeDimension sizeDimension) {
314      double value = double.NaN;
315      switch (sizeDimension) {
316        case SizeDimension.Constant: {
317            value = 2;
318            break;
319          }
320        default: {
321            throw new ArgumentException("No handling strategy for " + sizeDimension.ToString() + " is defined.");
322          }
323      }
324      return value;
325    }
326    private void UpdateCursorInterval() {
327      Series series = chart.Series[0];
328      double[] xValues = (from point in series.Points
329                          where !point.IsEmpty
330                          select point.XValue)
331                    .DefaultIfEmpty(1.0)
332                    .ToArray();
333      double[] yValues = (from point in series.Points
334                          where !point.IsEmpty
335                          select point.YValues[0])
336                    .DefaultIfEmpty(1.0)
337                    .ToArray();
338
339      double xRange = xValues.Max() - xValues.Min();
340      double yRange = yValues.Max() - yValues.Min();
341      if(xRange.IsAlmost(0.0)) xRange = 1.0;
342      if(yRange.IsAlmost(0.0)) yRange = 1.0;
343      double xDigits = (int)Math.Log10(xRange) - 3;
344      double yDigits = (int)Math.Log10(yRange) - 3;
345      double xZoomInterval = Math.Pow(10, xDigits);
346      double yZoomInterval = Math.Pow(10, yDigits);
347      this.chart.ChartAreas[0].CursorX.Interval = xZoomInterval;
348      this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
349    }
350
351    #region drag and drop and tooltip
352    private IRun draggedRun;
353    private void chart_MouseDown(object sender, MouseEventArgs e) {
354      HitTestResult h = this.chart.HitTest(e.X, e.Y);
355      if (h.ChartElementType == ChartElementType.DataPoint) {
356        IRun run = (IRun)((DataPoint)h.Object).Tag;
357        if (e.Clicks >= 2) {
358          IContentView view = MainFormManager.MainForm.ShowContent(run);
359          if (view != null) {
360            view.ReadOnly = this.ReadOnly;
361            view.Locked = this.Locked;
362          }
363        } else
364          this.draggedRun = run;
365        this.chart.ChartAreas[0].CursorX.SetSelectionPosition(double.NaN, double.NaN);
366        this.chart.ChartAreas[0].CursorY.SetSelectionPosition(double.NaN, double.NaN);
367      }
368    }
369
370    private void chart_MouseUp(object sender, MouseEventArgs e) {
371      if (isSelecting) {
372        System.Windows.Forms.DataVisualization.Charting.Cursor xCursor = chart.ChartAreas[0].CursorX;
373        System.Windows.Forms.DataVisualization.Charting.Cursor yCursor = chart.ChartAreas[0].CursorY;
374
375        double minX = Math.Min(xCursor.SelectionStart, xCursor.SelectionEnd);
376        double maxX = Math.Max(xCursor.SelectionStart, xCursor.SelectionEnd);
377        double minY = Math.Min(yCursor.SelectionStart, yCursor.SelectionEnd);
378        double maxY = Math.Max(yCursor.SelectionStart, yCursor.SelectionEnd);
379
380        //check for click to select model
381        if (minX == maxX && minY == maxY) {
382          HitTestResult hitTest = chart.HitTest(e.X, e.Y);
383          if (hitTest.ChartElementType == ChartElementType.DataPoint) {
384            int pointIndex = hitTest.PointIndex;
385            IRun run = (IRun)this.chart.Series[0].Points[pointIndex].Tag;
386            run.Color = colorDialog.Color;
387          }
388        } else {
389          List<DataPoint> selectedPoints = new List<DataPoint>();
390          foreach (DataPoint p in this.chart.Series[0].Points) {
391            if (p.XValue >= minX && p.XValue < maxX &&
392              p.YValues[0] >= minY && p.YValues[0] < maxY) {
393              selectedPoints.Add(p);
394            }
395          }
396          foreach (DataPoint p in selectedPoints) {
397            IRun run = (IRun)p.Tag;
398            run.Color = colorDialog.Color;
399          }
400        }
401        this.chart.ChartAreas[0].CursorX.SelectionStart = this.chart.ChartAreas[0].CursorX.SelectionEnd;
402        this.chart.ChartAreas[0].CursorY.SelectionStart = this.chart.ChartAreas[0].CursorY.SelectionEnd;
403      }
404    }
405
406    private void chart_MouseMove(object sender, MouseEventArgs e) {
407      HitTestResult h = this.chart.HitTest(e.X, e.Y);
408      if (!Locked) {
409        if (this.draggedRun != null && h.ChartElementType != ChartElementType.DataPoint) {
410          DataObject data = new DataObject();
411          data.SetData("Type", draggedRun.GetType());
412          data.SetData("Value", draggedRun);
413          if (ReadOnly)
414            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
415          else {
416            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
417            if ((result & DragDropEffects.Move) == DragDropEffects.Move)
418              Content.Remove(draggedRun);
419          }
420          this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !isSelecting;
421          this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !isSelecting;
422          this.draggedRun = null;
423        }
424      }
425      string newTooltipText = string.Empty;
426      string oldTooltipText;
427      if (h.ChartElementType == ChartElementType.DataPoint) {
428        IRun run = (IRun)((DataPoint)h.Object).Tag;
429        newTooltipText = BuildTooltip(run);
430      }
431
432      oldTooltipText = this.tooltip.GetToolTip(chart);
433      if (newTooltipText != oldTooltipText)
434        this.tooltip.SetToolTip(chart, newTooltipText);
435    }
436
437    private string BuildTooltip(IRun run) {
438      string tooltip;
439      tooltip = run.Name + System.Environment.NewLine;
440
441      double? xValue = this.GetValue(run, (string)xAxisComboBox.SelectedItem);
442      double? yValue = this.GetValue(run, (string)yAxisComboBox.SelectedItem);
443      double? sizeValue = this.GetValue(run, (string)sizeComboBox.SelectedItem);
444
445      string xString = xValue == null ? string.Empty : xValue.Value.ToString();
446      string yString = yValue == null ? string.Empty : yValue.Value.ToString();
447      string sizeString = sizeValue == null ? string.Empty : sizeValue.Value.ToString();
448
449      tooltip += xAxisComboBox.SelectedItem + " : " + xString + Environment.NewLine;
450      tooltip += yAxisComboBox.SelectedItem + " : " + yString + Environment.NewLine;
451      tooltip += sizeComboBox.SelectedItem + " : " + sizeString + Environment.NewLine;
452
453      return tooltip;
454    }
455    #endregion
456
457    #region GUI events and updating
458    private double GetXJitter(IRun run) {
459      if (!this.xJitter.ContainsKey(run))
460        this.xJitter[run] = random.NextDouble() * 2.0 - 1.0;
461      return this.xJitter[run];
462    }
463    private double GetYJitter(IRun run) {
464      if (!this.yJitter.ContainsKey(run))
465        this.yJitter[run] = random.NextDouble() * 2.0 - 1.0;
466      return this.yJitter[run];
467    }
468    private void jitterTrackBar_ValueChanged(object sender, EventArgs e) {
469      this.xJitterFactor = xTrackBar.Value / 100.0;
470      this.yJitterFactor = yTrackBar.Value / 100.0;
471      this.UpdateDataPoints();
472    }
473
474    private void AxisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
475      UpdateDataPoints();
476      UpdateAxisLabels();
477    }
478    private void UpdateAxisLabels() {
479      Axis xAxis = this.chart.ChartAreas[0].AxisX;
480      Axis yAxis = this.chart.ChartAreas[0].AxisY;
481      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
482      SetCustomAxisLabels(xAxis, xAxisComboBox.SelectedIndex - axisDimensionCount);
483      SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
484    }
485    private void SetCustomAxisLabels(Axis axis, int dimension) {
486      axis.CustomLabels.Clear();
487      if (categoricalMapping.ContainsKey(dimension)) {
488        CustomLabel label = null;
489        foreach (var pair in categoricalMapping[dimension]) {
490          string labelText = pair.Key.ToString();
491          if (labelText.Length > 25)
492            labelText = labelText.Substring(0, 25) + " ... ";
493          label = axis.CustomLabels.Add(pair.Value - 0.5, pair.Value + 0.5, labelText);
494          label.GridTicks = GridTickTypes.TickMark;
495        }
496        axis.IsLabelAutoFit = false;
497        axis.LabelStyle.Enabled = true;
498        axis.LabelStyle.Angle = 0;
499        axis.LabelStyle.TruncatedLabels = true;
500      }
501    }
502
503    private void zoomButton_CheckedChanged(object sender, EventArgs e) {
504      this.isSelecting = selectButton.Checked;
505      this.colorButton.Enabled = this.isSelecting;
506      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !isSelecting;
507      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !isSelecting;
508    }
509    private void colorButton_Click(object sender, EventArgs e) {
510      if (colorDialog.ShowDialog(this) == DialogResult.OK) {
511        this.colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
512      }
513    }
514    private Image GenerateImage(int width, int height, Color fillColor) {
515      Image colorImage = new Bitmap(width, height);
516      using (Graphics gfx = Graphics.FromImage(colorImage)) {
517        using (SolidBrush brush = new SolidBrush(fillColor)) {
518          gfx.FillRectangle(brush, 0, 0, width, height);
519        }
520      }
521      return colorImage;
522    }
523    #endregion
524  }
525}
Note: See TracBrowser for help on using the repository browser.