Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1413: Removed bug regarding the axis value change.

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