Free cookie consent management tool by TermsFeed Policy Generator

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

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

ticket #1056

  • Changed combobox events from SelectedIndexChanged to SelectedValueChanged
  • Changed series type from Bubbles to Points as this improves the performance of the chart as cneumuel suggested.
  • Added trackbar to change the maximum marker size.
File size: 27.5 KB
RevLine 
[3349]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.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();
[3499]229      if (Content != null) {
230        foreach (IRun run in this.Content)
231          this.AddDataPoint(run);
[3543]232
[5330]233        ////check to correct max bubble size
234        //if (this.chart.Series[0].Points.Select(p => p.YValues[1]).Distinct().Count() == 1)
235        //  this.chart.Series[0]["BubbleMaxSize"] = "2";
236        //else
237        //  this.chart.Series[0]["BubbleMaxSize"] = "7";
[3543]238
239        if (this.chart.Series[0].Points.Count == 0)
240          noRunsLabel.Visible = true;
[4209]241        else {
[3543]242          noRunsLabel.Visible = false;
[5330]243          UpdateMarkerSizes();
[4209]244          UpdateCursorInterval();
245        }
[3499]246      }
[3428]247    }
[5330]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      if (maxSizeValue - minSizeValue < double.Epsilon) return;
255
256      for (int i = 0; i < sizeValues.Length; i++) {
257        DataPoint point = this.chart.Series[0].Points[i];
258        double relativeSize = (point.YValues[1] - minSizeValue) / (maxSizeValue - minSizeValue);
259        point.MarkerSize = (int)Math.Round((sizeTrackBar.Value - sizeTrackBar.Minimum) * relativeSize + sizeTrackBar.Minimum);
260      }
261
262    }
263
[3428]264    private void AddDataPoint(IRun run) {
[3349]265      double? xValue;
266      double? yValue;
[3411]267      double? sizeValue;
[3428]268      Series series = this.chart.Series[0];
[3726]269
[4845]270      xValue = GetValue(run, xAxisValue);
271      yValue = GetValue(run, yAxisValue);
272      sizeValue = GetValue(run, sizeAxisValue);
[3726]273
[3543]274      if (xValue.HasValue && yValue.HasValue && sizeValue.HasValue) {
[3701]275        xValue = xValue.Value;
276        if (!xJitterFactor.IsAlmost(0.0))
277          xValue += 0.1 * GetXJitter(run) * xJitterFactor * (this.chart.ChartAreas[0].AxisX.Maximum - this.chart.ChartAreas[0].AxisX.Minimum);
278        yValue = yValue.Value;
279        if (!yJitterFactor.IsAlmost(0.0))
280          yValue += 0.1 * GetYJitter(run) * yJitterFactor * (this.chart.ChartAreas[0].AxisY.Maximum - this.chart.ChartAreas[0].AxisY.Minimum);
[3428]281        if (run.Visible) {
[3411]282          DataPoint point = new DataPoint(xValue.Value, new double[] { yValue.Value, sizeValue.Value });
[3428]283          point.Tag = run;
284          point.Color = run.Color;
[3411]285          series.Points.Add(point);
[4883]286
287          if (!runToDataPointMapping.ContainsKey(run)) runToDataPointMapping.Add(run, new List<DataPoint>());
288          runToDataPointMapping[run].Add(point);
[3411]289        }
[3349]290      }
291    }
[3524]292    private double? GetValue(IRun run, string columnName) {
293      if (run == null || string.IsNullOrEmpty(columnName))
[3349]294        return null;
295
[3524]296      if (Enum.IsDefined(typeof(AxisDimension), columnName)) {
297        AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
298        return GetValue(run, axisDimension);
299      } else if (Enum.IsDefined(typeof(SizeDimension), columnName)) {
300        SizeDimension sizeDimension = (SizeDimension)Enum.Parse(typeof(SizeDimension), columnName);
301        return GetValue(run, sizeDimension);
302      } else {
303        int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
304        IItem value = Content.GetValue(run, columnIndex);
305        if (value == null)
306          return null;
[3447]307
[3524]308        DoubleValue doubleValue = value as DoubleValue;
309        IntValue intValue = value as IntValue;
[4049]310        TimeSpanValue timeSpanValue = value as TimeSpanValue;
[3543]311        double? ret = null;
312        if (doubleValue != null) {
313          if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
314            ret = doubleValue.Value;
315        } else if (intValue != null)
[3524]316          ret = intValue.Value;
[4049]317        else if (timeSpanValue != null) {
318          ret = timeSpanValue.Value.TotalSeconds;
319        } else
[3524]320          ret = GetCategoricalValue(columnIndex, value.ToString());
[3447]321
[3524]322        return ret;
323      }
[3349]324    }
[3524]325    private double GetCategoricalValue(int dimension, string value) {
[3349]326      if (!this.categoricalMapping.ContainsKey(dimension))
327        this.categoricalMapping[dimension] = new Dictionary<object, double>();
[3524]328      if (!this.categoricalMapping[dimension].ContainsKey(value)) {
[3349]329        if (this.categoricalMapping[dimension].Values.Count == 0)
[3524]330          this.categoricalMapping[dimension][value] = 1.0;
[3349]331        else
[3524]332          this.categoricalMapping[dimension][value] = this.categoricalMapping[dimension].Values.Max() + 1.0;
[3349]333      }
[3524]334      return this.categoricalMapping[dimension][value];
[3349]335    }
[3524]336    private double GetValue(IRun run, AxisDimension axisDimension) {
337      double value = double.NaN;
338      switch (axisDimension) {
339        case AxisDimension.Index: {
340            value = Content.ToList().IndexOf(run);
341            break;
342          }
343        default: {
344            throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
345          }
346      }
347      return value;
348    }
349    private double GetValue(IRun run, SizeDimension sizeDimension) {
350      double value = double.NaN;
351      switch (sizeDimension) {
352        case SizeDimension.Constant: {
353            value = 2;
354            break;
355          }
356        default: {
357            throw new ArgumentException("No handling strategy for " + sizeDimension.ToString() + " is defined.");
358          }
359      }
360      return value;
361    }
[3707]362    private void UpdateCursorInterval() {
363      Series series = chart.Series[0];
364      double[] xValues = (from point in series.Points
365                          where !point.IsEmpty
366                          select point.XValue)
367                    .DefaultIfEmpty(1.0)
368                    .ToArray();
369      double[] yValues = (from point in series.Points
370                          where !point.IsEmpty
371                          select point.YValues[0])
372                    .DefaultIfEmpty(1.0)
373                    .ToArray();
[3349]374
[3707]375      double xRange = xValues.Max() - xValues.Min();
376      double yRange = yValues.Max() - yValues.Min();
[4049]377      if (xRange.IsAlmost(0.0)) xRange = 1.0;
378      if (yRange.IsAlmost(0.0)) yRange = 1.0;
[3707]379      double xDigits = (int)Math.Log10(xRange) - 3;
380      double yDigits = (int)Math.Log10(yRange) - 3;
381      double xZoomInterval = Math.Pow(10, xDigits);
382      double yZoomInterval = Math.Pow(10, yDigits);
383      this.chart.ChartAreas[0].CursorX.Interval = xZoomInterval;
384      this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
[4049]385
386      //code to handle TimeSpanValues correct
387      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
388      int columnIndex = xAxisComboBox.SelectedIndex - axisDimensionCount;
389      if (columnIndex >= 0 && Content.GetValue(0, columnIndex) is TimeSpanValue)
390        this.chart.ChartAreas[0].CursorX.Interval = 1;
391      columnIndex = yAxisComboBox.SelectedIndex - axisDimensionCount;
392      if (columnIndex >= 0 && Content.GetValue(0, columnIndex) is TimeSpanValue)
393        this.chart.ChartAreas[0].CursorY.Interval = 1;
[3707]394    }
395
[4888]396    #region Drag & drop and tooltip
[3411]397    private IRun draggedRun;
398    private void chart_MouseDown(object sender, MouseEventArgs e) {
399      HitTestResult h = this.chart.HitTest(e.X, e.Y);
400      if (h.ChartElementType == ChartElementType.DataPoint) {
[3459]401        IRun run = (IRun)((DataPoint)h.Object).Tag;
[3428]402        if (e.Clicks >= 2) {
[3557]403          IContentView view = MainFormManager.MainForm.ShowContent(run);
404          if (view != null) {
405            view.ReadOnly = this.ReadOnly;
406            view.Locked = this.Locked;
407          }
[3546]408        } else
[3428]409          this.draggedRun = run;
[3546]410        this.chart.ChartAreas[0].CursorX.SetSelectionPosition(double.NaN, double.NaN);
411        this.chart.ChartAreas[0].CursorY.SetSelectionPosition(double.NaN, double.NaN);
[3411]412      }
413    }
414
[3428]415    private void chart_MouseUp(object sender, MouseEventArgs e) {
[3459]416      if (isSelecting) {
[4888]417        Content.UpdateOfRunsInProgress = true;
[3459]418        System.Windows.Forms.DataVisualization.Charting.Cursor xCursor = chart.ChartAreas[0].CursorX;
419        System.Windows.Forms.DataVisualization.Charting.Cursor yCursor = chart.ChartAreas[0].CursorY;
[3428]420
[3459]421        double minX = Math.Min(xCursor.SelectionStart, xCursor.SelectionEnd);
422        double maxX = Math.Max(xCursor.SelectionStart, xCursor.SelectionEnd);
423        double minY = Math.Min(yCursor.SelectionStart, yCursor.SelectionEnd);
424        double maxY = Math.Max(yCursor.SelectionStart, yCursor.SelectionEnd);
[3428]425
[3459]426        //check for click to select model
427        if (minX == maxX && minY == maxY) {
428          HitTestResult hitTest = chart.HitTest(e.X, e.Y);
429          if (hitTest.ChartElementType == ChartElementType.DataPoint) {
430            int pointIndex = hitTest.PointIndex;
431            IRun run = (IRun)this.chart.Series[0].Points[pointIndex].Tag;
432            run.Color = colorDialog.Color;
433          }
434        } else {
435          List<DataPoint> selectedPoints = new List<DataPoint>();
436          foreach (DataPoint p in this.chart.Series[0].Points) {
437            if (p.XValue >= minX && p.XValue < maxX &&
438              p.YValues[0] >= minY && p.YValues[0] < maxY) {
439              selectedPoints.Add(p);
440            }
441          }
442          foreach (DataPoint p in selectedPoints) {
443            IRun run = (IRun)p.Tag;
444            run.Color = colorDialog.Color;
445          }
[3428]446        }
[3638]447        this.chart.ChartAreas[0].CursorX.SelectionStart = this.chart.ChartAreas[0].CursorX.SelectionEnd;
448        this.chart.ChartAreas[0].CursorY.SelectionStart = this.chart.ChartAreas[0].CursorY.SelectionEnd;
[4888]449        Content.UpdateOfRunsInProgress = false;
[3428]450      }
451    }
452
[3411]453    private void chart_MouseMove(object sender, MouseEventArgs e) {
[3487]454      HitTestResult h = this.chart.HitTest(e.X, e.Y);
[3432]455      if (!Locked) {
456        if (this.draggedRun != null && h.ChartElementType != ChartElementType.DataPoint) {
457          DataObject data = new DataObject();
458          data.SetData("Type", draggedRun.GetType());
459          data.SetData("Value", draggedRun);
[3459]460          if (ReadOnly)
[3432]461            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
[3459]462          else {
[3432]463            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
464            if ((result & DragDropEffects.Move) == DragDropEffects.Move)
465              Content.Remove(draggedRun);
466          }
[3459]467          this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !isSelecting;
468          this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !isSelecting;
[3432]469          this.draggedRun = null;
[3411]470        }
471      }
[4212]472
[3524]473      string newTooltipText = string.Empty;
[3487]474      string oldTooltipText;
475      if (h.ChartElementType == ChartElementType.DataPoint) {
476        IRun run = (IRun)((DataPoint)h.Object).Tag;
[3524]477        newTooltipText = BuildTooltip(run);
[4212]478      } else if (h.ChartElementType == ChartElementType.AxisLabels) {
479        newTooltipText = ((CustomLabel)h.Object).ToolTip;
[3524]480      }
481
[3487]482      oldTooltipText = this.tooltip.GetToolTip(chart);
483      if (newTooltipText != oldTooltipText)
484        this.tooltip.SetToolTip(chart, newTooltipText);
[3411]485    }
[3524]486
487    private string BuildTooltip(IRun run) {
488      string tooltip;
489      tooltip = run.Name + System.Environment.NewLine;
490
491      double? xValue = this.GetValue(run, (string)xAxisComboBox.SelectedItem);
492      double? yValue = this.GetValue(run, (string)yAxisComboBox.SelectedItem);
493      double? sizeValue = this.GetValue(run, (string)sizeComboBox.SelectedItem);
494
495      string xString = xValue == null ? string.Empty : xValue.Value.ToString();
496      string yString = yValue == null ? string.Empty : yValue.Value.ToString();
497      string sizeString = sizeValue == null ? string.Empty : sizeValue.Value.ToString();
498
[4049]499      //code to handle TimeSpanValues correct
500      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
501      int columnIndex = xAxisComboBox.SelectedIndex - axisDimensionCount;
502      if (xValue.HasValue && columnIndex > 0 && Content.GetValue(0, columnIndex) is TimeSpanValue) {
503        TimeSpan time = TimeSpan.FromSeconds(xValue.Value);
[4212]504        xString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.Seconds);
[4049]505      }
506      columnIndex = yAxisComboBox.SelectedIndex - axisDimensionCount;
507      if (yValue.HasValue && columnIndex > 0 && Content.GetValue(0, columnIndex) is TimeSpanValue) {
508        TimeSpan time = TimeSpan.FromSeconds(yValue.Value);
[4212]509        yString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.Seconds);
[4049]510      }
511
[3524]512      tooltip += xAxisComboBox.SelectedItem + " : " + xString + Environment.NewLine;
513      tooltip += yAxisComboBox.SelectedItem + " : " + yString + Environment.NewLine;
514      tooltip += sizeComboBox.SelectedItem + " : " + sizeString + Environment.NewLine;
515
516      return tooltip;
517    }
[3411]518    #endregion
519
520    #region GUI events and updating
521    private double GetXJitter(IRun run) {
522      if (!this.xJitter.ContainsKey(run))
523        this.xJitter[run] = random.NextDouble() * 2.0 - 1.0;
524      return this.xJitter[run];
525    }
526    private double GetYJitter(IRun run) {
527      if (!this.yJitter.ContainsKey(run))
528        this.yJitter[run] = random.NextDouble() * 2.0 - 1.0;
529      return this.yJitter[run];
530    }
531    private void jitterTrackBar_ValueChanged(object sender, EventArgs e) {
532      this.xJitterFactor = xTrackBar.Value / 100.0;
533      this.yJitterFactor = yTrackBar.Value / 100.0;
534      this.UpdateDataPoints();
535    }
[5330]536    private void sizeTrackBar_ValueChanged(object sender, EventArgs e) {
537      UpdateMarkerSizes();
538    }
[3411]539
[5330]540    private void AxisComboBox_SelectedValueChanged(object sender, EventArgs e) {
[4812]541      bool axisSelected = xAxisComboBox.SelectedIndex != -1 && yAxisComboBox.SelectedIndex != -1;
542      xTrackBar.Enabled = yTrackBar.Enabled = axisSelected;
543      colorXAxisButton.Enabled = colorYAxisButton.Enabled = axisSelected;
[4845]544
545      if (!xAxisComboBox.DroppedDown)
546        xAxisValue = (string)xAxisComboBox.SelectedItem;
547      if (!yAxisComboBox.DroppedDown)
548        yAxisValue = (string)yAxisComboBox.SelectedItem;
549      if (!sizeComboBox.DroppedDown)
550        sizeAxisValue = (string)sizeComboBox.SelectedItem;
551
[3411]552      UpdateDataPoints();
553      UpdateAxisLabels();
554    }
[3349]555    private void UpdateAxisLabels() {
556      Axis xAxis = this.chart.ChartAreas[0].AxisX;
557      Axis yAxis = this.chart.ChartAreas[0].AxisY;
[3536]558      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
559      SetCustomAxisLabels(xAxis, xAxisComboBox.SelectedIndex - axisDimensionCount);
560      SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
[4635]561      if (xAxisComboBox.SelectedItem != null)
562        xAxis.Title = xAxisComboBox.SelectedItem.ToString();
563      if (yAxisComboBox.SelectedItem != null)
564        yAxis.Title = yAxisComboBox.SelectedItem.ToString();
[3349]565    }
[4049]566
567    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
568      this.UpdateAxisLabels();
569    }
570
[3411]571    private void SetCustomAxisLabels(Axis axis, int dimension) {
572      axis.CustomLabels.Clear();
[3349]573      if (categoricalMapping.ContainsKey(dimension)) {
574        foreach (var pair in categoricalMapping[dimension]) {
[3536]575          string labelText = pair.Key.ToString();
[4212]576          CustomLabel label = new CustomLabel();
577          label.ToolTip = labelText;
[3543]578          if (labelText.Length > 25)
579            labelText = labelText.Substring(0, 25) + " ... ";
[4212]580          label.Text = labelText;
[3349]581          label.GridTicks = GridTickTypes.TickMark;
[4212]582          label.FromPosition = pair.Value - 0.5;
583          label.ToPosition = pair.Value + 0.5;
584          axis.CustomLabels.Add(label);
[3349]585        }
[4049]586      } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
587        this.chart.ChartAreas[0].RecalculateAxesScale();
588        for (double i = axis.Minimum; i <= axis.Maximum; i += axis.LabelStyle.Interval) {
589          TimeSpan time = TimeSpan.FromSeconds(i);
[4058]590          string x = string.Format("{0:00}:{1:00}:{2:00}", (int)time.Hours, time.Minutes, time.Seconds);
591          axis.CustomLabels.Add(i - axis.LabelStyle.Interval / 2, i + axis.LabelStyle.Interval / 2, x);
[4049]592        }
[3349]593      }
594    }
[3411]595
596    private void zoomButton_CheckedChanged(object sender, EventArgs e) {
597      this.isSelecting = selectButton.Checked;
598      this.colorButton.Enabled = this.isSelecting;
599      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !isSelecting;
600      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !isSelecting;
601    }
602    private void colorButton_Click(object sender, EventArgs e) {
603      if (colorDialog.ShowDialog(this) == DialogResult.OK) {
604        this.colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
605      }
606    }
607    private Image GenerateImage(int width, int height, Color fillColor) {
608      Image colorImage = new Bitmap(width, height);
609      using (Graphics gfx = Graphics.FromImage(colorImage)) {
610        using (SolidBrush brush = new SolidBrush(fillColor)) {
611          gfx.FillRectangle(brush, 0, 0, width, height);
612        }
613      }
614      return colorImage;
615    }
[4653]616
617    private void openBoxPlotViewToolStripMenuItem_Click(object sender, EventArgs e) {
618      RunCollectionBoxPlotView boxplotView = new RunCollectionBoxPlotView();
619      boxplotView.Content = this.Content;
620      boxplotView.xAxisComboBox.SelectedItem = xAxisComboBox.SelectedItem;
621      boxplotView.yAxisComboBox.SelectedItem = yAxisComboBox.SelectedItem;
622      boxplotView.Show();
623    }
[3411]624    #endregion
[4799]625
[4888]626    #region Automatic coloring
[4799]627    private void colorXAxisButton_Click(object sender, EventArgs e) {
[4845]628      ColorRuns(xAxisValue);
[4799]629    }
630
631    private void colorYAxisButton_Click(object sender, EventArgs e) {
[4845]632      ColorRuns(yAxisValue);
633    }
634
635    private void ColorRuns(string axisValue) {
[4888]636      Content.UpdateOfRunsInProgress = true;
[4845]637      var runs = Content.Select(r => new { Run = r, Value = GetValue(r, axisValue) }).Where(r => r.Value.HasValue);
638      double minValue = runs.Min(r => r.Value.Value);
639      double maxValue = runs.Max(r => r.Value.Value);
640      double range = maxValue - minValue;
641
642      foreach (var r in runs) {
643        int colorIndex = 0;
644        if (!range.IsAlmost(0)) colorIndex = (int)((ColorGradient.Colors.Count - 1) * (r.Value.Value - minValue) / (maxValue - minValue));
645        r.Run.Color = ColorGradient.Colors[colorIndex];
[4799]646      }
[4888]647      Content.UpdateOfRunsInProgress = false;
[4799]648    }
649    #endregion
[3349]650  }
651}
Note: See TracBrowser for help on using the repository browser.