Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 23.7 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      this.categoricalMapping = new Dictionary<int, Dictionary<object, double>>();
57      this.xJitter = new Dictionary<IRun, double>();
58      this.yJitter = new Dictionary<IRun, double>();
59      this.random = new Random();
60      this.colorDialog.Color = Color.Black;
61      this.colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
62      this.isSelecting = false;
63
64      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
65      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
66      this.chart.ChartAreas[0].CursorX.Interval = 1;
67      this.chart.ChartAreas[0].CursorY.Interval = 1;
68      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !this.isSelecting;
69      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !this.isSelecting;
70    }
71
72    public new RunCollection Content {
73      get { return (RunCollection)base.Content; }
74      set { base.Content = value; }
75    }
76
77    public IStringConvertibleMatrix Matrix {
78      get { return this.Content; }
79    }
80
81    protected override void RegisterContentEvents() {
82      base.RegisterContentEvents();
83      Content.Reset += new EventHandler(Content_Reset);
84      Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
85      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
86      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
87      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
88      RegisterRunEvents(Content);
89    }
90    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
91      foreach (IRun run in runs)
92        run.Changed += new EventHandler(run_Changed);
93    }
94    protected override void DeregisterContentEvents() {
95      base.DeregisterContentEvents();
96      Content.Reset -= new EventHandler(Content_Reset);
97      Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
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);
101      DeregisterRunEvents(Content);
102    }
103    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
104      foreach (IRun run in runs)
105        run.Changed -= new EventHandler(run_Changed);
106    }
107
108    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
109      DeregisterRunEvents(e.OldItems);
110      RegisterRunEvents(e.Items);
111    }
112    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
113      DeregisterRunEvents(e.Items);
114    }
115    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
116      RegisterRunEvents(e.Items);
117    }
118    private void run_Changed(object sender, EventArgs e) {
119      if (InvokeRequired)
120        this.Invoke(new EventHandler(run_Changed), sender, e);
121      else {
122        IRun run = (IRun)sender;
123        UpdateRun(run);
124      }
125    }
126
127    private void UpdateRun(IRun run) {
128      DataPoint point = this.chart.Series[0].Points.Where(p => p.Tag == run).SingleOrDefault();
129      if (point != null) {
130        point.Color = run.Color;
131        if (!run.Visible)
132          this.chart.Series[0].Points.Remove(point);
133      } else
134        AddDataPoint(run);
135      UpdateCursorInterval();
136
137
138      if (this.chart.Series[0].Points.Count == 0)
139        noRunsLabel.Visible = true;
140      else
141        noRunsLabel.Visible = false;
142    }
143
144    protected override void OnContentChanged() {
145      base.OnContentChanged();
146      this.categoricalMapping.Clear();
147      UpdateComboBoxes();
148      UpdateDataPoints();
149    }
150    private void Content_ColumnNamesChanged(object sender, EventArgs e) {
151      if (InvokeRequired)
152        Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
153      else
154        UpdateComboBoxes();
155    }
156
157    private void UpdateComboBoxes() {
158      string selectedXAxis = (string)this.xAxisComboBox.SelectedItem;
159      string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
160      string selectedSizeAxis = (string)this.sizeComboBox.SelectedItem;
161      this.xAxisComboBox.Items.Clear();
162      this.yAxisComboBox.Items.Clear();
163      this.sizeComboBox.Items.Clear();
164      if (Content != null) {
165        string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
166        this.xAxisComboBox.Items.AddRange(additionalAxisDimension);
167        this.xAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
168        this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
169        this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
170        string[] additionalSizeDimension = Enum.GetNames(typeof(SizeDimension));
171        this.sizeComboBox.Items.AddRange(additionalSizeDimension);
172        this.sizeComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
173        this.sizeComboBox.SelectedItem = SizeDimension.Constant.ToString();
174
175        bool changed = false;
176        if (selectedXAxis != null && xAxisComboBox.Items.Contains(selectedXAxis)) {
177          xAxisComboBox.SelectedItem = selectedXAxis;
178          changed = true;
179        }
180        if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
181          yAxisComboBox.SelectedItem = selectedYAxis;
182          changed = true;
183        }
184        if (selectedSizeAxis != null && sizeComboBox.Items.Contains(selectedSizeAxis)) {
185          sizeComboBox.SelectedItem = selectedSizeAxis;
186          changed = true;
187        }
188        if (changed)
189          UpdateDataPoints();
190      }
191    }
192
193    private void Content_Reset(object sender, EventArgs e) {
194      if (InvokeRequired)
195        Invoke(new EventHandler(Content_Reset), sender, e);
196      else {
197        this.categoricalMapping.Clear();
198        UpdateDataPoints();
199      }
200    }
201
202    private void UpdateDataPoints() {
203      Series series = this.chart.Series[0];
204      series.Points.Clear();
205      if (Content != null) {
206        foreach (IRun run in this.Content)
207          this.AddDataPoint(run);
208
209        //check to correct max bubble size
210        if (this.chart.Series[0].Points.Select(p => p.YValues[1]).Distinct().Count() == 1)
211          this.chart.Series[0]["BubbleMaxSize"] = "2";
212        else
213          this.chart.Series[0]["BubbleMaxSize"] = "7";
214
215        if (this.chart.Series[0].Points.Count == 0)
216          noRunsLabel.Visible = true;
217        else
218          noRunsLabel.Visible = false;
219        UpdateCursorInterval();
220      }
221    }
222    private void AddDataPoint(IRun run) {
223      double? xValue;
224      double? yValue;
225      double? sizeValue;
226      Series series = this.chart.Series[0];
227      int row = this.Content.ToList().IndexOf(run);
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
240      if (xValue.HasValue && yValue.HasValue && sizeValue.HasValue) {
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);
247        if (run.Visible) {
248          DataPoint point = new DataPoint(xValue.Value, new double[] { yValue.Value, sizeValue.Value });
249          point.Tag = run;
250          point.Color = run.Color;
251          series.Points.Add(point);
252        }
253      }
254    }
255    private double? GetValue(IRun run, string columnName) {
256      if (run == null || string.IsNullOrEmpty(columnName))
257        return null;
258
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;
270
271        DoubleValue doubleValue = value as DoubleValue;
272        IntValue intValue = value as IntValue;
273        TimeSpanValue timeSpanValue = value as TimeSpanValue;
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)
279          ret = intValue.Value;
280        else if (timeSpanValue != null) {
281          ret = timeSpanValue.Value.TotalSeconds;
282        } else
283          ret = GetCategoricalValue(columnIndex, value.ToString());
284
285        return ret;
286      }
287    }
288    private double GetCategoricalValue(int dimension, string value) {
289      if (!this.categoricalMapping.ContainsKey(dimension))
290        this.categoricalMapping[dimension] = new Dictionary<object, double>();
291      if (!this.categoricalMapping[dimension].ContainsKey(value)) {
292        if (this.categoricalMapping[dimension].Values.Count == 0)
293          this.categoricalMapping[dimension][value] = 1.0;
294        else
295          this.categoricalMapping[dimension][value] = this.categoricalMapping[dimension].Values.Max() + 1.0;
296      }
297      return this.categoricalMapping[dimension][value];
298    }
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    }
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();
337
338      double xRange = xValues.Max() - xValues.Min();
339      double yRange = yValues.Max() - yValues.Min();
340      if (xRange.IsAlmost(0.0)) xRange = 1.0;
341      if (yRange.IsAlmost(0.0)) yRange = 1.0;
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;
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;
357    }
358
359    #region drag and drop and tooltip
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) {
364        IRun run = (IRun)((DataPoint)h.Object).Tag;
365        if (e.Clicks >= 2) {
366          IContentView view = MainFormManager.MainForm.ShowContent(run);
367          if (view != null) {
368            view.ReadOnly = this.ReadOnly;
369            view.Locked = this.Locked;
370          }
371        } else
372          this.draggedRun = run;
373        this.chart.ChartAreas[0].CursorX.SetSelectionPosition(double.NaN, double.NaN);
374        this.chart.ChartAreas[0].CursorY.SetSelectionPosition(double.NaN, double.NaN);
375      }
376    }
377
378    private void chart_MouseUp(object sender, MouseEventArgs e) {
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;
382
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);
387
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          }
408        }
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;
411      }
412    }
413
414    private void chart_MouseMove(object sender, MouseEventArgs e) {
415      HitTestResult h = this.chart.HitTest(e.X, e.Y);
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);
421          if (ReadOnly)
422            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
423          else {
424            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
425            if ((result & DragDropEffects.Move) == DragDropEffects.Move)
426              Content.Remove(draggedRun);
427          }
428          this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !isSelecting;
429          this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !isSelecting;
430          this.draggedRun = null;
431        }
432      }
433      string newTooltipText = string.Empty;
434      string oldTooltipText;
435      if (h.ChartElementType == ChartElementType.DataPoint) {
436        IRun run = (IRun)((DataPoint)h.Object).Tag;
437        newTooltipText = BuildTooltip(run);
438      }
439
440      oldTooltipText = this.tooltip.GetToolTip(chart);
441      if (newTooltipText != oldTooltipText)
442        this.tooltip.SetToolTip(chart, newTooltipText);
443    }
444
445    private string BuildTooltip(IRun run) {
446      string tooltip;
447      tooltip = run.Name + System.Environment.NewLine;
448
449      double? xValue = this.GetValue(run, (string)xAxisComboBox.SelectedItem);
450      double? yValue = this.GetValue(run, (string)yAxisComboBox.SelectedItem);
451      double? sizeValue = this.GetValue(run, (string)sizeComboBox.SelectedItem);
452
453      string xString = xValue == null ? string.Empty : xValue.Value.ToString();
454      string yString = yValue == null ? string.Empty : yValue.Value.ToString();
455      string sizeString = sizeValue == null ? string.Empty : sizeValue.Value.ToString();
456
457      //code to handle TimeSpanValues correct
458      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
459      int columnIndex = xAxisComboBox.SelectedIndex - axisDimensionCount;
460      if (xValue.HasValue && columnIndex > 0 && Content.GetValue(0, columnIndex) is TimeSpanValue) {
461        TimeSpan time = TimeSpan.FromSeconds(xValue.Value);
462        xString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.TotalSeconds);
463      }
464      columnIndex = yAxisComboBox.SelectedIndex - axisDimensionCount;
465      if (yValue.HasValue && columnIndex > 0 && Content.GetValue(0, columnIndex) is TimeSpanValue) {
466        TimeSpan time = TimeSpan.FromSeconds(yValue.Value);
467        yString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.TotalSeconds);
468      }
469
470      tooltip += xAxisComboBox.SelectedItem + " : " + xString + Environment.NewLine;
471      tooltip += yAxisComboBox.SelectedItem + " : " + yString + Environment.NewLine;
472      tooltip += sizeComboBox.SelectedItem + " : " + sizeString + Environment.NewLine;
473
474      return tooltip;
475    }
476    #endregion
477
478    #region GUI events and updating
479    private double GetXJitter(IRun run) {
480      if (!this.xJitter.ContainsKey(run))
481        this.xJitter[run] = random.NextDouble() * 2.0 - 1.0;
482      return this.xJitter[run];
483    }
484    private double GetYJitter(IRun run) {
485      if (!this.yJitter.ContainsKey(run))
486        this.yJitter[run] = random.NextDouble() * 2.0 - 1.0;
487      return this.yJitter[run];
488    }
489    private void jitterTrackBar_ValueChanged(object sender, EventArgs e) {
490      this.xJitterFactor = xTrackBar.Value / 100.0;
491      this.yJitterFactor = yTrackBar.Value / 100.0;
492      this.UpdateDataPoints();
493    }
494
495    private void AxisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
496      UpdateDataPoints();
497      UpdateAxisLabels();
498    }
499    private void UpdateAxisLabels() {
500      Axis xAxis = this.chart.ChartAreas[0].AxisX;
501      Axis yAxis = this.chart.ChartAreas[0].AxisY;
502      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
503      SetCustomAxisLabels(xAxis, xAxisComboBox.SelectedIndex - axisDimensionCount);
504      SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
505    }
506
507    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
508      this.UpdateAxisLabels();
509    }
510
511    private void SetCustomAxisLabels(Axis axis, int dimension) {
512      axis.CustomLabels.Clear();
513      if (categoricalMapping.ContainsKey(dimension)) {
514        CustomLabel label = null;
515        foreach (var pair in categoricalMapping[dimension]) {
516          string labelText = pair.Key.ToString();
517          if (labelText.Length > 25)
518            labelText = labelText.Substring(0, 25) + " ... ";
519          label = axis.CustomLabels.Add(pair.Value - 0.5, pair.Value + 0.5, labelText);
520          label.GridTicks = GridTickTypes.TickMark;
521        }
522        axis.IsLabelAutoFit = false;
523        axis.LabelStyle.Enabled = true;
524        axis.LabelStyle.Angle = 0;
525        axis.LabelStyle.TruncatedLabels = true;
526      } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
527        this.chart.ChartAreas[0].RecalculateAxesScale();
528        for (double i = axis.Minimum; i <= axis.Maximum; i += axis.LabelStyle.Interval) {
529          TimeSpan time = TimeSpan.FromSeconds(i);
530          string x = string.Format("{0:00}:{1:00}:{2:00}", (int)time.Hours, time.Minutes, time.Seconds);
531          axis.CustomLabels.Add(i - axis.LabelStyle.Interval / 2, i + axis.LabelStyle.Interval / 2, x);
532        }
533      }
534    }
535
536    private void zoomButton_CheckedChanged(object sender, EventArgs e) {
537      this.isSelecting = selectButton.Checked;
538      this.colorButton.Enabled = this.isSelecting;
539      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !isSelecting;
540      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !isSelecting;
541    }
542    private void colorButton_Click(object sender, EventArgs e) {
543      if (colorDialog.ShowDialog(this) == DialogResult.OK) {
544        this.colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
545      }
546    }
547    private Image GenerateImage(int width, int height, Color fillColor) {
548      Image colorImage = new Bitmap(width, height);
549      using (Graphics gfx = Graphics.FromImage(colorImage)) {
550        using (SolidBrush brush = new SolidBrush(fillColor)) {
551          gfx.FillRectangle(brush, 0, 0, width, height);
552        }
553      }
554      return colorImage;
555    }
556    #endregion
557  }
558}
Note: See TracBrowser for help on using the repository browser.