Free cookie consent management tool by TermsFeed Policy Generator

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

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

adapted bubble chart to handle TimeSpanValues correctly (ticket #1047)

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