Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RegressionBenchmarks/HeuristicLab.Optimization.Views/3.3/RunCollectionBubbleChartView.cs @ 7085

Last change on this file since 7085 was 6673, checked in by mkommend, 13 years ago

#1624: Implemented RunCollectionContentConstraint to hide specific runs.

File size: 28.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using System.Windows.Forms.DataVisualization.Charting;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33
34namespace HeuristicLab.Optimization.Views {
35  [View("RunCollection BubbleChart")]
36  [Content(typeof(RunCollection), false)]
37  public partial class RunCollectionBubbleChartView : AsynchronousContentView {
38    private enum SizeDimension { Constant = 0 }
39    private enum AxisDimension { Index = 0 }
40
41    private string xAxisValue;
42    private string yAxisValue;
43    private string sizeAxisValue;
44
45    private Dictionary<IRun, List<DataPoint>> runToDataPointMapping;
46    private Dictionary<int, Dictionary<object, double>> categoricalMapping;
47    private Dictionary<IRun, double> xJitter;
48    private Dictionary<IRun, double> yJitter;
49    private double xJitterFactor = 0.0;
50    private double yJitterFactor = 0.0;
51    private Random random;
52    private bool isSelecting = false;
53    private bool suppressUpdates = false;
54
55    public RunCollectionBubbleChartView() {
56      InitializeComponent();
57
58      chart.ContextMenuStrip.Items.Insert(0, hideRunToolStripMenuItem);
59      chart.ContextMenuStrip.Items.Insert(1, openBoxPlotViewToolStripMenuItem);
60      chart.ContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(ContextMenuStrip_Opening);
61
62      runToDataPointMapping = new Dictionary<IRun, List<DataPoint>>();
63      categoricalMapping = new Dictionary<int, Dictionary<object, double>>();
64      xJitter = new Dictionary<IRun, double>();
65      yJitter = new Dictionary<IRun, double>();
66      random = new Random();
67
68      colorDialog.Color = Color.Black;
69      colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
70      isSelecting = false;
71
72      chart.CustomizeAllChartAreas();
73      chart.ChartAreas[0].CursorX.Interval = 1;
74      chart.ChartAreas[0].CursorY.Interval = 1;
75      chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !this.isSelecting;
76      chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !this.isSelecting;
77    }
78
79    public new RunCollection Content {
80      get { return (RunCollection)base.Content; }
81      set { base.Content = value; }
82    }
83    public IStringConvertibleMatrix Matrix {
84      get { return this.Content; }
85    }
86
87    protected override void RegisterContentEvents() {
88      base.RegisterContentEvents();
89      Content.Reset += new EventHandler(Content_Reset);
90      Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
91      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
92      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
93      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
94      Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
95      RegisterRunEvents(Content);
96    }
97    protected override void DeregisterContentEvents() {
98      base.DeregisterContentEvents();
99      Content.Reset -= new EventHandler(Content_Reset);
100      Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
101      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
102      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
103      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
104      Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
105      DeregisterRunEvents(Content);
106    }
107    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
108      foreach (IRun run in runs)
109        run.Changed += new EventHandler(run_Changed);
110    }
111    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
112      foreach (IRun run in runs)
113        run.Changed -= new EventHandler(run_Changed);
114    }
115
116    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
117      DeregisterRunEvents(e.OldItems);
118      RegisterRunEvents(e.Items);
119    }
120    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
121      DeregisterRunEvents(e.Items);
122    }
123    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
124      RegisterRunEvents(e.Items);
125    }
126    private void run_Changed(object sender, EventArgs e) {
127      if (InvokeRequired)
128        this.Invoke(new EventHandler(run_Changed), sender, e);
129      else {
130        IRun run = (IRun)sender;
131        UpdateRun(run);
132      }
133    }
134
135    private void UpdateRun(IRun run) {
136      if (!suppressUpdates) {
137        if (runToDataPointMapping.ContainsKey(run)) {
138          foreach (DataPoint point in runToDataPointMapping[run]) {
139            point.Color = run.Color;
140            if (!run.Visible) {
141              this.chart.Series[0].Points.Remove(point);
142              UpdateCursorInterval();
143              chart.ChartAreas[0].RecalculateAxesScale();
144            }
145          }
146          if (!run.Visible) runToDataPointMapping.Remove(run);
147        } else {
148          AddDataPoint(run);
149          UpdateCursorInterval();
150          chart.ChartAreas[0].RecalculateAxesScale();
151        }
152
153        if (this.chart.Series[0].Points.Count == 0)
154          noRunsLabel.Visible = true;
155        else
156          noRunsLabel.Visible = false;
157      }
158    }
159
160    protected override void OnContentChanged() {
161      base.OnContentChanged();
162      this.categoricalMapping.Clear();
163      UpdateComboBoxes();
164      UpdateDataPoints();
165    }
166    private void Content_ColumnNamesChanged(object sender, EventArgs e) {
167      if (InvokeRequired)
168        Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
169      else
170        UpdateComboBoxes();
171    }
172
173    private void UpdateComboBoxes() {
174      string selectedXAxis = (string)this.xAxisComboBox.SelectedItem;
175      string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
176      string selectedSizeAxis = (string)this.sizeComboBox.SelectedItem;
177      this.xAxisComboBox.Items.Clear();
178      this.yAxisComboBox.Items.Clear();
179      this.sizeComboBox.Items.Clear();
180      if (Content != null) {
181        string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
182        this.xAxisComboBox.Items.AddRange(additionalAxisDimension);
183        this.xAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
184        this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
185        this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
186        string[] additionalSizeDimension = Enum.GetNames(typeof(SizeDimension));
187        this.sizeComboBox.Items.AddRange(additionalSizeDimension);
188        this.sizeComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
189        this.sizeComboBox.SelectedItem = SizeDimension.Constant.ToString();
190
191        bool changed = false;
192        if (selectedXAxis != null && xAxisComboBox.Items.Contains(selectedXAxis)) {
193          xAxisComboBox.SelectedItem = selectedXAxis;
194          changed = true;
195        }
196        if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
197          yAxisComboBox.SelectedItem = selectedYAxis;
198          changed = true;
199        }
200        if (selectedSizeAxis != null && sizeComboBox.Items.Contains(selectedSizeAxis)) {
201          sizeComboBox.SelectedItem = selectedSizeAxis;
202          changed = true;
203        }
204        if (changed)
205          UpdateDataPoints();
206      }
207    }
208
209
210    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
211      if (InvokeRequired)
212        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
213      else {
214        suppressUpdates = Content.UpdateOfRunsInProgress;
215        if (!suppressUpdates) UpdateDataPoints();
216      }
217    }
218
219    private void Content_Reset(object sender, EventArgs e) {
220      if (InvokeRequired)
221        Invoke(new EventHandler(Content_Reset), sender, e);
222      else {
223        this.categoricalMapping.Clear();
224        UpdateDataPoints();
225      }
226    }
227
228    private void UpdateDataPoints() {
229      Series series = this.chart.Series[0];
230      series.Points.Clear();
231      runToDataPointMapping.Clear();
232
233      chart.ChartAreas[0].AxisX.IsMarginVisible = xAxisValue != AxisDimension.Index.ToString();
234      chart.ChartAreas[0].AxisY.IsMarginVisible = yAxisValue != AxisDimension.Index.ToString();
235
236      if (Content != null) {
237        foreach (IRun run in this.Content)
238          this.AddDataPoint(run);
239
240        if (this.chart.Series[0].Points.Count == 0)
241          noRunsLabel.Visible = true;
242        else {
243          noRunsLabel.Visible = false;
244          UpdateMarkerSizes();
245          UpdateCursorInterval();
246        }
247      }
248      var xAxis = chart.ChartAreas[0].AxisX;
249      var yAxis = chart.ChartAreas[0].AxisY;
250      xTrackBar.Value = 0;
251      yTrackBar.Value = 0;
252      SetAutomaticUpdateOfAxis(xAxis, true);
253      SetAutomaticUpdateOfAxis(yAxis, true);
254    }
255
256    private void UpdateMarkerSizes() {
257      double[] sizeValues = this.chart.Series[0].Points.Select(p => p.YValues[1]).ToArray();
258      double minSizeValue = sizeValues.Min();
259      double maxSizeValue = sizeValues.Max();
260
261      for (int i = 0; i < sizeValues.Length; i++) {
262        DataPoint point = this.chart.Series[0].Points[i];
263        double sizeRange = maxSizeValue - minSizeValue;
264        double relativeSize = (point.YValues[1] - minSizeValue);
265
266        if (sizeRange > double.Epsilon) relativeSize /= sizeRange;
267        else relativeSize = 1;
268
269        point.MarkerSize = (int)Math.Round((sizeTrackBar.Value - sizeTrackBar.Minimum) * relativeSize + sizeTrackBar.Minimum);
270      }
271    }
272
273    private void UpdateDataPointJitter() {
274      var xAxis = this.chart.ChartAreas[0].AxisX;
275      var yAxis = this.chart.ChartAreas[0].AxisY;
276      SetAutomaticUpdateOfAxis(xAxis, false);
277      SetAutomaticUpdateOfAxis(yAxis, false);
278
279      foreach (DataPoint point in chart.Series[0].Points) {
280        IRun run = (IRun)point.Tag;
281        double xValue = GetValue(run, xAxisValue).Value;
282        double yValue = GetValue(run, yAxisValue).Value;
283
284        if (!xJitterFactor.IsAlmost(0.0))
285          xValue += 0.1 * GetXJitter(run) * xJitterFactor * (xAxis.Maximum - xAxis.Minimum);
286        if (!yJitterFactor.IsAlmost(0.0))
287          yValue += 0.1 * GetYJitter(run) * yJitterFactor * (yAxis.Maximum - yAxis.Minimum);
288
289        point.XValue = xValue;
290        point.YValues[0] = yValue;
291      }
292    }
293
294    private void SetAutomaticUpdateOfAxis(Axis axis, bool enabled) {
295      if (enabled) {
296        axis.Maximum = double.NaN;
297        axis.Minimum = double.NaN;
298        axis.MajorGrid.Interval = double.NaN;
299        axis.MajorTickMark.Interval = double.NaN;
300        axis.LabelStyle.Interval = double.NaN;
301      } else {
302        axis.Minimum = axis.Minimum;
303        axis.Maximum = axis.Maximum;
304        axis.MajorGrid.Interval = axis.MajorGrid.Interval;
305        axis.MajorTickMark.Interval = axis.MajorTickMark.Interval;
306        axis.LabelStyle.Interval = axis.LabelStyle.Interval;
307      }
308
309    }
310
311    private void AddDataPoint(IRun run) {
312      double? xValue;
313      double? yValue;
314      double? sizeValue;
315      Series series = this.chart.Series[0];
316
317      xValue = GetValue(run, xAxisValue);
318      yValue = GetValue(run, yAxisValue);
319      sizeValue = GetValue(run, sizeAxisValue);
320
321      if (xValue.HasValue && yValue.HasValue && sizeValue.HasValue) {
322        xValue = xValue.Value;
323
324        yValue = yValue.Value;
325
326        if (run.Visible) {
327          DataPoint point = new DataPoint(xValue.Value, new double[] { yValue.Value, sizeValue.Value });
328          point.Tag = run;
329          point.Color = run.Color;
330          series.Points.Add(point);
331          if (!runToDataPointMapping.ContainsKey(run)) runToDataPointMapping.Add(run, new List<DataPoint>());
332          runToDataPointMapping[run].Add(point);
333        }
334      }
335    }
336    private double? GetValue(IRun run, string columnName) {
337      if (run == null || string.IsNullOrEmpty(columnName))
338        return null;
339
340      if (Enum.IsDefined(typeof(AxisDimension), columnName)) {
341        AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
342        return GetValue(run, axisDimension);
343      } else if (Enum.IsDefined(typeof(SizeDimension), columnName)) {
344        SizeDimension sizeDimension = (SizeDimension)Enum.Parse(typeof(SizeDimension), columnName);
345        return GetValue(run, sizeDimension);
346      } else {
347        int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
348        IItem value = Content.GetValue(run, columnIndex);
349        if (value == null)
350          return null;
351
352        DoubleValue doubleValue = value as DoubleValue;
353        IntValue intValue = value as IntValue;
354        TimeSpanValue timeSpanValue = value as TimeSpanValue;
355        double? ret = null;
356        if (doubleValue != null) {
357          if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
358            ret = doubleValue.Value;
359        } else if (intValue != null)
360          ret = intValue.Value;
361        else if (timeSpanValue != null) {
362          ret = timeSpanValue.Value.TotalSeconds;
363        } else
364          ret = GetCategoricalValue(columnIndex, value.ToString());
365
366        return ret;
367      }
368    }
369    private double GetCategoricalValue(int dimension, string value) {
370      if (!this.categoricalMapping.ContainsKey(dimension))
371        this.categoricalMapping[dimension] = new Dictionary<object, double>();
372      if (!this.categoricalMapping[dimension].ContainsKey(value)) {
373        if (this.categoricalMapping[dimension].Values.Count == 0)
374          this.categoricalMapping[dimension][value] = 1.0;
375        else
376          this.categoricalMapping[dimension][value] = this.categoricalMapping[dimension].Values.Max() + 1.0;
377      }
378      return this.categoricalMapping[dimension][value];
379    }
380    private double GetValue(IRun run, AxisDimension axisDimension) {
381      double value = double.NaN;
382      switch (axisDimension) {
383        case AxisDimension.Index: {
384            value = Content.ToList().IndexOf(run);
385            break;
386          }
387        default: {
388            throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
389          }
390      }
391      return value;
392    }
393    private double GetValue(IRun run, SizeDimension sizeDimension) {
394      double value = double.NaN;
395      switch (sizeDimension) {
396        case SizeDimension.Constant: {
397            value = 2;
398            break;
399          }
400        default: {
401            throw new ArgumentException("No handling strategy for " + sizeDimension.ToString() + " is defined.");
402          }
403      }
404      return value;
405    }
406    private void UpdateCursorInterval() {
407      Series series = chart.Series[0];
408      double[] xValues = (from point in series.Points
409                          where !point.IsEmpty
410                          select point.XValue)
411                    .DefaultIfEmpty(1.0)
412                    .ToArray();
413      double[] yValues = (from point in series.Points
414                          where !point.IsEmpty
415                          select point.YValues[0])
416                    .DefaultIfEmpty(1.0)
417                    .ToArray();
418
419      double xRange = xValues.Max() - xValues.Min();
420      double yRange = yValues.Max() - yValues.Min();
421      if (xRange.IsAlmost(0.0)) xRange = 1.0;
422      if (yRange.IsAlmost(0.0)) yRange = 1.0;
423      double xDigits = (int)Math.Log10(xRange) - 3;
424      double yDigits = (int)Math.Log10(yRange) - 3;
425      double xZoomInterval = Math.Pow(10, xDigits);
426      double yZoomInterval = Math.Pow(10, yDigits);
427      this.chart.ChartAreas[0].CursorX.Interval = xZoomInterval;
428      this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
429
430      //code to handle TimeSpanValues correct
431      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
432      int columnIndex = xAxisComboBox.SelectedIndex - axisDimensionCount;
433      if (columnIndex >= 0 && Content.GetValue(0, columnIndex) is TimeSpanValue)
434        this.chart.ChartAreas[0].CursorX.Interval = 1;
435      columnIndex = yAxisComboBox.SelectedIndex - axisDimensionCount;
436      if (columnIndex >= 0 && Content.GetValue(0, columnIndex) is TimeSpanValue)
437        this.chart.ChartAreas[0].CursorY.Interval = 1;
438    }
439
440    #region Drag & drop and tooltip
441    private void chart_MouseDoubleClick(object sender, MouseEventArgs e) {
442      HitTestResult h = this.chart.HitTest(e.X, e.Y, ChartElementType.DataPoint);
443      if (h.ChartElementType == ChartElementType.DataPoint) {
444        IRun run = (IRun)((DataPoint)h.Object).Tag;
445        IContentView view = MainFormManager.MainForm.ShowContent(run);
446        if (view != null) {
447          view.ReadOnly = this.ReadOnly;
448          view.Locked = this.Locked;
449        }
450
451        this.chart.ChartAreas[0].CursorX.SelectionStart = this.chart.ChartAreas[0].CursorX.SelectionEnd;
452        this.chart.ChartAreas[0].CursorY.SelectionStart = this.chart.ChartAreas[0].CursorY.SelectionEnd;
453      }
454      UpdateAxisLabels();
455    }
456
457    private void chart_MouseUp(object sender, MouseEventArgs e) {
458      if (isSelecting) {
459        System.Windows.Forms.DataVisualization.Charting.Cursor xCursor = chart.ChartAreas[0].CursorX;
460        System.Windows.Forms.DataVisualization.Charting.Cursor yCursor = chart.ChartAreas[0].CursorY;
461
462        double minX = Math.Min(xCursor.SelectionStart, xCursor.SelectionEnd);
463        double maxX = Math.Max(xCursor.SelectionStart, xCursor.SelectionEnd);
464        double minY = Math.Min(yCursor.SelectionStart, yCursor.SelectionEnd);
465        double maxY = Math.Max(yCursor.SelectionStart, yCursor.SelectionEnd);
466
467        //check for click to select model
468        if (minX == maxX && minY == maxY) {
469          HitTestResult hitTest = chart.HitTest(e.X, e.Y);
470          if (hitTest.ChartElementType == ChartElementType.DataPoint) {
471            int pointIndex = hitTest.PointIndex;
472            IRun run = (IRun)this.chart.Series[0].Points[pointIndex].Tag;
473            run.Color = colorDialog.Color;
474          }
475        } else {
476          List<DataPoint> selectedPoints = new List<DataPoint>();
477          foreach (DataPoint p in this.chart.Series[0].Points) {
478            if (p.XValue >= minX && p.XValue < maxX &&
479              p.YValues[0] >= minY && p.YValues[0] < maxY) {
480              selectedPoints.Add(p);
481            }
482          }
483          foreach (DataPoint p in selectedPoints) {
484            IRun run = (IRun)p.Tag;
485            run.Color = colorDialog.Color;
486          }
487        }
488        this.chart.ChartAreas[0].CursorX.SelectionStart = this.chart.ChartAreas[0].CursorX.SelectionEnd;
489        this.chart.ChartAreas[0].CursorY.SelectionStart = this.chart.ChartAreas[0].CursorY.SelectionEnd;
490      }
491    }
492
493    private void chart_MouseMove(object sender, MouseEventArgs e) {
494      HitTestResult h = this.chart.HitTest(e.X, e.Y);
495      string newTooltipText = string.Empty;
496      string oldTooltipText;
497      if (h.ChartElementType == ChartElementType.DataPoint) {
498        IRun run = (IRun)((DataPoint)h.Object).Tag;
499        newTooltipText = BuildTooltip(run);
500      } else if (h.ChartElementType == ChartElementType.AxisLabels) {
501        newTooltipText = ((CustomLabel)h.Object).ToolTip;
502      }
503
504      oldTooltipText = this.tooltip.GetToolTip(chart);
505      if (newTooltipText != oldTooltipText)
506        this.tooltip.SetToolTip(chart, newTooltipText);
507    }
508
509    private string BuildTooltip(IRun run) {
510      string tooltip;
511      tooltip = run.Name + System.Environment.NewLine;
512
513      double? xValue = this.GetValue(run, (string)xAxisComboBox.SelectedItem);
514      double? yValue = this.GetValue(run, (string)yAxisComboBox.SelectedItem);
515      double? sizeValue = this.GetValue(run, (string)sizeComboBox.SelectedItem);
516
517      string xString = xValue == null ? string.Empty : xValue.Value.ToString();
518      string yString = yValue == null ? string.Empty : yValue.Value.ToString();
519      string sizeString = sizeValue == null ? string.Empty : sizeValue.Value.ToString();
520
521      //code to handle TimeSpanValues correct
522      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
523      int columnIndex = xAxisComboBox.SelectedIndex - axisDimensionCount;
524      if (xValue.HasValue && columnIndex > 0 && Content.GetValue(0, columnIndex) is TimeSpanValue) {
525        TimeSpan time = TimeSpan.FromSeconds(xValue.Value);
526        xString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.Seconds);
527      }
528      columnIndex = yAxisComboBox.SelectedIndex - axisDimensionCount;
529      if (yValue.HasValue && columnIndex > 0 && Content.GetValue(0, columnIndex) is TimeSpanValue) {
530        TimeSpan time = TimeSpan.FromSeconds(yValue.Value);
531        yString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.Seconds);
532      }
533
534      tooltip += xAxisComboBox.SelectedItem + " : " + xString + Environment.NewLine;
535      tooltip += yAxisComboBox.SelectedItem + " : " + yString + Environment.NewLine;
536      tooltip += sizeComboBox.SelectedItem + " : " + sizeString + Environment.NewLine;
537
538      return tooltip;
539    }
540    #endregion
541
542    #region GUI events and updating
543    private double GetXJitter(IRun run) {
544      if (!this.xJitter.ContainsKey(run))
545        this.xJitter[run] = random.NextDouble() * 2.0 - 1.0;
546      return this.xJitter[run];
547    }
548    private double GetYJitter(IRun run) {
549      if (!this.yJitter.ContainsKey(run))
550        this.yJitter[run] = random.NextDouble() * 2.0 - 1.0;
551      return this.yJitter[run];
552    }
553    private void jitterTrackBar_ValueChanged(object sender, EventArgs e) {
554      this.xJitterFactor = xTrackBar.Value / 100.0;
555      this.yJitterFactor = yTrackBar.Value / 100.0;
556      UpdateDataPointJitter();
557    }
558    private void sizeTrackBar_ValueChanged(object sender, EventArgs e) {
559      UpdateMarkerSizes();
560    }
561
562    private void AxisComboBox_SelectedValueChanged(object sender, EventArgs e) {
563      bool axisSelected = xAxisComboBox.SelectedIndex != -1 && yAxisComboBox.SelectedIndex != -1;
564      xTrackBar.Enabled = yTrackBar.Enabled = axisSelected;
565      colorXAxisButton.Enabled = colorYAxisButton.Enabled = axisSelected;
566
567      xAxisValue = (string)xAxisComboBox.SelectedItem;
568      yAxisValue = (string)yAxisComboBox.SelectedItem;
569      sizeAxisValue = (string)sizeComboBox.SelectedItem;
570
571      UpdateDataPoints();
572      UpdateAxisLabels();
573    }
574    private void UpdateAxisLabels() {
575      Axis xAxis = this.chart.ChartAreas[0].AxisX;
576      Axis yAxis = this.chart.ChartAreas[0].AxisY;
577      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
578      SetCustomAxisLabels(xAxis, xAxisComboBox.SelectedIndex - axisDimensionCount);
579      SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
580      if (xAxisComboBox.SelectedItem != null)
581        xAxis.Title = xAxisComboBox.SelectedItem.ToString();
582      if (yAxisComboBox.SelectedItem != null)
583        yAxis.Title = yAxisComboBox.SelectedItem.ToString();
584    }
585
586    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
587      this.UpdateAxisLabels();
588    }
589
590    private void SetCustomAxisLabels(Axis axis, int dimension) {
591      axis.CustomLabels.Clear();
592      if (categoricalMapping.ContainsKey(dimension)) {
593        foreach (var pair in categoricalMapping[dimension]) {
594          string labelText = pair.Key.ToString();
595          CustomLabel label = new CustomLabel();
596          label.ToolTip = labelText;
597          if (labelText.Length > 25)
598            labelText = labelText.Substring(0, 25) + " ... ";
599          label.Text = labelText;
600          label.GridTicks = GridTickTypes.TickMark;
601          label.FromPosition = pair.Value - 0.5;
602          label.ToPosition = pair.Value + 0.5;
603          axis.CustomLabels.Add(label);
604        }
605      } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
606        this.chart.ChartAreas[0].RecalculateAxesScale();
607        for (double i = axis.Minimum; i <= axis.Maximum; i += axis.LabelStyle.Interval) {
608          TimeSpan time = TimeSpan.FromSeconds(i);
609          string x = string.Format("{0:00}:{1:00}:{2:00}", time.Hours, time.Minutes, time.Seconds);
610          axis.CustomLabels.Add(i - axis.LabelStyle.Interval / 2, i + axis.LabelStyle.Interval / 2, x);
611        }
612      }
613    }
614
615    private void zoomButton_CheckedChanged(object sender, EventArgs e) {
616      this.isSelecting = selectButton.Checked;
617      this.colorButton.Enabled = this.isSelecting;
618      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !isSelecting;
619      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !isSelecting;
620    }
621    private void colorButton_Click(object sender, EventArgs e) {
622      if (colorDialog.ShowDialog(this) == DialogResult.OK) {
623        this.colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
624      }
625    }
626    private Image GenerateImage(int width, int height, Color fillColor) {
627      Image colorImage = new Bitmap(width, height);
628      using (Graphics gfx = Graphics.FromImage(colorImage)) {
629        using (SolidBrush brush = new SolidBrush(fillColor)) {
630          gfx.FillRectangle(brush, 0, 0, width, height);
631        }
632      }
633      return colorImage;
634    }
635
636    private IRun runToHide = null;
637    private void ContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
638      var pos = Control.MousePosition;
639      var chartPos = chart.PointToClient(pos);
640
641      HitTestResult h = this.chart.HitTest(chartPos.X, chartPos.Y);
642      if (h.ChartElementType == ChartElementType.DataPoint) {
643        runToHide = (IRun)((DataPoint)h.Object).Tag;
644        hideRunToolStripMenuItem.Visible = true;
645      } else {
646        runToHide = null;
647        hideRunToolStripMenuItem.Visible = false;
648      }
649
650    }
651    private void hideRunToolStripMenuItem_Click(object sender, EventArgs e) {
652      var constraint = Content.Constraints.OfType<RunCollectionContentConstraint>().Where(c => c.Active).FirstOrDefault();
653      if (constraint == null) {
654        constraint = new RunCollectionContentConstraint();
655        Content.Constraints.Add(constraint);
656        constraint.Active = true;
657      }
658      constraint.ConstraintData.Add(runToHide);
659    }
660
661    private void openBoxPlotViewToolStripMenuItem_Click(object sender, EventArgs e) {
662      RunCollectionBoxPlotView boxplotView = new RunCollectionBoxPlotView();
663      boxplotView.Content = this.Content;
664      boxplotView.xAxisComboBox.SelectedItem = xAxisComboBox.SelectedItem;
665      boxplotView.yAxisComboBox.SelectedItem = yAxisComboBox.SelectedItem;
666      boxplotView.Show();
667    }
668    #endregion
669
670    #region Automatic coloring
671    private void colorXAxisButton_Click(object sender, EventArgs e) {
672      ColorRuns(xAxisValue);
673    }
674
675    private void colorYAxisButton_Click(object sender, EventArgs e) {
676      ColorRuns(yAxisValue);
677    }
678
679    private void ColorRuns(string axisValue) {
680      var runs = Content.Select(r => new { Run = r, Value = GetValue(r, axisValue) }).Where(r => r.Value.HasValue);
681      double minValue = runs.Min(r => r.Value.Value);
682      double maxValue = runs.Max(r => r.Value.Value);
683      double range = maxValue - minValue;
684
685      foreach (var r in runs) {
686        int colorIndex = 0;
687        if (!range.IsAlmost(0)) colorIndex = (int)((ColorGradient.Colors.Count - 1) * (r.Value.Value - minValue) / (range));
688        r.Run.Color = ColorGradient.Colors[colorIndex];
689      }
690    }
691    #endregion
692  }
693}
Note: See TracBrowser for help on using the repository browser.