Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4636 was 4636, checked in by epitzer, 14 years ago

More improvements to EnhancedChart (#1237)

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