Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6539 was 6096, checked in by mkommend, 13 years ago

#1413: Fixed remaining bugs in the bubble chart.

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