Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Analysis.Views/3.3/IndexedDataTableView.cs @ 12764

Last change on this file since 12764 was 12764, checked in by abeham, 9 years ago

#2431:

  • branched relevant plugins
  • added analyzer to calculate quality vs evaluation first-hit graph
  • (also added results parameter)
File size: 26.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.Analysis.Views {
33  [View("IndexedDataTable View")]
34  [Content(typeof(IndexedDataTable<>), true)]
35  public partial class IndexedDataTableView<T> : NamedItemView {
36    protected List<Series> invisibleSeries;
37    protected Dictionary<IObservableList<Tuple<T, double>>, IndexedDataRow<T>> valuesRowsTable;
38    /// <summary>
39    /// Gets or sets the variable to represent visually.
40    /// </summary>
41    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
42    /// No own data storage present.</remarks>
43    public new IndexedDataTable<T> Content {
44      get { return (IndexedDataTable<T>)base.Content; }
45      set { base.Content = value; }
46    }
47
48    /// <summary>
49    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
50    /// </summary>
51    public IndexedDataTableView() {
52      InitializeComponent();
53      valuesRowsTable = new Dictionary<IObservableList<Tuple<T, double>>, IndexedDataRow<T>>();
54      invisibleSeries = new List<Series>();
55      chart.CustomizeAllChartAreas();
56      chart.ChartAreas[0].CursorX.Interval = 1;
57    }
58
59    #region Event Handler Registration
60    /// <summary>
61    /// Removes the eventhandlers from the underlying <see cref="Variable"/>.
62    /// </summary>
63    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
64    protected override void DeregisterContentEvents() {
65      foreach (var row in Content.Rows)
66        DeregisterDataRowEvents(row);
67      Content.VisualPropertiesChanged -= new EventHandler(Content_VisualPropertiesChanged);
68      Content.Rows.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(Rows_ItemsAdded);
69      Content.Rows.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(Rows_ItemsRemoved);
70      Content.Rows.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(Rows_ItemsReplaced);
71      Content.Rows.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(Rows_CollectionReset);
72      base.DeregisterContentEvents();
73    }
74
75    /// <summary>
76    /// Adds eventhandlers to the underlying <see cref="Variable"/>.
77    /// </summary>
78    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
79    protected override void RegisterContentEvents() {
80      base.RegisterContentEvents();
81      Content.VisualPropertiesChanged += new EventHandler(Content_VisualPropertiesChanged);
82      Content.Rows.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(Rows_ItemsAdded);
83      Content.Rows.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(Rows_ItemsRemoved);
84      Content.Rows.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(Rows_ItemsReplaced);
85      Content.Rows.CollectionReset += new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(Rows_CollectionReset);
86      foreach (var row in Content.Rows)
87        RegisterDataRowEvents(row);
88    }
89
90    /// <summary>
91    /// Automatically called for every existing data row and whenever a data row is added
92    /// to the data table. Do not call this method directly.
93    /// </summary>
94    /// <param name="row">The DataRow that was added.</param>
95    protected virtual void RegisterDataRowEvents(IndexedDataRow<T> row) {
96      row.NameChanged += new EventHandler(Row_NameChanged);
97      row.VisualPropertiesChanged += new EventHandler(Row_VisualPropertiesChanged);
98      valuesRowsTable.Add(row.Values, row);
99      row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsAdded);
100      row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsRemoved);
101      row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsReplaced);
102      row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsMoved);
103      row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_CollectionReset);
104    }
105
106    /// <summary>
107    /// Automatically called for every data row that is removed from the DataTable. Do
108    /// not directly call this method.
109    /// </summary>
110    /// <param name="row">The DataRow that was removed.</param>
111    protected virtual void DeregisterDataRowEvents(IndexedDataRow<T> row) {
112      row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsAdded);
113      row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsRemoved);
114      row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsReplaced);
115      row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsMoved);
116      row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_CollectionReset);
117      valuesRowsTable.Remove(row.Values);
118      row.VisualPropertiesChanged -= new EventHandler(Row_VisualPropertiesChanged);
119      row.NameChanged -= new EventHandler(Row_NameChanged);
120    }
121    #endregion
122
123    protected override void OnContentChanged() {
124      base.OnContentChanged();
125      invisibleSeries.Clear();
126      chart.Titles[0].Text = string.Empty;
127      chart.ChartAreas[0].AxisX.Title = string.Empty;
128      chart.ChartAreas[0].AxisY.Title = string.Empty;
129      chart.ChartAreas[0].AxisY2.Title = string.Empty;
130      chart.Series.Clear();
131      if (Content != null) {
132        chart.Titles[0].Text = Content.Name;
133        foreach (var row in Content.Rows)
134          AddDataRow(row);
135        ConfigureChartArea(chart.ChartAreas[0]);
136        RecalculateAxesScale(chart.ChartAreas[0]);
137      }
138    }
139
140    protected override void SetEnabledStateOfControls() {
141      base.SetEnabledStateOfControls();
142      chart.Enabled = Content != null;
143    }
144
145    /// <summary>
146    /// Add the DataRow as a series to the chart.
147    /// </summary>
148    /// <param name="row">DataRow to add as series to the chart.</param>
149    protected virtual void AddDataRow(IndexedDataRow<T> row) {
150      Series series = new Series(row.Name);
151      if (row.VisualProperties.DisplayName.Trim() != String.Empty) series.LegendText = row.VisualProperties.DisplayName;
152      else series.LegendText = row.Name;
153      ConfigureSeries(series, row);
154      FillSeriesWithRowValues(series, row);
155
156      chart.Series.Add(series);
157      ConfigureChartArea(chart.ChartAreas[0]);
158      RecalculateAxesScale(chart.ChartAreas[0]);
159      UpdateYCursorInterval();
160    }
161
162    private void ConfigureSeries(Series series, IndexedDataRow<T> row) {
163      RemoveCustomPropertyIfExists(series, "PointWidth");
164      series.BorderWidth = 1;
165      series.BorderDashStyle = ChartDashStyle.Solid;
166      series.BorderColor = Color.Empty;
167      if (!typeof(T).Equals(typeof(DateTime)))
168        series.IsXValueIndexed = true;
169
170      if (row.VisualProperties.Color != Color.Empty)
171        series.Color = row.VisualProperties.Color;
172      else series.Color = Color.Empty;
173      series.IsVisibleInLegend = row.VisualProperties.IsVisibleInLegend;
174
175      switch (row.VisualProperties.ChartType) {
176        case DataRowVisualProperties.DataRowChartType.Line:
177          series.ChartType = SeriesChartType.FastLine;
178          series.BorderWidth = row.VisualProperties.LineWidth;
179          series.BorderDashStyle = ConvertLineStyle(row.VisualProperties.LineStyle);
180          break;
181        case DataRowVisualProperties.DataRowChartType.Bars:
182          // Bar is incompatible with anything but Bar and StackedBar*
183          if (!chart.Series.Any(x => x.ChartType != SeriesChartType.Bar && x.ChartType != SeriesChartType.StackedBar && x.ChartType != SeriesChartType.StackedBar100)) {
184            series.ChartType = SeriesChartType.Bar;
185            chart.ChartAreas[0].AxisX.Interval = 1;
186          } else {
187            series.ChartType = SeriesChartType.FastPoint; //default
188            row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
189          }
190          break;
191        case DataRowVisualProperties.DataRowChartType.Columns:
192          series.ChartType = SeriesChartType.Column;
193          break;
194        case DataRowVisualProperties.DataRowChartType.Points:
195          series.ChartType = SeriesChartType.FastPoint;
196          break;
197        case DataRowVisualProperties.DataRowChartType.StepLine:
198          series.ChartType = SeriesChartType.StepLine;
199          series.BorderWidth = row.VisualProperties.LineWidth;
200          series.BorderDashStyle = ConvertLineStyle(row.VisualProperties.LineStyle);
201          break;
202        default:
203          series.ChartType = SeriesChartType.FastPoint;
204          break;
205      }
206      series.YAxisType = row.VisualProperties.SecondYAxis ? AxisType.Secondary : AxisType.Primary;
207      series.XAxisType = row.VisualProperties.SecondXAxis ? AxisType.Secondary : AxisType.Primary;
208      if (row.VisualProperties.DisplayName.Trim() != String.Empty) series.LegendText = row.VisualProperties.DisplayName;
209      else series.LegendText = row.Name;
210      series.ToolTip = series.LegendText + " X = #VALX, Y = #VALY";
211    }
212
213    private void ConfigureChartArea(ChartArea area) {
214      if (Content.VisualProperties.TitleFont != null) chart.Titles[0].Font = Content.VisualProperties.TitleFont;
215      if (!Content.VisualProperties.TitleColor.IsEmpty) chart.Titles[0].ForeColor = Content.VisualProperties.TitleColor;
216
217      if (Content.VisualProperties.AxisTitleFont != null) area.AxisX.TitleFont = Content.VisualProperties.AxisTitleFont;
218      if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisX.TitleForeColor = Content.VisualProperties.AxisTitleColor;
219      area.AxisX.Title = Content.VisualProperties.XAxisTitle;
220
221      if (Content.VisualProperties.AxisTitleFont != null) area.AxisX2.TitleFont = Content.VisualProperties.AxisTitleFont;
222      if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisX2.TitleForeColor = Content.VisualProperties.AxisTitleColor;
223      area.AxisX2.Title = Content.VisualProperties.SecondXAxisTitle;
224
225      if (Content.VisualProperties.AxisTitleFont != null) area.AxisY.TitleFont = Content.VisualProperties.AxisTitleFont;
226      if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisY.TitleForeColor = Content.VisualProperties.AxisTitleColor;
227      area.AxisY.Title = Content.VisualProperties.YAxisTitle;
228
229      if (Content.VisualProperties.AxisTitleFont != null) area.AxisY2.TitleFont = Content.VisualProperties.AxisTitleFont;
230      if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisY2.TitleForeColor = Content.VisualProperties.AxisTitleColor;
231      area.AxisY2.Title = Content.VisualProperties.SecondYAxisTitle;
232
233      if (typeof(T).Equals(typeof(DateTime))) {
234        area.AxisX.IntervalType = DateTimeIntervalType.Hours;
235        area.AxisX.LabelStyle.Format = "dd.MM.yyyy HH:mm";
236        area.AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
237      }
238    }
239
240    private void RecalculateAxesScale(ChartArea area) {
241      // Reset the axes bounds so that RecalculateAxesScale() will assign new bounds
242      foreach (Axis a in area.Axes) {
243        a.Minimum = double.NaN;
244        a.Maximum = double.NaN;
245      }
246      area.RecalculateAxesScale();
247      area.AxisX.IsMarginVisible = false;
248      area.AxisX2.IsMarginVisible = false;
249
250      if (!Content.VisualProperties.XAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.XAxisMinimumFixedValue)) area.AxisX.Minimum = Content.VisualProperties.XAxisMinimumFixedValue;
251      if (!Content.VisualProperties.XAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.XAxisMaximumFixedValue)) area.AxisX.Maximum = Content.VisualProperties.XAxisMaximumFixedValue;
252      if (!Content.VisualProperties.SecondXAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.SecondXAxisMinimumFixedValue)) area.AxisX2.Minimum = Content.VisualProperties.SecondXAxisMinimumFixedValue;
253      if (!Content.VisualProperties.SecondXAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.SecondXAxisMaximumFixedValue)) area.AxisX2.Maximum = Content.VisualProperties.SecondXAxisMaximumFixedValue;
254      if (!Content.VisualProperties.YAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.YAxisMinimumFixedValue)) area.AxisY.Minimum = Content.VisualProperties.YAxisMinimumFixedValue;
255      if (!Content.VisualProperties.YAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.YAxisMaximumFixedValue)) area.AxisY.Maximum = Content.VisualProperties.YAxisMaximumFixedValue;
256      if (!Content.VisualProperties.SecondYAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.SecondYAxisMinimumFixedValue)) area.AxisY2.Minimum = Content.VisualProperties.SecondYAxisMinimumFixedValue;
257      if (!Content.VisualProperties.SecondYAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.SecondYAxisMaximumFixedValue)) area.AxisY2.Maximum = Content.VisualProperties.SecondYAxisMaximumFixedValue;
258      if (area.AxisX.Minimum >= area.AxisX.Maximum) area.AxisX.Maximum = area.AxisX.Minimum + 1;
259      if (area.AxisX2.Minimum >= area.AxisX2.Maximum) area.AxisX2.Maximum = area.AxisX2.Minimum + 1;
260      if (area.AxisY.Minimum >= area.AxisY.Maximum) area.AxisY.Maximum = area.AxisY.Minimum + 1;
261      if (area.AxisY2.Minimum >= area.AxisY2.Maximum) area.AxisY2.Maximum = area.AxisY2.Minimum + 1;
262    }
263
264    /// <summary>
265    /// Set the Y Cursor interval to visible points of enabled series.
266    /// </summary>
267    protected virtual void UpdateYCursorInterval() {
268      double interestingValuesRange = (
269        from series in chart.Series
270        where series.Enabled
271        let values = (from point in series.Points
272                      where !point.IsEmpty
273                      select point.YValues[0]).DefaultIfEmpty(1.0)
274        let range = values.Max() - values.Min()
275        where range > 0.0
276        select range
277        ).DefaultIfEmpty(1.0).Min();
278
279      double digits = (int)Math.Log10(interestingValuesRange) - 3;
280      double yZoomInterval = Math.Pow(10, digits);
281      this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
282    }
283
284
285    /// <summary>
286    /// Remove the corresponding series for a certain DataRow.
287    /// </summary>
288    /// <param name="row">DataRow which series should be removed.</param>
289    protected virtual void RemoveDataRow(IndexedDataRow<T> row) {
290      Series series = chart.Series[row.Name];
291      chart.Series.Remove(series);
292      if (invisibleSeries.Contains(series))
293        invisibleSeries.Remove(series);
294      RecalculateAxesScale(chart.ChartAreas[0]);
295    }
296
297    #region Event Handlers
298    #region Content Event Handlers
299    protected override void Content_NameChanged(object sender, EventArgs e) {
300      if (InvokeRequired)
301        Invoke(new EventHandler(Content_NameChanged), sender, e);
302      else {
303        chart.Titles[0].Text = Content.Name;
304        base.Content_NameChanged(sender, e);
305      }
306    }
307    private void Content_VisualPropertiesChanged(object sender, EventArgs e) {
308      if (InvokeRequired)
309        Invoke(new EventHandler(Content_VisualPropertiesChanged), sender, e);
310      else {
311        ConfigureChartArea(chart.ChartAreas[0]);
312        RecalculateAxesScale(chart.ChartAreas[0]); // axes min/max could have changed
313      }
314    }
315    #endregion
316    #region Rows Event Handlers
317    private void Rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedDataRow<T>> e) {
318      if (InvokeRequired)
319        Invoke(new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(Rows_ItemsAdded), sender, e);
320      else {
321        foreach (var row in e.Items) {
322          AddDataRow(row);
323          RegisterDataRowEvents(row);
324        }
325      }
326    }
327    private void Rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedDataRow<T>> e) {
328      if (InvokeRequired)
329        Invoke(new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(Rows_ItemsRemoved), sender, e);
330      else {
331        foreach (var row in e.Items) {
332          DeregisterDataRowEvents(row);
333          RemoveDataRow(row);
334        }
335      }
336    }
337    private void Rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedDataRow<T>> e) {
338      if (InvokeRequired)
339        Invoke(new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(Rows_ItemsReplaced), sender, e);
340      else {
341        foreach (var row in e.OldItems) {
342          DeregisterDataRowEvents(row);
343          RemoveDataRow(row);
344        }
345        foreach (var row in e.Items) {
346          AddDataRow(row);
347          RegisterDataRowEvents(row);
348        }
349      }
350    }
351    private void Rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedDataRow<T>> e) {
352      if (InvokeRequired)
353        Invoke(new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(Rows_CollectionReset), sender, e);
354      else {
355        foreach (var row in e.OldItems) {
356          DeregisterDataRowEvents(row);
357          RemoveDataRow(row);
358        }
359        foreach (var row in e.Items) {
360          AddDataRow(row);
361          RegisterDataRowEvents(row);
362        }
363      }
364    }
365    #endregion
366    #region Row Event Handlers
367    private void Row_VisualPropertiesChanged(object sender, EventArgs e) {
368      if (InvokeRequired)
369        Invoke(new EventHandler(Row_VisualPropertiesChanged), sender, e);
370      else {
371        var row = (IndexedDataRow<T>)sender;
372        Series series = chart.Series[row.Name];
373        series.Points.Clear();
374        ConfigureSeries(series, row);
375        FillSeriesWithRowValues(series, row);
376        RecalculateAxesScale(chart.ChartAreas[0]);
377      }
378    }
379    private void Row_NameChanged(object sender, EventArgs e) {
380      if (InvokeRequired)
381        Invoke(new EventHandler(Row_NameChanged), sender, e);
382      else {
383        var row = (IndexedDataRow<T>)sender;
384        chart.Series[row.Name].Name = row.Name;
385      }
386    }
387    #endregion
388    #region Values Event Handlers
389    private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
390      if (InvokeRequired)
391        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsAdded), sender, e);
392      else {
393        IndexedDataRow<T> row = null;
394        valuesRowsTable.TryGetValue((IObservableList<Tuple<T, double>>)sender, out row);
395        if (row != null) {
396          Series rowSeries = chart.Series[row.Name];
397          if (!invisibleSeries.Contains(rowSeries)) {
398            rowSeries.Points.Clear();
399            FillSeriesWithRowValues(rowSeries, row);
400            RecalculateAxesScale(chart.ChartAreas[0]);
401            UpdateYCursorInterval();
402          }
403        }
404      }
405    }
406    private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
407      if (InvokeRequired)
408        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsRemoved), sender, e);
409      else {
410        IndexedDataRow<T> row = null;
411        valuesRowsTable.TryGetValue((IObservableList<Tuple<T, double>>)sender, out row);
412        if (row != null) {
413          Series rowSeries = chart.Series[row.Name];
414          if (!invisibleSeries.Contains(rowSeries)) {
415            rowSeries.Points.Clear();
416            FillSeriesWithRowValues(rowSeries, row);
417            RecalculateAxesScale(chart.ChartAreas[0]);
418            UpdateYCursorInterval();
419          }
420        }
421      }
422    }
423    private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
424      if (InvokeRequired)
425        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsReplaced), sender, e);
426      else {
427        IndexedDataRow<T> row = null;
428        valuesRowsTable.TryGetValue((IObservableList<Tuple<T, double>>)sender, out row);
429        if (row != null) {
430          Series rowSeries = chart.Series[row.Name];
431          if (!invisibleSeries.Contains(rowSeries)) {
432            if (row.VisualProperties.ChartType == DataRowVisualProperties.DataRowChartType.Histogram) {
433              rowSeries.Points.Clear();
434              FillSeriesWithRowValues(rowSeries, row);
435            } else {
436              foreach (IndexedItem<Tuple<T, double>> item in e.Items) {
437                rowSeries.Points[item.Index].SetValueXY(item.Value.Item1, item.Value.Item2);
438              }
439            }
440            RecalculateAxesScale(chart.ChartAreas[0]);
441            UpdateYCursorInterval();
442          }
443        }
444      }
445    }
446    private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
447      if (InvokeRequired)
448        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsMoved), sender, e);
449      else {
450        IndexedDataRow<T> row = null;
451        valuesRowsTable.TryGetValue((IObservableList<Tuple<T, double>>)sender, out row);
452        if (row != null) {
453          Series rowSeries = chart.Series[row.Name];
454          if (!invisibleSeries.Contains(rowSeries)) {
455            rowSeries.Points.Clear();
456            FillSeriesWithRowValues(rowSeries, row);
457            RecalculateAxesScale(chart.ChartAreas[0]);
458            UpdateYCursorInterval();
459          }
460        }
461      }
462    }
463
464    private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
465      if (InvokeRequired)
466        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_CollectionReset), sender, e);
467      else {
468        IndexedDataRow<T> row = null;
469        valuesRowsTable.TryGetValue((IObservableList<Tuple<T, double>>)sender, out row);
470        if (row != null) {
471          Series rowSeries = chart.Series[row.Name];
472          if (!invisibleSeries.Contains(rowSeries)) {
473            rowSeries.Points.Clear();
474            FillSeriesWithRowValues(rowSeries, row);
475            RecalculateAxesScale(chart.ChartAreas[0]);
476            UpdateYCursorInterval();
477          }
478        }
479      }
480    }
481    #endregion
482    #endregion
483
484    #region Chart Event Handlers
485    private void chart_MouseDown(object sender, MouseEventArgs e) {
486      HitTestResult result = chart.HitTest(e.X, e.Y);
487      if (result.ChartElementType == ChartElementType.LegendItem) {
488        ToggleSeriesVisible(result.Series);
489      }
490    }
491    private void chart_MouseMove(object sender, MouseEventArgs e) {
492      HitTestResult result = chart.HitTest(e.X, e.Y);
493      if (result.ChartElementType == ChartElementType.LegendItem)
494        this.Cursor = Cursors.Hand;
495      else
496        this.Cursor = Cursors.Default;
497    }
498    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
499      foreach (LegendItem legendItem in e.LegendItems) {
500        var series = chart.Series[legendItem.SeriesName];
501        if (series != null) {
502          bool seriesIsInvisible = invisibleSeries.Contains(series);
503          foreach (LegendCell cell in legendItem.Cells) {
504            cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black;
505          }
506        }
507      }
508    }
509    #endregion
510
511    private void ToggleSeriesVisible(Series series) {
512      if (!invisibleSeries.Contains(series)) {
513        series.Points.Clear();
514        invisibleSeries.Add(series);
515      } else {
516        invisibleSeries.Remove(series);
517        if (Content != null) {
518
519          var row = (from r in Content.Rows
520                     where r.Name == series.Name
521                     select r).Single();
522          FillSeriesWithRowValues(series, row);
523          this.chart.Legends[series.Legend].ForeColor = Color.Black;
524          RecalculateAxesScale(chart.ChartAreas[0]);
525          UpdateYCursorInterval();
526        }
527      }
528    }
529
530    private void FillSeriesWithRowValues(Series series, IndexedDataRow<T> row) {
531      for (int i = 0; i < row.Values.Count; i++) {
532        var value = row.Values[i];
533        DataPoint point = new DataPoint();
534        point.SetValueXY(value.Item1, value.Item2);
535        series.Points.Add(point);
536      }
537    }
538
539    #region Helpers
540    protected void RemoveCustomPropertyIfExists(Series series, string property) {
541      if (series.IsCustomPropertySet(property)) series.DeleteCustomProperty(property);
542    }
543
544    private ChartDashStyle ConvertLineStyle(DataRowVisualProperties.DataRowLineStyle dataRowLineStyle) {
545      switch (dataRowLineStyle) {
546        case DataRowVisualProperties.DataRowLineStyle.Dash:
547          return ChartDashStyle.Dash;
548        case DataRowVisualProperties.DataRowLineStyle.DashDot:
549          return ChartDashStyle.DashDot;
550        case DataRowVisualProperties.DataRowLineStyle.DashDotDot:
551          return ChartDashStyle.DashDotDot;
552        case DataRowVisualProperties.DataRowLineStyle.Dot:
553          return ChartDashStyle.Dot;
554        case DataRowVisualProperties.DataRowLineStyle.NotSet:
555          return ChartDashStyle.NotSet;
556        case DataRowVisualProperties.DataRowLineStyle.Solid:
557          return ChartDashStyle.Solid;
558        default:
559          return ChartDashStyle.NotSet;
560      }
561    }
562
563    /// <summary>
564    /// Determines whether a double value can be displayed (converted to Decimal and not an NaN).
565    /// </summary>
566    /// <param name="x">The number to check.</param>
567    /// <returns><code>true</code> if the value can be safely shwon in the chart,
568    /// <code>false</code> otherwise.</returns>
569    protected static bool IsInvalidValue(double x) {
570      return double.IsNaN(x) || x < (double)decimal.MinValue || x > (double)decimal.MaxValue;
571    }
572    #endregion
573  }
574}
Note: See TracBrowser for help on using the repository browser.