Free cookie consent management tool by TermsFeed Policy Generator

source: branches/histogram/HeuristicLab.Analysis.Views/3.3/DataTableView.cs @ 6115

Last change on this file since 6115 was 6115, checked in by abeham, 13 years ago

#1465

  • Added new interface IConfigureableView to HeuristicLab.MainForm
  • Adapted ViewHost to show a configuration button when its ActiveView is of type IConfigureableView
  • Changed DataTableHistoryView to be an IConfigureableView
  • When changing the configuration of a history view the configuration will be applied to every frame
  • Fixed a bug in calculating the histogram (when all values were the same)
  • Added preceeding and trailing 0-bar in the histogram to prevent cutting the first and last column in the view
  • Added a method Replace(IEnumerable<T>) to the ObservableList to do Clear() and AddRange() with just a single event notification
    • Calling that method from the QualityDistributionAnalyzer (otherwise the result view is flickering)
  • Fixing a bug regarding axis labels in the QualityDistributionAnalyzer
  • Removed double AfterDeserializationHook in QAP
File size: 28.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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  /// <summary>
34  /// The visual representation of a <see cref="Variable"/>.
35  /// </summary>
36  [View("DataTable View")]
37  [Content(typeof(DataTable), true)]
38  public partial class DataTableView : NamedItemView {
39    protected List<Series> invisibleSeries;
40    protected Dictionary<IObservableList<double>, DataRow> valuesRowsTable;
41    /// <summary>
42    /// Gets or sets the variable to represent visually.
43    /// </summary>
44    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
45    /// No own data storage present.</remarks>
46    public new DataTable Content {
47      get { return (DataTable)base.Content; }
48      set { base.Content = value; }
49    }
50
51    /// <summary>
52    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
53    /// </summary>
54    public DataTableView() {
55      InitializeComponent();
56      valuesRowsTable = new Dictionary<IObservableList<double>, DataRow>();
57      invisibleSeries = new List<Series>();
58      chart.CustomizeAllChartAreas();
59      chart.ChartAreas[0].CursorX.Interval = 1;
60    }
61
62    #region Event Handler Registration
63    /// <summary>
64    /// Removes the eventhandlers from the underlying <see cref="Variable"/>.
65    /// </summary>
66    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
67    protected override void DeregisterContentEvents() {
68      foreach (DataRow row in Content.Rows)
69        DeregisterDataRowEvents(row);
70      Content.VisualPropertiesChanged -= new EventHandler(Content_VisualPropertiesChanged);
71      Content.Rows.ItemsAdded -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded);
72      Content.Rows.ItemsRemoved -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved);
73      Content.Rows.ItemsReplaced -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced);
74      Content.Rows.CollectionReset -= new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset);
75      base.DeregisterContentEvents();
76    }
77
78    /// <summary>
79    /// Adds eventhandlers to the underlying <see cref="Variable"/>.
80    /// </summary>
81    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
82    protected override void RegisterContentEvents() {
83      base.RegisterContentEvents();
84      Content.VisualPropertiesChanged += new EventHandler(Content_VisualPropertiesChanged);
85      Content.Rows.ItemsAdded += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded);
86      Content.Rows.ItemsRemoved += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved);
87      Content.Rows.ItemsReplaced += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced);
88      Content.Rows.CollectionReset += new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset);
89      foreach (DataRow row in Content.Rows)
90        RegisterDataRowEvents(row);
91    }
92
93    /// <summary>
94    /// Automatically called for every existing data row and whenever a data row is added
95    /// to the data table. Do not call this method directly.
96    /// </summary>
97    /// <param name="row">The DataRow that was added.</param>
98    protected virtual void RegisterDataRowEvents(DataRow row) {
99      row.NameChanged += new EventHandler(Row_NameChanged);
100      row.VisualPropertiesChanged += new EventHandler(Row_VisualPropertiesChanged);
101      valuesRowsTable.Add(row.Values, row);
102      row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
103      row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
104      row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
105      row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
106      row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
107    }
108
109    /// <summary>
110    /// Automatically called for every data row that is removed from the DataTable. Do
111    /// not directly call this method.
112    /// </summary>
113    /// <param name="row">The DataRow that was removed.</param>
114    protected virtual void DeregisterDataRowEvents(DataRow row) {
115      row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
116      row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
117      row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
118      row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
119      row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
120      valuesRowsTable.Remove(row.Values);
121      row.VisualPropertiesChanged -= new EventHandler(Row_VisualPropertiesChanged);
122      row.NameChanged -= new EventHandler(Row_NameChanged);
123    }
124    #endregion
125
126    protected override void OnContentChanged() {
127      base.OnContentChanged();
128      invisibleSeries.Clear();
129      chart.Titles[0].Text = string.Empty;
130      chart.ChartAreas[0].AxisX.Title = string.Empty;
131      chart.ChartAreas[0].AxisY.Title = string.Empty;
132      chart.ChartAreas[0].AxisY2.Title = string.Empty;
133      chart.Series.Clear();
134      if (Content != null) {
135        chart.Titles[0].Text = Content.Name;
136        foreach (DataRow row in Content.Rows)
137          AddDataRow(row);
138        ConfigureChartArea(chart.ChartAreas[0]);
139        RecalculateAxesScale(chart.ChartAreas[0]);
140      }
141    }
142
143    protected override void SetEnabledStateOfControls() {
144      base.SetEnabledStateOfControls();
145      chart.Enabled = Content != null;
146    }
147
148    /// <summary>
149    /// Add the DataRow as a series to the chart.
150    /// </summary>
151    /// <param name="row">DataRow to add as series to the chart.</param>
152    protected virtual void AddDataRow(DataRow row) {
153      Series series = new Series(row.Name);
154      ConfigureSeries(series, row);
155      FillSeriesWithRowValues(series, row);
156
157      chart.Series.Add(series);
158      ConfigureChartArea(chart.ChartAreas[0]);
159      RecalculateAxesScale(chart.ChartAreas[0]);
160      UpdateYCursorInterval();
161    }
162
163    private void ConfigureSeries(Series series, DataRow row) {
164      RemoveCustomPropertyIfExists(series, "PointWidth");
165      series.BorderWidth = 1;
166      series.BorderDashStyle = ChartDashStyle.Solid;
167      series.BorderColor = Color.Empty;
168
169      if (row.VisualProperties.Color != Color.Empty)
170        series.Color = row.VisualProperties.Color;
171      else series.Color = Color.Empty;
172
173      switch (row.VisualProperties.ChartType) {
174        case DataRowVisualProperties.DataRowChartType.Line:
175          series.ChartType = SeriesChartType.FastLine;
176          series.BorderWidth = row.VisualProperties.LineWidth;
177          series.BorderDashStyle = ConvertLineStyle(row.VisualProperties.LineStyle);
178          break;
179        case DataRowVisualProperties.DataRowChartType.Bars:
180          // Bar is incompatible with anything but Bar and StackedBar*
181          if (!chart.Series.Any(x => x.ChartType != SeriesChartType.Bar && x.ChartType != SeriesChartType.StackedBar && x.ChartType != SeriesChartType.StackedBar100))
182            series.ChartType = SeriesChartType.Bar;
183          else {
184            series.ChartType = SeriesChartType.FastPoint; //default
185            row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
186          }
187          break;
188        case DataRowVisualProperties.DataRowChartType.Columns:
189          series.ChartType = SeriesChartType.Column;
190          break;
191        case DataRowVisualProperties.DataRowChartType.Points:
192          series.ChartType = SeriesChartType.FastPoint;
193          break;
194        case DataRowVisualProperties.DataRowChartType.Histogram:
195          series.ChartType = SeriesChartType.Column;
196          series.SetCustomProperty("PointWidth", "1");
197          if (!series.Color.IsEmpty && series.Color.GetBrightness() < 0.25)
198            series.BorderColor = Color.White;
199          else series.BorderColor = Color.Black;
200          break;
201        default:
202          series.ChartType = SeriesChartType.FastPoint;
203          break;
204      }
205      series.YAxisType = row.VisualProperties.SecondYAxis ? AxisType.Secondary : AxisType.Primary;
206      series.XAxisType = row.VisualProperties.SecondXAxis ? AxisType.Secondary : AxisType.Primary;
207      series.ToolTip = row.Name + " X = #INDEX, Y = #VAL";
208    }
209
210    private void ConfigureChartArea(ChartArea area) {
211      if (Content.VisualProperties.TitleFont != null) chart.Titles[0].Font = Content.VisualProperties.TitleFont;
212      if (!Content.VisualProperties.TitleColor.IsEmpty) chart.Titles[0].ForeColor = Content.VisualProperties.TitleColor;
213
214      if (Content.VisualProperties.AxisTitleFont != null) area.AxisX.TitleFont = Content.VisualProperties.AxisTitleFont;
215      if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisX.TitleForeColor = Content.VisualProperties.AxisTitleColor;
216      area.AxisX.Title = Content.VisualProperties.XAxisTitle;
217
218      if (Content.VisualProperties.AxisTitleFont != null) area.AxisX2.TitleFont = Content.VisualProperties.AxisTitleFont;
219      if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisX2.TitleForeColor = Content.VisualProperties.AxisTitleColor;
220      area.AxisX2.Title = Content.VisualProperties.SecondXAxisTitle;
221
222      if (Content.VisualProperties.AxisTitleFont != null) area.AxisY.TitleFont = Content.VisualProperties.AxisTitleFont;
223      if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisY.TitleForeColor = Content.VisualProperties.AxisTitleColor;
224      area.AxisY.Title = Content.VisualProperties.YAxisTitle;
225
226      if (Content.VisualProperties.AxisTitleFont != null) area.AxisY2.TitleFont = Content.VisualProperties.AxisTitleFont;
227      if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisY2.TitleForeColor = Content.VisualProperties.AxisTitleColor;
228      area.AxisY2.Title = Content.VisualProperties.SecondYAxisTitle;
229    }
230
231    private void RecalculateAxesScale(ChartArea area) {
232      // Reset the axes bounds so that RecalculateAxesScale() will assign new bounds
233      foreach (Axis a in area.Axes) {
234        a.Minimum = double.NaN;
235        a.Maximum = double.NaN;
236      }
237      area.RecalculateAxesScale();
238      area.AxisX.IsMarginVisible = false;
239      area.AxisX2.IsMarginVisible = false;
240
241      if (!Content.VisualProperties.XAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.XAxisMinimumFixedValue)) area.AxisX.Minimum = Content.VisualProperties.XAxisMinimumFixedValue;
242      if (!Content.VisualProperties.XAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.XAxisMaximumFixedValue)) area.AxisX.Maximum = Content.VisualProperties.XAxisMaximumFixedValue;
243      if (!Content.VisualProperties.SecondXAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.SecondXAxisMinimumFixedValue)) area.AxisX2.Minimum = Content.VisualProperties.SecondXAxisMinimumFixedValue;
244      if (!Content.VisualProperties.SecondXAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.SecondXAxisMaximumFixedValue)) area.AxisX2.Maximum = Content.VisualProperties.SecondXAxisMaximumFixedValue;
245      if (!Content.VisualProperties.YAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.YAxisMinimumFixedValue)) area.AxisY.Minimum = Content.VisualProperties.YAxisMinimumFixedValue;
246      if (!Content.VisualProperties.YAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.YAxisMaximumFixedValue)) area.AxisY.Maximum = Content.VisualProperties.YAxisMaximumFixedValue;
247      if (!Content.VisualProperties.SecondYAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.SecondYAxisMinimumFixedValue)) area.AxisY2.Minimum = Content.VisualProperties.SecondYAxisMinimumFixedValue;
248      if (!Content.VisualProperties.SecondYAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.SecondYAxisMaximumFixedValue)) area.AxisY2.Maximum = Content.VisualProperties.SecondYAxisMaximumFixedValue;
249      if (area.AxisX.Minimum > area.AxisX.Maximum) area.AxisX.Maximum = area.AxisX.Minimum + 1;
250      if (area.AxisX2.Minimum > area.AxisX2.Maximum) area.AxisX2.Maximum = area.AxisX2.Minimum + 1;
251      if (area.AxisY.Minimum > area.AxisY.Maximum) area.AxisY.Maximum = area.AxisY.Minimum + 1;
252      if (area.AxisY2.Minimum > area.AxisY2.Maximum) area.AxisY2.Maximum = area.AxisY2.Minimum + 1;
253    }
254
255    /// <summary>
256    /// Set the Y Cursor interval to visible points of enabled series.
257    /// </summary>
258    protected virtual void UpdateYCursorInterval() {
259      double interestingValuesRange = (
260        from series in chart.Series
261        where series.Enabled
262        let values = (from point in series.Points
263                      where !point.IsEmpty
264                      select point.YValues[0]).DefaultIfEmpty(1.0)
265        let range = values.Max() - values.Min()
266        where range > 0.0
267        select range
268        ).DefaultIfEmpty(1.0).Min();
269
270      double digits = (int)Math.Log10(interestingValuesRange) - 3;
271      double yZoomInterval = Math.Pow(10, digits);
272      this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
273    }
274
275
276    /// <summary>
277    /// Remove the corresponding series for a certain DataRow.
278    /// </summary>
279    /// <param name="row">DataRow which series should be removed.</param>
280    protected virtual void RemoveDataRow(DataRow row) {
281      Series series = chart.Series[row.Name];
282      chart.Series.Remove(series);
283      if (invisibleSeries.Contains(series))
284        invisibleSeries.Remove(series);
285      RecalculateAxesScale(chart.ChartAreas[0]);
286    }
287
288    #region Event Handlers
289    #region Content Event Handlers
290    protected override void Content_NameChanged(object sender, EventArgs e) {
291      if (InvokeRequired)
292        Invoke(new EventHandler(Content_NameChanged), sender, e);
293      else {
294        chart.Titles[0].Text = Content.Name;
295        base.Content_NameChanged(sender, e);
296      }
297    }
298    private void Content_VisualPropertiesChanged(object sender, EventArgs e) {
299      if (InvokeRequired)
300        Invoke(new EventHandler(Content_VisualPropertiesChanged), sender, e);
301      else {
302        ConfigureChartArea(chart.ChartAreas[0]);
303        RecalculateAxesScale(chart.ChartAreas[0]); // axes min/max could have changed
304      }
305    }
306    #endregion
307    #region Rows Event Handlers
308    private void Rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
309      if (InvokeRequired)
310        Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded), sender, e);
311      else {
312        foreach (DataRow row in e.Items) {
313          AddDataRow(row);
314          RegisterDataRowEvents(row);
315        }
316      }
317    }
318    private void Rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
319      if (InvokeRequired)
320        Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved), sender, e);
321      else {
322        foreach (DataRow row in e.Items) {
323          DeregisterDataRowEvents(row);
324          RemoveDataRow(row);
325        }
326      }
327    }
328    private void Rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
329      if (InvokeRequired)
330        Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced), sender, e);
331      else {
332        foreach (DataRow row in e.OldItems) {
333          DeregisterDataRowEvents(row);
334          RemoveDataRow(row);
335        }
336        foreach (DataRow row in e.Items) {
337          AddDataRow(row);
338          RegisterDataRowEvents(row);
339        }
340      }
341    }
342    private void Rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
343      if (InvokeRequired)
344        Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset), sender, e);
345      else {
346        foreach (DataRow row in e.OldItems) {
347          DeregisterDataRowEvents(row);
348          RemoveDataRow(row);
349        }
350        foreach (DataRow row in e.Items) {
351          AddDataRow(row);
352          RegisterDataRowEvents(row);
353        }
354      }
355    }
356    #endregion
357    #region Row Event Handlers
358    private void Row_VisualPropertiesChanged(object sender, EventArgs e) {
359      if (InvokeRequired)
360        Invoke(new EventHandler(Row_VisualPropertiesChanged), sender, e);
361      else {
362        DataRow row = (DataRow)sender;
363        Series series = chart.Series[row.Name];
364        series.Points.Clear();
365        ConfigureSeries(series, row);
366        FillSeriesWithRowValues(series, row);
367        RecalculateAxesScale(chart.ChartAreas[0]);
368      }
369    }
370    private void Row_NameChanged(object sender, EventArgs e) {
371      if (InvokeRequired)
372        Invoke(new EventHandler(Row_NameChanged), sender, e);
373      else {
374        DataRow row = (DataRow)sender;
375        chart.Series[row.Name].Name = row.Name;
376      }
377    }
378    #endregion
379    #region Values Event Handlers
380    private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
381      if (InvokeRequired)
382        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded), sender, e);
383      else {
384        DataRow row = null;
385        valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
386        if (row != null) {
387          Series rowSeries = chart.Series[row.Name];
388          if (!invisibleSeries.Contains(rowSeries)) {
389            rowSeries.Points.Clear();
390            FillSeriesWithRowValues(rowSeries, row);
391            RecalculateAxesScale(chart.ChartAreas[0]);
392            UpdateYCursorInterval();
393          }
394        }
395      }
396    }
397    private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
398      if (InvokeRequired)
399        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved), sender, e);
400      else {
401        DataRow row = null;
402        valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
403        if (row != null) {
404          Series rowSeries = chart.Series[row.Name];
405          if (!invisibleSeries.Contains(rowSeries)) {
406            rowSeries.Points.Clear();
407            FillSeriesWithRowValues(rowSeries, row);
408            RecalculateAxesScale(chart.ChartAreas[0]);
409            UpdateYCursorInterval();
410          }
411        }
412      }
413    }
414    private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
415      if (InvokeRequired)
416        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced), sender, e);
417      else {
418        DataRow row = null;
419        valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
420        if (row != null) {
421          Series rowSeries = chart.Series[row.Name];
422          if (!invisibleSeries.Contains(rowSeries)) {
423            if (row.VisualProperties.ChartType == DataRowVisualProperties.DataRowChartType.Histogram) {
424              rowSeries.Points.Clear();
425              FillSeriesWithRowValues(rowSeries, row);
426            } else {
427              foreach (IndexedItem<double> item in e.Items) {
428                if (IsInvalidValue(item.Value))
429                  rowSeries.Points[item.Index].IsEmpty = true;
430                else {
431                  rowSeries.Points[item.Index].YValues = new double[] { item.Value };
432                  rowSeries.Points[item.Index].IsEmpty = false;
433                }
434              }
435            }
436            RecalculateAxesScale(chart.ChartAreas[0]);
437            UpdateYCursorInterval();
438          }
439        }
440      }
441    }
442    private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
443      if (InvokeRequired)
444        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved), sender, e);
445      else {
446        DataRow row = null;
447        valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
448        if (row != null) {
449          Series rowSeries = chart.Series[row.Name];
450          if (!invisibleSeries.Contains(rowSeries)) {
451            rowSeries.Points.Clear();
452            FillSeriesWithRowValues(rowSeries, row);
453            RecalculateAxesScale(chart.ChartAreas[0]);
454            UpdateYCursorInterval();
455          }
456        }
457      }
458    }
459
460    private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
461      if (InvokeRequired)
462        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset), sender, e);
463      else {
464        DataRow row = null;
465        valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
466        if (row != null) {
467          Series rowSeries = chart.Series[row.Name];
468          if (!invisibleSeries.Contains(rowSeries)) {
469            rowSeries.Points.Clear();
470            FillSeriesWithRowValues(rowSeries, row);
471            RecalculateAxesScale(chart.ChartAreas[0]);
472            UpdateYCursorInterval();
473          }
474        }
475      }
476    }
477    #endregion
478    #endregion
479
480    #region Chart Event Handlers
481    private void chart_MouseDown(object sender, MouseEventArgs e) {
482      HitTestResult result = chart.HitTest(e.X, e.Y);
483      if (result.ChartElementType == ChartElementType.LegendItem) {
484        ToggleSeriesVisible(result.Series);
485      }
486    }
487    private void chart_MouseMove(object sender, MouseEventArgs e) {
488      HitTestResult result = chart.HitTest(e.X, e.Y);
489      if (result.ChartElementType == ChartElementType.LegendItem)
490        this.Cursor = Cursors.Hand;
491      else
492        this.Cursor = Cursors.Default;
493    }
494    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
495      foreach (LegendItem legendItem in e.LegendItems) {
496        var series = chart.Series[legendItem.SeriesName];
497        if (series != null) {
498          bool seriesIsInvisible = invisibleSeries.Contains(series);
499          foreach (LegendCell cell in legendItem.Cells) {
500            cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black;
501          }
502        }
503      }
504    }
505    private void chart_PropertiesClicked(object sender, EventArgs e) {
506      DataTableVisualPropertiesDialog dialog = new DataTableVisualPropertiesDialog(Content);
507      dialog.ShowDialog();
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, DataRow row) {
531      switch (row.VisualProperties.ChartType) {
532        case DataRowVisualProperties.DataRowChartType.Histogram:
533          CalculateHistogram(series, row);
534          break;
535        default: {
536            for (int i = 0; i < row.Values.Count; i++) {
537              var value = row.Values[i];
538              DataPoint point = new DataPoint();
539              point.XValue = row.VisualProperties.StartIndexZero ? i : i + 1;
540              if (IsInvalidValue(value))
541                point.IsEmpty = true;
542              else
543                point.YValues = new double[] { value };
544              series.Points.Add(point);
545            }
546          }
547          break;
548      }
549    }
550
551    protected virtual void CalculateHistogram(Series series, DataRow row) {
552      series.Points.Clear();
553      if (!row.Values.Any()) return;
554      int bins = row.VisualProperties.Bins;
555
556      double minValue = row.Values.Min();
557      double maxValue = row.Values.Max();
558      double intervalWidth = (maxValue - minValue) / bins;
559      if (intervalWidth < 0) return;
560      if (intervalWidth == 0) {
561        series.Points.AddXY(minValue, row.Values.Count);
562        return;
563      }
564
565      if (!row.VisualProperties.ExactBins) {
566        intervalWidth = HumanRoundRange(intervalWidth);
567        minValue = Math.Floor(minValue / intervalWidth) * intervalWidth;
568        maxValue = Math.Ceiling(maxValue / intervalWidth) * intervalWidth;
569      }
570
571      double current = minValue, intervalCenter = intervalWidth / 2.0;
572      int frequency = 0;
573      series.Points.AddXY(current - intervalCenter, 0); // so that the first column is not visually truncated
574      foreach (double v in row.Values.Where(x => !IsInvalidValue(x)).OrderBy(x => x)) {
575        while (v > current + intervalWidth) {
576          series.Points.AddXY(current + intervalCenter, frequency);
577          current += intervalWidth;
578          frequency = 0;
579        }
580        frequency++;
581      }
582      series.Points.AddXY(current + intervalCenter, frequency);
583      series.Points.AddXY(current + 3 * intervalCenter, 0); // so that the last column is not visually truncated
584    }
585
586    #region Helpers
587    protected void RemoveCustomPropertyIfExists(Series series, string property) {
588      if (series.IsCustomPropertySet(property)) series.DeleteCustomProperty(property);
589    }
590
591    private double HumanRoundRange(double range) {
592      double base10 = Math.Pow(10.0, Math.Floor(Math.Log10(range)));
593      double rounding = range / base10;
594      if (rounding <= 1.5) rounding = 1;
595      else if (rounding <= 2.25) rounding = 2;
596      else if (rounding <= 3.75) rounding = 2.5;
597      else if (rounding <= 7.5) rounding = 5;
598      else rounding = 10;
599      return rounding * base10;
600    }
601
602    private double HumanRoundMax(double max) {
603      double base10;
604      if (max > 0) base10 = Math.Pow(10.0, Math.Floor(Math.Log10(max)));
605      else base10 = Math.Pow(10.0, Math.Ceiling(Math.Log10(-max)));
606      double rounding = (max > 0) ? base10 : -base10;
607      while (rounding < max) rounding += base10;
608      return rounding;
609    }
610
611    private ChartDashStyle ConvertLineStyle(DataRowVisualProperties.DataRowLineStyle dataRowLineStyle) {
612      switch (dataRowLineStyle) {
613        case DataRowVisualProperties.DataRowLineStyle.Dash:
614          return ChartDashStyle.Dash;
615        case DataRowVisualProperties.DataRowLineStyle.DashDot:
616          return ChartDashStyle.DashDot;
617        case DataRowVisualProperties.DataRowLineStyle.DashDotDot:
618          return ChartDashStyle.DashDotDot;
619        case DataRowVisualProperties.DataRowLineStyle.Dot:
620          return ChartDashStyle.Dot;
621        case DataRowVisualProperties.DataRowLineStyle.NotSet:
622          return ChartDashStyle.NotSet;
623        case DataRowVisualProperties.DataRowLineStyle.Solid:
624          return ChartDashStyle.Solid;
625        default:
626          return ChartDashStyle.NotSet;
627      }
628    }
629
630    /// <summary>
631    /// Determines whether a double value can be displayed (converted to Decimal and not an NaN).
632    /// </summary>
633    /// <param name="x">The number to check.</param>
634    /// <returns><code>true</code> if the value can be safely shwon in the chart,
635    /// <code>false</code> otherwise.</returns>
636    protected static bool IsInvalidValue(double x) {
637      return double.IsNaN(x) || x < (double)decimal.MinValue || x > (double)decimal.MaxValue;
638    }
639    #endregion
640  }
641}
Note: See TracBrowser for help on using the repository browser.