Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added tooltips for customized axis labels in BubbleChartView (ticket #1135)

File size: 23.8 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
[3349]45    private Dictionary<int, Dictionary<object, double>> categoricalMapping;
[3411]46    private Dictionary<IRun, double> xJitter;
47    private Dictionary<IRun, double> yJitter;
48    private double xJitterFactor = 0.0;
49    private double yJitterFactor = 0.0;
50    private Random random;
51    private bool isSelecting = false;
[3349]52
53    public RunCollectionBubbleChartView() {
54      InitializeComponent();
[3411]55
[3349]56      this.categoricalMapping = new Dictionary<int, Dictionary<object, double>>();
[3411]57      this.xJitter = new Dictionary<IRun, double>();
58      this.yJitter = new Dictionary<IRun, double>();
59      this.random = new Random();
[3428]60      this.colorDialog.Color = Color.Black;
61      this.colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
62      this.isSelecting = false;
[3411]63
64      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
65      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
[3707]66      this.chart.ChartAreas[0].CursorX.Interval = 1;
67      this.chart.ChartAreas[0].CursorY.Interval = 1;
[3411]68      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !this.isSelecting;
69      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !this.isSelecting;
[3349]70    }
71
72    public new RunCollection Content {
73      get { return (RunCollection)base.Content; }
74      set { base.Content = value; }
75    }
[3447]76    public IStringConvertibleMatrix Matrix {
77      get { return this.Content; }
78    }
79
[3349]80    protected override void RegisterContentEvents() {
81      base.RegisterContentEvents();
82      Content.Reset += new EventHandler(Content_Reset);
83      Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
[3428]84      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
85      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
86      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[3448]87      RegisterRunEvents(Content);
88    }
[3349]89    protected override void DeregisterContentEvents() {
90      base.DeregisterContentEvents();
91      Content.Reset -= new EventHandler(Content_Reset);
92      Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
[3428]93      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
94      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
95      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[3448]96      DeregisterRunEvents(Content);
97    }
[4094]98    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
99      foreach (IRun run in runs)
100        run.Changed += new EventHandler(run_Changed);
101    }
[3448]102    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
103      foreach (IRun run in runs)
[3428]104        run.Changed -= new EventHandler(run_Changed);
[3349]105    }
[3428]106
107    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[3448]108      DeregisterRunEvents(e.OldItems);
109      RegisterRunEvents(e.Items);
[3428]110    }
111    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[3449]112      DeregisterRunEvents(e.Items);
[3428]113    }
114    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[3448]115      RegisterRunEvents(e.Items);
[3428]116    }
117    private void run_Changed(object sender, EventArgs e) {
[3632]118      if (InvokeRequired)
119        this.Invoke(new EventHandler(run_Changed), sender, e);
120      else {
121        IRun run = (IRun)sender;
122        UpdateRun(run);
123      }
[3614]124    }
125
126    private void UpdateRun(IRun run) {
[3428]127      DataPoint point = this.chart.Series[0].Points.Where(p => p.Tag == run).SingleOrDefault();
128      if (point != null) {
129        point.Color = run.Color;
130        if (!run.Visible)
131          this.chart.Series[0].Points.Remove(point);
132      } else
133        AddDataPoint(run);
[4049]134      UpdateCursorInterval();
[3614]135
[3707]136
[3614]137      if (this.chart.Series[0].Points.Count == 0)
138        noRunsLabel.Visible = true;
139      else
140        noRunsLabel.Visible = false;
[3428]141    }
142
[3349]143    protected override void OnContentChanged() {
144      base.OnContentChanged();
[3411]145      this.categoricalMapping.Clear();
[3499]146      UpdateComboBoxes();
147      UpdateDataPoints();
[3349]148    }
149    private void Content_ColumnNamesChanged(object sender, EventArgs e) {
150      if (InvokeRequired)
151        Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
152      else
153        UpdateComboBoxes();
154    }
155
156    private void UpdateComboBoxes() {
[3701]157      string selectedXAxis = (string)this.xAxisComboBox.SelectedItem;
158      string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
159      string selectedSizeAxis = (string)this.sizeComboBox.SelectedItem;
[3349]160      this.xAxisComboBox.Items.Clear();
161      this.yAxisComboBox.Items.Clear();
[3411]162      this.sizeComboBox.Items.Clear();
[3499]163      if (Content != null) {
[3524]164        string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
165        this.xAxisComboBox.Items.AddRange(additionalAxisDimension);
[3499]166        this.xAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
[3524]167        this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
[3499]168        this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
[3524]169        string[] additionalSizeDimension = Enum.GetNames(typeof(SizeDimension));
170        this.sizeComboBox.Items.AddRange(additionalSizeDimension);
[3499]171        this.sizeComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
[3524]172        this.sizeComboBox.SelectedItem = SizeDimension.Constant.ToString();
[3701]173
174        bool changed = false;
175        if (selectedXAxis != null && xAxisComboBox.Items.Contains(selectedXAxis)) {
176          xAxisComboBox.SelectedItem = selectedXAxis;
177          changed = true;
178        }
179        if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
180          yAxisComboBox.SelectedItem = selectedYAxis;
181          changed = true;
182        }
183        if (selectedSizeAxis != null && sizeComboBox.Items.Contains(selectedSizeAxis)) {
184          sizeComboBox.SelectedItem = selectedSizeAxis;
185          changed = true;
186        }
187        if (changed)
188          UpdateDataPoints();
[3499]189      }
[3349]190    }
191
192    private void Content_Reset(object sender, EventArgs e) {
193      if (InvokeRequired)
194        Invoke(new EventHandler(Content_Reset), sender, e);
195      else {
196        this.categoricalMapping.Clear();
197        UpdateDataPoints();
198      }
199    }
200
201    private void UpdateDataPoints() {
202      Series series = this.chart.Series[0];
203      series.Points.Clear();
[3499]204      if (Content != null) {
205        foreach (IRun run in this.Content)
206          this.AddDataPoint(run);
[3543]207
208        //check to correct max bubble size
209        if (this.chart.Series[0].Points.Select(p => p.YValues[1]).Distinct().Count() == 1)
210          this.chart.Series[0]["BubbleMaxSize"] = "2";
211        else
212          this.chart.Series[0]["BubbleMaxSize"] = "7";
213
214        if (this.chart.Series[0].Points.Count == 0)
215          noRunsLabel.Visible = true;
[4209]216        else {
[3543]217          noRunsLabel.Visible = false;
[4209]218          UpdateCursorInterval();
219        }
[3499]220      }
[3428]221    }
222    private void AddDataPoint(IRun run) {
[3349]223      double? xValue;
224      double? yValue;
[3411]225      double? sizeValue;
[3428]226      Series series = this.chart.Series[0];
227      int row = this.Content.ToList().IndexOf(run);
[3726]228
229      if (!xAxisComboBox.DroppedDown)
230        this.xAxisValue = (string)xAxisComboBox.SelectedItem;
231      if (!yAxisComboBox.DroppedDown)
232        this.yAxisValue = (string)yAxisComboBox.SelectedItem;
233      if (!sizeComboBox.DroppedDown)
234        this.sizeAxisValue = (string)sizeComboBox.SelectedItem;
235
236      xValue = GetValue(run, this.xAxisValue);
237      yValue = GetValue(run, this.yAxisValue);
238      sizeValue = GetValue(run, this.sizeAxisValue);
239
[3543]240      if (xValue.HasValue && yValue.HasValue && sizeValue.HasValue) {
[3701]241        xValue = xValue.Value;
242        if (!xJitterFactor.IsAlmost(0.0))
243          xValue += 0.1 * GetXJitter(run) * xJitterFactor * (this.chart.ChartAreas[0].AxisX.Maximum - this.chart.ChartAreas[0].AxisX.Minimum);
244        yValue = yValue.Value;
245        if (!yJitterFactor.IsAlmost(0.0))
246          yValue += 0.1 * GetYJitter(run) * yJitterFactor * (this.chart.ChartAreas[0].AxisY.Maximum - this.chart.ChartAreas[0].AxisY.Minimum);
[3428]247        if (run.Visible) {
[3411]248          DataPoint point = new DataPoint(xValue.Value, new double[] { yValue.Value, sizeValue.Value });
[3428]249          point.Tag = run;
250          point.Color = run.Color;
[3411]251          series.Points.Add(point);
252        }
[3349]253      }
254    }
[3524]255    private double? GetValue(IRun run, string columnName) {
256      if (run == null || string.IsNullOrEmpty(columnName))
[3349]257        return null;
258
[3524]259      if (Enum.IsDefined(typeof(AxisDimension), columnName)) {
260        AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
261        return GetValue(run, axisDimension);
262      } else if (Enum.IsDefined(typeof(SizeDimension), columnName)) {
263        SizeDimension sizeDimension = (SizeDimension)Enum.Parse(typeof(SizeDimension), columnName);
264        return GetValue(run, sizeDimension);
265      } else {
266        int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
267        IItem value = Content.GetValue(run, columnIndex);
268        if (value == null)
269          return null;
[3447]270
[3524]271        DoubleValue doubleValue = value as DoubleValue;
272        IntValue intValue = value as IntValue;
[4049]273        TimeSpanValue timeSpanValue = value as TimeSpanValue;
[3543]274        double? ret = null;
275        if (doubleValue != null) {
276          if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
277            ret = doubleValue.Value;
278        } else if (intValue != null)
[3524]279          ret = intValue.Value;
[4049]280        else if (timeSpanValue != null) {
281          ret = timeSpanValue.Value.TotalSeconds;
282        } else
[3524]283          ret = GetCategoricalValue(columnIndex, value.ToString());
[3447]284
[3524]285        return ret;
286      }
[3349]287    }
[3524]288    private double GetCategoricalValue(int dimension, string value) {
[3349]289      if (!this.categoricalMapping.ContainsKey(dimension))
290        this.categoricalMapping[dimension] = new Dictionary<object, double>();
[3524]291      if (!this.categoricalMapping[dimension].ContainsKey(value)) {
[3349]292        if (this.categoricalMapping[dimension].Values.Count == 0)
[3524]293          this.categoricalMapping[dimension][value] = 1.0;
[3349]294        else
[3524]295          this.categoricalMapping[dimension][value] = this.categoricalMapping[dimension].Values.Max() + 1.0;
[3349]296      }
[3524]297      return this.categoricalMapping[dimension][value];
[3349]298    }
[3524]299    private double GetValue(IRun run, AxisDimension axisDimension) {
300      double value = double.NaN;
301      switch (axisDimension) {
302        case AxisDimension.Index: {
303            value = Content.ToList().IndexOf(run);
304            break;
305          }
306        default: {
307            throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
308          }
309      }
310      return value;
311    }
312    private double GetValue(IRun run, SizeDimension sizeDimension) {
313      double value = double.NaN;
314      switch (sizeDimension) {
315        case SizeDimension.Constant: {
316            value = 2;
317            break;
318          }
319        default: {
320            throw new ArgumentException("No handling strategy for " + sizeDimension.ToString() + " is defined.");
321          }
322      }
323      return value;
324    }
[3707]325    private void UpdateCursorInterval() {
326      Series series = chart.Series[0];
327      double[] xValues = (from point in series.Points
328                          where !point.IsEmpty
329                          select point.XValue)
330                    .DefaultIfEmpty(1.0)
331                    .ToArray();
332      double[] yValues = (from point in series.Points
333                          where !point.IsEmpty
334                          select point.YValues[0])
335                    .DefaultIfEmpty(1.0)
336                    .ToArray();
[3349]337
[3707]338      double xRange = xValues.Max() - xValues.Min();
339      double yRange = yValues.Max() - yValues.Min();
[4049]340      if (xRange.IsAlmost(0.0)) xRange = 1.0;
341      if (yRange.IsAlmost(0.0)) yRange = 1.0;
[3707]342      double xDigits = (int)Math.Log10(xRange) - 3;
343      double yDigits = (int)Math.Log10(yRange) - 3;
344      double xZoomInterval = Math.Pow(10, xDigits);
345      double yZoomInterval = Math.Pow(10, yDigits);
346      this.chart.ChartAreas[0].CursorX.Interval = xZoomInterval;
347      this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
[4049]348
349      //code to handle TimeSpanValues correct
350      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
351      int columnIndex = xAxisComboBox.SelectedIndex - axisDimensionCount;
352      if (columnIndex >= 0 && Content.GetValue(0, columnIndex) is TimeSpanValue)
353        this.chart.ChartAreas[0].CursorX.Interval = 1;
354      columnIndex = yAxisComboBox.SelectedIndex - axisDimensionCount;
355      if (columnIndex >= 0 && Content.GetValue(0, columnIndex) is TimeSpanValue)
356        this.chart.ChartAreas[0].CursorY.Interval = 1;
[3707]357    }
358
[3524]359    #region drag and drop and tooltip
[3411]360    private IRun draggedRun;
361    private void chart_MouseDown(object sender, MouseEventArgs e) {
362      HitTestResult h = this.chart.HitTest(e.X, e.Y);
363      if (h.ChartElementType == ChartElementType.DataPoint) {
[3459]364        IRun run = (IRun)((DataPoint)h.Object).Tag;
[3428]365        if (e.Clicks >= 2) {
[3557]366          IContentView view = MainFormManager.MainForm.ShowContent(run);
367          if (view != null) {
368            view.ReadOnly = this.ReadOnly;
369            view.Locked = this.Locked;
370          }
[3546]371        } else
[3428]372          this.draggedRun = run;
[3546]373        this.chart.ChartAreas[0].CursorX.SetSelectionPosition(double.NaN, double.NaN);
374        this.chart.ChartAreas[0].CursorY.SetSelectionPosition(double.NaN, double.NaN);
[3411]375      }
376    }
377
[3428]378    private void chart_MouseUp(object sender, MouseEventArgs e) {
[3459]379      if (isSelecting) {
380        System.Windows.Forms.DataVisualization.Charting.Cursor xCursor = chart.ChartAreas[0].CursorX;
381        System.Windows.Forms.DataVisualization.Charting.Cursor yCursor = chart.ChartAreas[0].CursorY;
[3428]382
[3459]383        double minX = Math.Min(xCursor.SelectionStart, xCursor.SelectionEnd);
384        double maxX = Math.Max(xCursor.SelectionStart, xCursor.SelectionEnd);
385        double minY = Math.Min(yCursor.SelectionStart, yCursor.SelectionEnd);
386        double maxY = Math.Max(yCursor.SelectionStart, yCursor.SelectionEnd);
[3428]387
[3459]388        //check for click to select model
389        if (minX == maxX && minY == maxY) {
390          HitTestResult hitTest = chart.HitTest(e.X, e.Y);
391          if (hitTest.ChartElementType == ChartElementType.DataPoint) {
392            int pointIndex = hitTest.PointIndex;
393            IRun run = (IRun)this.chart.Series[0].Points[pointIndex].Tag;
394            run.Color = colorDialog.Color;
395          }
396        } else {
397          List<DataPoint> selectedPoints = new List<DataPoint>();
398          foreach (DataPoint p in this.chart.Series[0].Points) {
399            if (p.XValue >= minX && p.XValue < maxX &&
400              p.YValues[0] >= minY && p.YValues[0] < maxY) {
401              selectedPoints.Add(p);
402            }
403          }
404          foreach (DataPoint p in selectedPoints) {
405            IRun run = (IRun)p.Tag;
406            run.Color = colorDialog.Color;
407          }
[3428]408        }
[3638]409        this.chart.ChartAreas[0].CursorX.SelectionStart = this.chart.ChartAreas[0].CursorX.SelectionEnd;
410        this.chart.ChartAreas[0].CursorY.SelectionStart = this.chart.ChartAreas[0].CursorY.SelectionEnd;
[3428]411      }
412    }
413
[3411]414    private void chart_MouseMove(object sender, MouseEventArgs e) {
[3487]415      HitTestResult h = this.chart.HitTest(e.X, e.Y);
[3432]416      if (!Locked) {
417        if (this.draggedRun != null && h.ChartElementType != ChartElementType.DataPoint) {
418          DataObject data = new DataObject();
419          data.SetData("Type", draggedRun.GetType());
420          data.SetData("Value", draggedRun);
[3459]421          if (ReadOnly)
[3432]422            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
[3459]423          else {
[3432]424            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
425            if ((result & DragDropEffects.Move) == DragDropEffects.Move)
426              Content.Remove(draggedRun);
427          }
[3459]428          this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !isSelecting;
429          this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !isSelecting;
[3432]430          this.draggedRun = null;
[3411]431        }
432      }
[4212]433
[3524]434      string newTooltipText = string.Empty;
[3487]435      string oldTooltipText;
436      if (h.ChartElementType == ChartElementType.DataPoint) {
437        IRun run = (IRun)((DataPoint)h.Object).Tag;
[3524]438        newTooltipText = BuildTooltip(run);
[4212]439      } else if (h.ChartElementType == ChartElementType.AxisLabels) {
440        newTooltipText = ((CustomLabel)h.Object).ToolTip;
[3524]441      }
442
[3487]443      oldTooltipText = this.tooltip.GetToolTip(chart);
444      if (newTooltipText != oldTooltipText)
445        this.tooltip.SetToolTip(chart, newTooltipText);
[3411]446    }
[3524]447
448    private string BuildTooltip(IRun run) {
449      string tooltip;
450      tooltip = run.Name + System.Environment.NewLine;
451
452      double? xValue = this.GetValue(run, (string)xAxisComboBox.SelectedItem);
453      double? yValue = this.GetValue(run, (string)yAxisComboBox.SelectedItem);
454      double? sizeValue = this.GetValue(run, (string)sizeComboBox.SelectedItem);
455
456      string xString = xValue == null ? string.Empty : xValue.Value.ToString();
457      string yString = yValue == null ? string.Empty : yValue.Value.ToString();
458      string sizeString = sizeValue == null ? string.Empty : sizeValue.Value.ToString();
459
[4049]460      //code to handle TimeSpanValues correct
461      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
462      int columnIndex = xAxisComboBox.SelectedIndex - axisDimensionCount;
463      if (xValue.HasValue && columnIndex > 0 && Content.GetValue(0, columnIndex) is TimeSpanValue) {
464        TimeSpan time = TimeSpan.FromSeconds(xValue.Value);
[4212]465        xString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.Seconds);
[4049]466      }
467      columnIndex = yAxisComboBox.SelectedIndex - axisDimensionCount;
468      if (yValue.HasValue && columnIndex > 0 && Content.GetValue(0, columnIndex) is TimeSpanValue) {
469        TimeSpan time = TimeSpan.FromSeconds(yValue.Value);
[4212]470        yString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.Seconds);
[4049]471      }
472
[3524]473      tooltip += xAxisComboBox.SelectedItem + " : " + xString + Environment.NewLine;
474      tooltip += yAxisComboBox.SelectedItem + " : " + yString + Environment.NewLine;
475      tooltip += sizeComboBox.SelectedItem + " : " + sizeString + Environment.NewLine;
476
477      return tooltip;
478    }
[3411]479    #endregion
480
481    #region GUI events and updating
482    private double GetXJitter(IRun run) {
483      if (!this.xJitter.ContainsKey(run))
484        this.xJitter[run] = random.NextDouble() * 2.0 - 1.0;
485      return this.xJitter[run];
486    }
487    private double GetYJitter(IRun run) {
488      if (!this.yJitter.ContainsKey(run))
489        this.yJitter[run] = random.NextDouble() * 2.0 - 1.0;
490      return this.yJitter[run];
491    }
492    private void jitterTrackBar_ValueChanged(object sender, EventArgs e) {
493      this.xJitterFactor = xTrackBar.Value / 100.0;
494      this.yJitterFactor = yTrackBar.Value / 100.0;
495      this.UpdateDataPoints();
496    }
497
498    private void AxisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
499      UpdateDataPoints();
500      UpdateAxisLabels();
501    }
[3349]502    private void UpdateAxisLabels() {
503      Axis xAxis = this.chart.ChartAreas[0].AxisX;
504      Axis yAxis = this.chart.ChartAreas[0].AxisY;
[3536]505      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
506      SetCustomAxisLabels(xAxis, xAxisComboBox.SelectedIndex - axisDimensionCount);
507      SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
[3349]508    }
[4049]509
510    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
511      this.UpdateAxisLabels();
512    }
513
[3411]514    private void SetCustomAxisLabels(Axis axis, int dimension) {
515      axis.CustomLabels.Clear();
[3349]516      if (categoricalMapping.ContainsKey(dimension)) {
517        foreach (var pair in categoricalMapping[dimension]) {
[3536]518          string labelText = pair.Key.ToString();
[4212]519          CustomLabel label = new CustomLabel();
520          label.ToolTip = labelText;
[3543]521          if (labelText.Length > 25)
522            labelText = labelText.Substring(0, 25) + " ... ";
[4212]523          label.Text = labelText;
[3349]524          label.GridTicks = GridTickTypes.TickMark;
[4212]525          label.FromPosition = pair.Value - 0.5;
526          label.ToPosition = pair.Value + 0.5;
527          axis.CustomLabels.Add(label);
[3349]528        }
[4049]529      } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
530        this.chart.ChartAreas[0].RecalculateAxesScale();
531        for (double i = axis.Minimum; i <= axis.Maximum; i += axis.LabelStyle.Interval) {
532          TimeSpan time = TimeSpan.FromSeconds(i);
[4058]533          string x = string.Format("{0:00}:{1:00}:{2:00}", (int)time.Hours, time.Minutes, time.Seconds);
534          axis.CustomLabels.Add(i - axis.LabelStyle.Interval / 2, i + axis.LabelStyle.Interval / 2, x);
[4049]535        }
[3349]536      }
537    }
[3411]538
539    private void zoomButton_CheckedChanged(object sender, EventArgs e) {
540      this.isSelecting = selectButton.Checked;
541      this.colorButton.Enabled = this.isSelecting;
542      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !isSelecting;
543      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !isSelecting;
544    }
545    private void colorButton_Click(object sender, EventArgs e) {
546      if (colorDialog.ShowDialog(this) == DialogResult.OK) {
547        this.colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
548      }
549    }
550    private Image GenerateImage(int width, int height, Color fillColor) {
551      Image colorImage = new Bitmap(width, height);
552      using (Graphics gfx = Graphics.FromImage(colorImage)) {
553        using (SolidBrush brush = new SolidBrush(fillColor)) {
554          gfx.FillRectangle(brush, 0, 0, width, height);
555        }
556      }
557      return colorImage;
558    }
559    #endregion
[3349]560  }
561}
Note: See TracBrowser for help on using the repository browser.