Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Optimization.Views/3.3/RunCollectionBubbleChartView.cs @ 12056

Last change on this file since 12056 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

File size: 28.8 KB
RevLine 
[3349]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3349]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;
[3376]28using HeuristicLab.Common;
[3349]29using HeuristicLab.Core;
[3447]30using HeuristicLab.Data;
[4068]31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
[3349]33
34namespace HeuristicLab.Optimization.Views {
35  [View("RunCollection BubbleChart")]
36  [Content(typeof(RunCollection), false)]
37  public partial class RunCollectionBubbleChartView : AsynchronousContentView {
[3524]38    private enum SizeDimension { Constant = 0 }
39    private enum AxisDimension { Index = 0 }
40
[3726]41    private string xAxisValue;
42    private string yAxisValue;
43    private string sizeAxisValue;
44
[4883]45    private Dictionary<IRun, List<DataPoint>> runToDataPointMapping;
[3349]46    private Dictionary<int, Dictionary<object, double>> categoricalMapping;
[3411]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;
[4883]53    private bool suppressUpdates = false;
[3349]54
55    public RunCollectionBubbleChartView() {
56      InitializeComponent();
[3411]57
[6760]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
[4883]62      runToDataPointMapping = new Dictionary<IRun, List<DataPoint>>();
[4635]63      categoricalMapping = new Dictionary<int, Dictionary<object, double>>();
64      xJitter = new Dictionary<IRun, double>();
65      yJitter = new Dictionary<IRun, double>();
66      random = new Random();
[4846]67
[4635]68      colorDialog.Color = Color.Black;
69      colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
70      isSelecting = false;
[4846]71
[4636]72      chart.CustomizeAllChartAreas();
[4635]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;
[3349]77    }
78
79    public new RunCollection Content {
80      get { return (RunCollection)base.Content; }
81      set { base.Content = value; }
82    }
[3447]83    public IStringConvertibleMatrix Matrix {
84      get { return this.Content; }
85    }
86
[3349]87    protected override void RegisterContentEvents() {
88      base.RegisterContentEvents();
89      Content.Reset += new EventHandler(Content_Reset);
90      Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
[3428]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);
[4888]94      Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
[3448]95      RegisterRunEvents(Content);
96    }
[3349]97    protected override void DeregisterContentEvents() {
98      base.DeregisterContentEvents();
99      Content.Reset -= new EventHandler(Content_Reset);
100      Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
[3428]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);
[4888]104      Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
[3448]105      DeregisterRunEvents(Content);
106    }
[4094]107    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
108      foreach (IRun run in runs)
109        run.Changed += new EventHandler(run_Changed);
110    }
[3448]111    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
112      foreach (IRun run in runs)
[3428]113        run.Changed -= new EventHandler(run_Changed);
[3349]114    }
[3428]115
116    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[3448]117      DeregisterRunEvents(e.OldItems);
118      RegisterRunEvents(e.Items);
[3428]119    }
120    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[3449]121      DeregisterRunEvents(e.Items);
[3428]122    }
123    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[3448]124      RegisterRunEvents(e.Items);
[3428]125    }
126    private void run_Changed(object sender, EventArgs e) {
[3632]127      if (InvokeRequired)
128        this.Invoke(new EventHandler(run_Changed), sender, e);
129      else {
130        IRun run = (IRun)sender;
131        UpdateRun(run);
132      }
[3614]133    }
134
135    private void UpdateRun(IRun run) {
[4883]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);
[4799]149          UpdateCursorInterval();
150          chart.ChartAreas[0].RecalculateAxesScale();
151        }
[4883]152
153        if (this.chart.Series[0].Points.Count == 0)
154          noRunsLabel.Visible = true;
155        else
156          noRunsLabel.Visible = false;
[4799]157      }
[3428]158    }
159
[3349]160    protected override void OnContentChanged() {
161      base.OnContentChanged();
[3411]162      this.categoricalMapping.Clear();
[3499]163      UpdateComboBoxes();
164      UpdateDataPoints();
[3349]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() {
[3701]174      string selectedXAxis = (string)this.xAxisComboBox.SelectedItem;
175      string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
176      string selectedSizeAxis = (string)this.sizeComboBox.SelectedItem;
[3349]177      this.xAxisComboBox.Items.Clear();
178      this.yAxisComboBox.Items.Clear();
[3411]179      this.sizeComboBox.Items.Clear();
[3499]180      if (Content != null) {
[3524]181        string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
182        this.xAxisComboBox.Items.AddRange(additionalAxisDimension);
[3499]183        this.xAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
[3524]184        this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
[3499]185        this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
[3524]186        string[] additionalSizeDimension = Enum.GetNames(typeof(SizeDimension));
187        this.sizeComboBox.Items.AddRange(additionalSizeDimension);
[3499]188        this.sizeComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
[3524]189        this.sizeComboBox.SelectedItem = SizeDimension.Constant.ToString();
[3701]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();
[3499]206      }
[3349]207    }
208
[4883]209
[4888]210    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
[4883]211      if (InvokeRequired)
[4888]212        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
[4883]213      else {
[4888]214        suppressUpdates = Content.UpdateOfRunsInProgress;
[4883]215        if (!suppressUpdates) UpdateDataPoints();
216      }
217    }
218
[3349]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();
[4799]231      runToDataPointMapping.Clear();
[6096]232
233      chart.ChartAreas[0].AxisX.IsMarginVisible = xAxisValue != AxisDimension.Index.ToString();
234      chart.ChartAreas[0].AxisY.IsMarginVisible = yAxisValue != AxisDimension.Index.ToString();
235
[3499]236      if (Content != null) {
237        foreach (IRun run in this.Content)
238          this.AddDataPoint(run);
[3543]239
240        if (this.chart.Series[0].Points.Count == 0)
241          noRunsLabel.Visible = true;
[4209]242        else {
[3543]243          noRunsLabel.Visible = false;
[5330]244          UpdateMarkerSizes();
[4209]245          UpdateCursorInterval();
246        }
[3499]247      }
[5824]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);
[3428]254    }
[5330]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];
[5348]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
[5330]269        point.MarkerSize = (int)Math.Round((sizeTrackBar.Value - sizeTrackBar.Minimum) * relativeSize + sizeTrackBar.Minimum);
270      }
271    }
272
[5824]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
[3428]311    private void AddDataPoint(IRun run) {
[3349]312      double? xValue;
313      double? yValue;
[3411]314      double? sizeValue;
[3428]315      Series series = this.chart.Series[0];
[3726]316
[4845]317      xValue = GetValue(run, xAxisValue);
318      yValue = GetValue(run, yAxisValue);
319      sizeValue = GetValue(run, sizeAxisValue);
[3726]320
[3543]321      if (xValue.HasValue && yValue.HasValue && sizeValue.HasValue) {
[3701]322        xValue = xValue.Value;
[5824]323
[3701]324        yValue = yValue.Value;
[5824]325
[3428]326        if (run.Visible) {
[3411]327          DataPoint point = new DataPoint(xValue.Value, new double[] { yValue.Value, sizeValue.Value });
[3428]328          point.Tag = run;
329          point.Color = run.Color;
[3411]330          series.Points.Add(point);
[4883]331          if (!runToDataPointMapping.ContainsKey(run)) runToDataPointMapping.Add(run, new List<DataPoint>());
332          runToDataPointMapping[run].Add(point);
[3411]333        }
[3349]334      }
335    }
[3524]336    private double? GetValue(IRun run, string columnName) {
337      if (run == null || string.IsNullOrEmpty(columnName))
[3349]338        return null;
339
[3524]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;
[3447]351
[3524]352        DoubleValue doubleValue = value as DoubleValue;
353        IntValue intValue = value as IntValue;
[4049]354        TimeSpanValue timeSpanValue = value as TimeSpanValue;
[3543]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)
[3524]360          ret = intValue.Value;
[4049]361        else if (timeSpanValue != null) {
362          ret = timeSpanValue.Value.TotalSeconds;
363        } else
[3524]364          ret = GetCategoricalValue(columnIndex, value.ToString());
[3447]365
[3524]366        return ret;
367      }
[3349]368    }
[3524]369    private double GetCategoricalValue(int dimension, string value) {
[3349]370      if (!this.categoricalMapping.ContainsKey(dimension))
371        this.categoricalMapping[dimension] = new Dictionary<object, double>();
[3524]372      if (!this.categoricalMapping[dimension].ContainsKey(value)) {
[3349]373        if (this.categoricalMapping[dimension].Values.Count == 0)
[3524]374          this.categoricalMapping[dimension][value] = 1.0;
[3349]375        else
[3524]376          this.categoricalMapping[dimension][value] = this.categoricalMapping[dimension].Values.Max() + 1.0;
[3349]377      }
[3524]378      return this.categoricalMapping[dimension][value];
[3349]379    }
[3524]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    }
[3707]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();
[3349]418
[3707]419      double xRange = xValues.Max() - xValues.Min();
420      double yRange = yValues.Max() - yValues.Min();
[4049]421      if (xRange.IsAlmost(0.0)) xRange = 1.0;
422      if (yRange.IsAlmost(0.0)) yRange = 1.0;
[3707]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;
[4049]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;
[3707]438    }
439
[4888]440    #region Drag & drop and tooltip
[6026]441    private void chart_MouseDoubleClick(object sender, MouseEventArgs e) {
[6094]442      HitTestResult h = this.chart.HitTest(e.X, e.Y, ChartElementType.DataPoint);
[3411]443      if (h.ChartElementType == ChartElementType.DataPoint) {
[3459]444        IRun run = (IRun)((DataPoint)h.Object).Tag;
[5976]445        IContentView view = MainFormManager.MainForm.ShowContent(run);
446        if (view != null) {
447          view.ReadOnly = this.ReadOnly;
448          view.Locked = this.Locked;
449        }
[6026]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;
[3411]453      }
[6096]454      UpdateAxisLabels();
[3411]455    }
456
[3428]457    private void chart_MouseUp(object sender, MouseEventArgs e) {
[3459]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;
[3428]461
[3459]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);
[3428]466
[3459]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          }
[3428]487        }
[3638]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;
[3428]490      }
491    }
492
[3411]493    private void chart_MouseMove(object sender, MouseEventArgs e) {
[3487]494      HitTestResult h = this.chart.HitTest(e.X, e.Y);
[3524]495      string newTooltipText = string.Empty;
[3487]496      string oldTooltipText;
497      if (h.ChartElementType == ChartElementType.DataPoint) {
498        IRun run = (IRun)((DataPoint)h.Object).Tag;
[3524]499        newTooltipText = BuildTooltip(run);
[4212]500      } else if (h.ChartElementType == ChartElementType.AxisLabels) {
501        newTooltipText = ((CustomLabel)h.Object).ToolTip;
[3524]502      }
503
[3487]504      oldTooltipText = this.tooltip.GetToolTip(chart);
505      if (newTooltipText != oldTooltipText)
506        this.tooltip.SetToolTip(chart, newTooltipText);
[3411]507    }
[3524]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
[4049]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);
[4212]526        xString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.Seconds);
[4049]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);
[4212]531        yString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.Seconds);
[4049]532      }
533
[3524]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    }
[3411]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;
[5824]556      UpdateDataPointJitter();
[3411]557    }
[5330]558    private void sizeTrackBar_ValueChanged(object sender, EventArgs e) {
559      UpdateMarkerSizes();
560    }
[3411]561
[5330]562    private void AxisComboBox_SelectedValueChanged(object sender, EventArgs e) {
[4812]563      bool axisSelected = xAxisComboBox.SelectedIndex != -1 && yAxisComboBox.SelectedIndex != -1;
564      xTrackBar.Enabled = yTrackBar.Enabled = axisSelected;
565      colorXAxisButton.Enabled = colorYAxisButton.Enabled = axisSelected;
[4845]566
[6094]567      xAxisValue = (string)xAxisComboBox.SelectedItem;
568      yAxisValue = (string)yAxisComboBox.SelectedItem;
569      sizeAxisValue = (string)sizeComboBox.SelectedItem;
[4845]570
[3411]571      UpdateDataPoints();
572      UpdateAxisLabels();
573    }
[3349]574    private void UpdateAxisLabels() {
575      Axis xAxis = this.chart.ChartAreas[0].AxisX;
576      Axis yAxis = this.chart.ChartAreas[0].AxisY;
[3536]577      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
578      SetCustomAxisLabels(xAxis, xAxisComboBox.SelectedIndex - axisDimensionCount);
579      SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
[4635]580      if (xAxisComboBox.SelectedItem != null)
581        xAxis.Title = xAxisComboBox.SelectedItem.ToString();
582      if (yAxisComboBox.SelectedItem != null)
583        yAxis.Title = yAxisComboBox.SelectedItem.ToString();
[3349]584    }
[4049]585
586    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
587      this.UpdateAxisLabels();
588    }
589
[3411]590    private void SetCustomAxisLabels(Axis axis, int dimension) {
591      axis.CustomLabels.Clear();
[3349]592      if (categoricalMapping.ContainsKey(dimension)) {
593        foreach (var pair in categoricalMapping[dimension]) {
[3536]594          string labelText = pair.Key.ToString();
[4212]595          CustomLabel label = new CustomLabel();
596          label.ToolTip = labelText;
[3543]597          if (labelText.Length > 25)
598            labelText = labelText.Substring(0, 25) + " ... ";
[4212]599          label.Text = labelText;
[3349]600          label.GridTicks = GridTickTypes.TickMark;
[4212]601          label.FromPosition = pair.Value - 0.5;
602          label.ToPosition = pair.Value + 0.5;
603          axis.CustomLabels.Add(label);
[3349]604        }
[4049]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);
[6096]609          string x = string.Format("{0:00}:{1:00}:{2:00}", time.Hours, time.Minutes, time.Seconds);
[4058]610          axis.CustomLabels.Add(i - axis.LabelStyle.Interval / 2, i + axis.LabelStyle.Interval / 2, x);
[4049]611        }
[3349]612      }
613    }
[3411]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    }
[4653]635
[6760]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
[4653]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    }
[3411]668    #endregion
[4799]669
[4888]670    #region Automatic coloring
[4799]671    private void colorXAxisButton_Click(object sender, EventArgs e) {
[4845]672      ColorRuns(xAxisValue);
[4799]673    }
674
675    private void colorYAxisButton_Click(object sender, EventArgs e) {
[4845]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;
[5976]687        if (!range.IsAlmost(0)) colorIndex = (int)((ColorGradient.Colors.Count - 1) * (r.Value.Value - minValue) / (range));
[4845]688        r.Run.Color = ColorGradient.Colors[colorIndex];
[4799]689      }
690    }
691    #endregion
[3349]692  }
693}
Note: See TracBrowser for help on using the repository browser.