1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
28 | using HeuristicLab.Collections;
|
---|
29 | using HeuristicLab.Core.Views;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Analysis.Views {
|
---|
33 | [View("DataTable View")]
|
---|
34 | [Content(typeof(DataTable), true)]
|
---|
35 | public partial class DataTableView : NamedItemView, IConfigureableView {
|
---|
36 | protected List<Series> invisibleSeries;
|
---|
37 | protected Dictionary<IObservableList<double>, DataRow> valuesRowsTable;
|
---|
38 |
|
---|
39 | public new DataTable Content {
|
---|
40 | get { return (DataTable)base.Content; }
|
---|
41 | set { base.Content = value; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public DataTableView() {
|
---|
45 | InitializeComponent();
|
---|
46 | valuesRowsTable = new Dictionary<IObservableList<double>, DataRow>();
|
---|
47 | invisibleSeries = new List<Series>();
|
---|
48 | chart.CustomizeAllChartAreas();
|
---|
49 | chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
50 | }
|
---|
51 |
|
---|
52 | #region Event Handler Registration
|
---|
53 | protected override void DeregisterContentEvents() {
|
---|
54 | foreach (DataRow row in Content.Rows)
|
---|
55 | DeregisterDataRowEvents(row);
|
---|
56 | Content.VisualPropertiesChanged -= new EventHandler(Content_VisualPropertiesChanged);
|
---|
57 | Content.Rows.ItemsAdded -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded);
|
---|
58 | Content.Rows.ItemsRemoved -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved);
|
---|
59 | Content.Rows.ItemsReplaced -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced);
|
---|
60 | Content.Rows.CollectionReset -= new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset);
|
---|
61 | base.DeregisterContentEvents();
|
---|
62 | }
|
---|
63 | protected override void RegisterContentEvents() {
|
---|
64 | base.RegisterContentEvents();
|
---|
65 | Content.VisualPropertiesChanged += new EventHandler(Content_VisualPropertiesChanged);
|
---|
66 | Content.Rows.ItemsAdded += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded);
|
---|
67 | Content.Rows.ItemsRemoved += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved);
|
---|
68 | Content.Rows.ItemsReplaced += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced);
|
---|
69 | Content.Rows.CollectionReset += new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset);
|
---|
70 | foreach (DataRow row in Content.Rows)
|
---|
71 | RegisterDataRowEvents(row);
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected virtual void RegisterDataRowEvents(DataRow row) {
|
---|
75 | row.NameChanged += new EventHandler(Row_NameChanged);
|
---|
76 | row.VisualPropertiesChanged += new EventHandler(Row_VisualPropertiesChanged);
|
---|
77 | valuesRowsTable.Add(row.Values, row);
|
---|
78 | row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
79 | row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
80 | row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
81 | row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
82 | row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
83 | }
|
---|
84 | protected virtual void DeregisterDataRowEvents(DataRow row) {
|
---|
85 | row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
86 | row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
87 | row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
88 | row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
89 | row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
90 | valuesRowsTable.Remove(row.Values);
|
---|
91 | row.VisualPropertiesChanged -= new EventHandler(Row_VisualPropertiesChanged);
|
---|
92 | row.NameChanged -= new EventHandler(Row_NameChanged);
|
---|
93 | }
|
---|
94 | #endregion
|
---|
95 |
|
---|
96 | protected override void OnContentChanged() {
|
---|
97 | base.OnContentChanged();
|
---|
98 | invisibleSeries.Clear();
|
---|
99 | chart.Titles[0].Text = string.Empty;
|
---|
100 | chart.ChartAreas[0].AxisX.Title = string.Empty;
|
---|
101 | chart.ChartAreas[0].AxisY.Title = string.Empty;
|
---|
102 | chart.ChartAreas[0].AxisY2.Title = string.Empty;
|
---|
103 | chart.Series.Clear();
|
---|
104 | if (Content != null) {
|
---|
105 | chart.Titles[0].Text = Content.Name;
|
---|
106 | foreach (DataRow row in Content.Rows)
|
---|
107 | AddDataRow(row);
|
---|
108 | ConfigureChartArea(chart.ChartAreas[0]);
|
---|
109 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected override void SetEnabledStateOfControls() {
|
---|
114 | base.SetEnabledStateOfControls();
|
---|
115 | chart.Enabled = Content != null;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void ShowConfiguration() {
|
---|
119 | if (Content != null) {
|
---|
120 | using (DataTableVisualPropertiesDialog dialog = new DataTableVisualPropertiesDialog(Content)) {
|
---|
121 | dialog.ShowDialog(this);
|
---|
122 | }
|
---|
123 | } else MessageBox.Show("Nothing to configure.");
|
---|
124 | }
|
---|
125 |
|
---|
126 | protected virtual void AddDataRow(DataRow row) {
|
---|
127 | Series series = new Series(row.Name);
|
---|
128 | if (row.VisualProperties.DisplayName.Trim() != String.Empty) series.LegendText = row.VisualProperties.DisplayName;
|
---|
129 | else series.LegendText = row.Name;
|
---|
130 | ConfigureSeries(series, row);
|
---|
131 | FillSeriesWithRowValues(series, row);
|
---|
132 |
|
---|
133 | chart.Series.Add(series);
|
---|
134 | ConfigureChartArea(chart.ChartAreas[0]);
|
---|
135 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
136 | UpdateYCursorInterval();
|
---|
137 | }
|
---|
138 |
|
---|
139 | private void ConfigureSeries(Series series, DataRow row) {
|
---|
140 | RemoveCustomPropertyIfExists(series, "PointWidth");
|
---|
141 | series.BorderWidth = 1;
|
---|
142 | series.BorderDashStyle = ChartDashStyle.Solid;
|
---|
143 | series.BorderColor = Color.Empty;
|
---|
144 |
|
---|
145 | if (row.VisualProperties.Color != Color.Empty)
|
---|
146 | series.Color = row.VisualProperties.Color;
|
---|
147 | else series.Color = Color.Empty;
|
---|
148 | series.IsVisibleInLegend = row.VisualProperties.IsVisibleInLegend;
|
---|
149 |
|
---|
150 | switch (row.VisualProperties.ChartType) {
|
---|
151 | case DataRowVisualProperties.DataRowChartType.Line:
|
---|
152 | series.ChartType = SeriesChartType.FastLine;
|
---|
153 | series.BorderWidth = row.VisualProperties.LineWidth;
|
---|
154 | series.BorderDashStyle = ConvertLineStyle(row.VisualProperties.LineStyle);
|
---|
155 | break;
|
---|
156 | case DataRowVisualProperties.DataRowChartType.Bars:
|
---|
157 | // Bar is incompatible with anything but Bar and StackedBar*
|
---|
158 | if (!chart.Series.Any(x => x.ChartType != SeriesChartType.Bar && x.ChartType != SeriesChartType.StackedBar && x.ChartType != SeriesChartType.StackedBar100))
|
---|
159 | series.ChartType = SeriesChartType.Bar;
|
---|
160 | else {
|
---|
161 | series.ChartType = SeriesChartType.FastPoint; //default
|
---|
162 | row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
|
---|
163 | }
|
---|
164 | break;
|
---|
165 | case DataRowVisualProperties.DataRowChartType.Columns:
|
---|
166 | series.ChartType = SeriesChartType.Column;
|
---|
167 | break;
|
---|
168 | case DataRowVisualProperties.DataRowChartType.Points:
|
---|
169 | series.ChartType = SeriesChartType.FastPoint;
|
---|
170 | break;
|
---|
171 | case DataRowVisualProperties.DataRowChartType.Histogram:
|
---|
172 | series.ChartType = SeriesChartType.Column;
|
---|
173 | series.SetCustomProperty("PointWidth", "1");
|
---|
174 | if (!series.Color.IsEmpty && series.Color.GetBrightness() < 0.25)
|
---|
175 | series.BorderColor = Color.White;
|
---|
176 | else series.BorderColor = Color.Black;
|
---|
177 | break;
|
---|
178 | default:
|
---|
179 | series.ChartType = SeriesChartType.FastPoint;
|
---|
180 | break;
|
---|
181 | }
|
---|
182 | series.YAxisType = row.VisualProperties.SecondYAxis ? AxisType.Secondary : AxisType.Primary;
|
---|
183 | series.XAxisType = row.VisualProperties.SecondXAxis ? AxisType.Secondary : AxisType.Primary;
|
---|
184 | if (row.VisualProperties.DisplayName.Trim() != String.Empty) series.LegendText = row.VisualProperties.DisplayName;
|
---|
185 | else series.LegendText = row.Name;
|
---|
186 | series.ToolTip = series.LegendText + " X = #INDEX, Y = #VAL";
|
---|
187 | }
|
---|
188 |
|
---|
189 | private void ConfigureChartArea(ChartArea area) {
|
---|
190 | if (Content.VisualProperties.TitleFont != null) chart.Titles[0].Font = Content.VisualProperties.TitleFont;
|
---|
191 | if (!Content.VisualProperties.TitleColor.IsEmpty) chart.Titles[0].ForeColor = Content.VisualProperties.TitleColor;
|
---|
192 | chart.Titles[0].Text = Content.VisualProperties.Title;
|
---|
193 |
|
---|
194 | if (Content.VisualProperties.AxisTitleFont != null) area.AxisX.TitleFont = Content.VisualProperties.AxisTitleFont;
|
---|
195 | if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisX.TitleForeColor = Content.VisualProperties.AxisTitleColor;
|
---|
196 | area.AxisX.Title = Content.VisualProperties.XAxisTitle;
|
---|
197 |
|
---|
198 | if (Content.VisualProperties.AxisTitleFont != null) area.AxisX2.TitleFont = Content.VisualProperties.AxisTitleFont;
|
---|
199 | if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisX2.TitleForeColor = Content.VisualProperties.AxisTitleColor;
|
---|
200 | area.AxisX2.Title = Content.VisualProperties.SecondXAxisTitle;
|
---|
201 |
|
---|
202 | if (Content.VisualProperties.AxisTitleFont != null) area.AxisY.TitleFont = Content.VisualProperties.AxisTitleFont;
|
---|
203 | if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisY.TitleForeColor = Content.VisualProperties.AxisTitleColor;
|
---|
204 | area.AxisY.Title = Content.VisualProperties.YAxisTitle;
|
---|
205 |
|
---|
206 | if (Content.VisualProperties.AxisTitleFont != null) area.AxisY2.TitleFont = Content.VisualProperties.AxisTitleFont;
|
---|
207 | if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisY2.TitleForeColor = Content.VisualProperties.AxisTitleColor;
|
---|
208 | area.AxisY2.Title = Content.VisualProperties.SecondYAxisTitle;
|
---|
209 | }
|
---|
210 |
|
---|
211 | private void RecalculateAxesScale(ChartArea area) {
|
---|
212 | // Reset the axes bounds so that RecalculateAxesScale() will assign new bounds
|
---|
213 | foreach (Axis a in area.Axes) {
|
---|
214 | a.Minimum = double.NaN;
|
---|
215 | a.Maximum = double.NaN;
|
---|
216 | }
|
---|
217 | area.RecalculateAxesScale();
|
---|
218 | area.AxisX.IsMarginVisible = false;
|
---|
219 | area.AxisX2.IsMarginVisible = false;
|
---|
220 |
|
---|
221 | if (!Content.VisualProperties.XAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.XAxisMinimumFixedValue)) area.AxisX.Minimum = Content.VisualProperties.XAxisMinimumFixedValue;
|
---|
222 | if (!Content.VisualProperties.XAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.XAxisMaximumFixedValue)) area.AxisX.Maximum = Content.VisualProperties.XAxisMaximumFixedValue;
|
---|
223 | if (!Content.VisualProperties.SecondXAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.SecondXAxisMinimumFixedValue)) area.AxisX2.Minimum = Content.VisualProperties.SecondXAxisMinimumFixedValue;
|
---|
224 | if (!Content.VisualProperties.SecondXAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.SecondXAxisMaximumFixedValue)) area.AxisX2.Maximum = Content.VisualProperties.SecondXAxisMaximumFixedValue;
|
---|
225 | if (!Content.VisualProperties.YAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.YAxisMinimumFixedValue)) area.AxisY.Minimum = Content.VisualProperties.YAxisMinimumFixedValue;
|
---|
226 | if (!Content.VisualProperties.YAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.YAxisMaximumFixedValue)) area.AxisY.Maximum = Content.VisualProperties.YAxisMaximumFixedValue;
|
---|
227 | if (!Content.VisualProperties.SecondYAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.SecondYAxisMinimumFixedValue)) area.AxisY2.Minimum = Content.VisualProperties.SecondYAxisMinimumFixedValue;
|
---|
228 | if (!Content.VisualProperties.SecondYAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.SecondYAxisMaximumFixedValue)) area.AxisY2.Maximum = Content.VisualProperties.SecondYAxisMaximumFixedValue;
|
---|
229 | if (area.AxisX.Minimum >= area.AxisX.Maximum) area.AxisX.Maximum = area.AxisX.Minimum + 1;
|
---|
230 | if (area.AxisX2.Minimum >= area.AxisX2.Maximum) area.AxisX2.Maximum = area.AxisX2.Minimum + 1;
|
---|
231 | if (area.AxisY.Minimum >= area.AxisY.Maximum) area.AxisY.Maximum = area.AxisY.Minimum + 1;
|
---|
232 | if (area.AxisY2.Minimum >= area.AxisY2.Maximum) area.AxisY2.Maximum = area.AxisY2.Minimum + 1;
|
---|
233 | }
|
---|
234 |
|
---|
235 | protected virtual void UpdateYCursorInterval() {
|
---|
236 | double interestingValuesRange = (
|
---|
237 | from series in chart.Series
|
---|
238 | where series.Enabled
|
---|
239 | let values = (from point in series.Points
|
---|
240 | where !point.IsEmpty
|
---|
241 | select point.YValues[0]).DefaultIfEmpty(1.0)
|
---|
242 | let range = values.Max() - values.Min()
|
---|
243 | where range > 0.0
|
---|
244 | select range
|
---|
245 | ).DefaultIfEmpty(1.0).Min();
|
---|
246 |
|
---|
247 | double digits = (int)Math.Log10(interestingValuesRange) - 3;
|
---|
248 | double yZoomInterval = Math.Pow(10, digits);
|
---|
249 | this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | protected virtual void RemoveDataRow(DataRow row) {
|
---|
254 | Series series = chart.Series[row.Name];
|
---|
255 | chart.Series.Remove(series);
|
---|
256 | if (invisibleSeries.Contains(series))
|
---|
257 | invisibleSeries.Remove(series);
|
---|
258 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
259 | }
|
---|
260 |
|
---|
261 | #region Event Handlers
|
---|
262 | #region Content Event Handlers
|
---|
263 | protected override void Content_NameChanged(object sender, EventArgs e) {
|
---|
264 | if (InvokeRequired)
|
---|
265 | Invoke(new EventHandler(Content_NameChanged), sender, e);
|
---|
266 | else {
|
---|
267 | chart.Titles[0].Text = Content.Name;
|
---|
268 | base.Content_NameChanged(sender, e);
|
---|
269 | }
|
---|
270 | }
|
---|
271 | private void Content_VisualPropertiesChanged(object sender, EventArgs e) {
|
---|
272 | if (InvokeRequired)
|
---|
273 | Invoke(new EventHandler(Content_VisualPropertiesChanged), sender, e);
|
---|
274 | else {
|
---|
275 | ConfigureChartArea(chart.ChartAreas[0]);
|
---|
276 | RecalculateAxesScale(chart.ChartAreas[0]); // axes min/max could have changed
|
---|
277 | }
|
---|
278 | }
|
---|
279 | #endregion
|
---|
280 | #region Rows Event Handlers
|
---|
281 | private void Rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
282 | if (InvokeRequired)
|
---|
283 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded), sender, e);
|
---|
284 | else {
|
---|
285 | foreach (DataRow row in e.Items) {
|
---|
286 | AddDataRow(row);
|
---|
287 | RegisterDataRowEvents(row);
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 | private void Rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
292 | if (InvokeRequired)
|
---|
293 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved), sender, e);
|
---|
294 | else {
|
---|
295 | foreach (DataRow row in e.Items) {
|
---|
296 | DeregisterDataRowEvents(row);
|
---|
297 | RemoveDataRow(row);
|
---|
298 | }
|
---|
299 | }
|
---|
300 | }
|
---|
301 | private void Rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
302 | if (InvokeRequired)
|
---|
303 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced), sender, e);
|
---|
304 | else {
|
---|
305 | foreach (DataRow row in e.OldItems) {
|
---|
306 | DeregisterDataRowEvents(row);
|
---|
307 | RemoveDataRow(row);
|
---|
308 | }
|
---|
309 | foreach (DataRow row in e.Items) {
|
---|
310 | AddDataRow(row);
|
---|
311 | RegisterDataRowEvents(row);
|
---|
312 | }
|
---|
313 | }
|
---|
314 | }
|
---|
315 | private void Rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
316 | if (InvokeRequired)
|
---|
317 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset), sender, e);
|
---|
318 | else {
|
---|
319 | foreach (DataRow row in e.OldItems) {
|
---|
320 | DeregisterDataRowEvents(row);
|
---|
321 | RemoveDataRow(row);
|
---|
322 | }
|
---|
323 | foreach (DataRow row in e.Items) {
|
---|
324 | AddDataRow(row);
|
---|
325 | RegisterDataRowEvents(row);
|
---|
326 | }
|
---|
327 | }
|
---|
328 | }
|
---|
329 | #endregion
|
---|
330 | #region Row Event Handlers
|
---|
331 | private void Row_VisualPropertiesChanged(object sender, EventArgs e) {
|
---|
332 | if (InvokeRequired)
|
---|
333 | Invoke(new EventHandler(Row_VisualPropertiesChanged), sender, e);
|
---|
334 | else {
|
---|
335 | DataRow row = (DataRow)sender;
|
---|
336 | Series series = chart.Series[row.Name];
|
---|
337 | series.Points.Clear();
|
---|
338 | ConfigureSeries(series, row);
|
---|
339 | FillSeriesWithRowValues(series, row);
|
---|
340 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
341 | }
|
---|
342 | }
|
---|
343 | private void Row_NameChanged(object sender, EventArgs e) {
|
---|
344 | if (InvokeRequired)
|
---|
345 | Invoke(new EventHandler(Row_NameChanged), sender, e);
|
---|
346 | else {
|
---|
347 | DataRow row = (DataRow)sender;
|
---|
348 | chart.Series[row.Name].Name = row.Name;
|
---|
349 | }
|
---|
350 | }
|
---|
351 | #endregion
|
---|
352 | #region Values Event Handlers
|
---|
353 | private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
354 | if (InvokeRequired)
|
---|
355 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded), sender, e);
|
---|
356 | else {
|
---|
357 | DataRow row = null;
|
---|
358 | valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
|
---|
359 | if (row != null) {
|
---|
360 | Series rowSeries = chart.Series[row.Name];
|
---|
361 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
362 | rowSeries.Points.Clear();
|
---|
363 | FillSeriesWithRowValues(rowSeries, row);
|
---|
364 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
365 | UpdateYCursorInterval();
|
---|
366 | }
|
---|
367 | }
|
---|
368 | }
|
---|
369 | }
|
---|
370 | private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
371 | if (InvokeRequired)
|
---|
372 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved), sender, e);
|
---|
373 | else {
|
---|
374 | DataRow row = null;
|
---|
375 | valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
|
---|
376 | if (row != null) {
|
---|
377 | Series rowSeries = chart.Series[row.Name];
|
---|
378 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
379 | rowSeries.Points.Clear();
|
---|
380 | FillSeriesWithRowValues(rowSeries, row);
|
---|
381 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
382 | UpdateYCursorInterval();
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|
386 | }
|
---|
387 | private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
388 | if (InvokeRequired)
|
---|
389 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced), sender, e);
|
---|
390 | else {
|
---|
391 | DataRow row = null;
|
---|
392 | valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
|
---|
393 | if (row != null) {
|
---|
394 | Series rowSeries = chart.Series[row.Name];
|
---|
395 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
396 | if (row.VisualProperties.ChartType == DataRowVisualProperties.DataRowChartType.Histogram) {
|
---|
397 | rowSeries.Points.Clear();
|
---|
398 | FillSeriesWithRowValues(rowSeries, row);
|
---|
399 | } else {
|
---|
400 | foreach (IndexedItem<double> item in e.Items) {
|
---|
401 | if (IsInvalidValue(item.Value))
|
---|
402 | rowSeries.Points[item.Index].IsEmpty = true;
|
---|
403 | else {
|
---|
404 | rowSeries.Points[item.Index].YValues = new double[] { item.Value };
|
---|
405 | rowSeries.Points[item.Index].IsEmpty = false;
|
---|
406 | }
|
---|
407 | }
|
---|
408 | }
|
---|
409 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
410 | UpdateYCursorInterval();
|
---|
411 | }
|
---|
412 | }
|
---|
413 | }
|
---|
414 | }
|
---|
415 | private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
416 | if (InvokeRequired)
|
---|
417 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved), sender, e);
|
---|
418 | else {
|
---|
419 | DataRow row = null;
|
---|
420 | valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
|
---|
421 | if (row != null) {
|
---|
422 | Series rowSeries = chart.Series[row.Name];
|
---|
423 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
424 | rowSeries.Points.Clear();
|
---|
425 | FillSeriesWithRowValues(rowSeries, row);
|
---|
426 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
427 | UpdateYCursorInterval();
|
---|
428 | }
|
---|
429 | }
|
---|
430 | }
|
---|
431 | }
|
---|
432 |
|
---|
433 | private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
434 | if (InvokeRequired)
|
---|
435 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset), sender, e);
|
---|
436 | else {
|
---|
437 | DataRow row = null;
|
---|
438 | valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
|
---|
439 | if (row != null) {
|
---|
440 | Series rowSeries = chart.Series[row.Name];
|
---|
441 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
442 | rowSeries.Points.Clear();
|
---|
443 | FillSeriesWithRowValues(rowSeries, row);
|
---|
444 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
445 | UpdateYCursorInterval();
|
---|
446 | }
|
---|
447 | }
|
---|
448 | }
|
---|
449 | }
|
---|
450 | #endregion
|
---|
451 | #endregion
|
---|
452 |
|
---|
453 | #region Chart Event Handlers
|
---|
454 | private void chart_MouseDown(object sender, MouseEventArgs e) {
|
---|
455 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
456 | if (result.ChartElementType == ChartElementType.LegendItem) {
|
---|
457 | ToggleSeriesVisible(result.Series);
|
---|
458 | }
|
---|
459 | }
|
---|
460 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
461 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
462 | if (result.ChartElementType == ChartElementType.LegendItem)
|
---|
463 | this.Cursor = Cursors.Hand;
|
---|
464 | else
|
---|
465 | this.Cursor = Cursors.Default;
|
---|
466 | }
|
---|
467 | private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
|
---|
468 | foreach (LegendItem legendItem in e.LegendItems) {
|
---|
469 | var series = chart.Series[legendItem.SeriesName];
|
---|
470 | if (series != null) {
|
---|
471 | bool seriesIsInvisible = invisibleSeries.Contains(series);
|
---|
472 | foreach (LegendCell cell in legendItem.Cells) {
|
---|
473 | cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black;
|
---|
474 | }
|
---|
475 | }
|
---|
476 | }
|
---|
477 | }
|
---|
478 | #endregion
|
---|
479 |
|
---|
480 | private void ToggleSeriesVisible(Series series) {
|
---|
481 | if (!invisibleSeries.Contains(series)) {
|
---|
482 | series.Points.Clear();
|
---|
483 | invisibleSeries.Add(series);
|
---|
484 | } else {
|
---|
485 | invisibleSeries.Remove(series);
|
---|
486 | if (Content != null) {
|
---|
487 |
|
---|
488 | var row = (from r in Content.Rows
|
---|
489 | where r.Name == series.Name
|
---|
490 | select r).Single();
|
---|
491 | FillSeriesWithRowValues(series, row);
|
---|
492 | this.chart.Legends[series.Legend].ForeColor = Color.Black;
|
---|
493 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
494 | UpdateYCursorInterval();
|
---|
495 | }
|
---|
496 | }
|
---|
497 | }
|
---|
498 |
|
---|
499 | private void FillSeriesWithRowValues(Series series, DataRow row) {
|
---|
500 | switch (row.VisualProperties.ChartType) {
|
---|
501 | case DataRowVisualProperties.DataRowChartType.Histogram:
|
---|
502 | CalculateHistogram(series, row);
|
---|
503 | break;
|
---|
504 | default: {
|
---|
505 | for (int i = 0; i < row.Values.Count; i++) {
|
---|
506 | var value = row.Values[i];
|
---|
507 | DataPoint point = new DataPoint();
|
---|
508 | point.XValue = row.VisualProperties.StartIndexZero ? i : i + 1;
|
---|
509 | if (IsInvalidValue(value))
|
---|
510 | point.IsEmpty = true;
|
---|
511 | else
|
---|
512 | point.YValues = new double[] { value };
|
---|
513 | series.Points.Add(point);
|
---|
514 | }
|
---|
515 | }
|
---|
516 | break;
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | protected virtual void CalculateHistogram(Series series, DataRow row) {
|
---|
521 | series.Points.Clear();
|
---|
522 | if (!row.Values.Any()) return;
|
---|
523 | int bins = row.VisualProperties.Bins;
|
---|
524 |
|
---|
525 | double minValue = row.Values.Min();
|
---|
526 | double maxValue = row.Values.Max();
|
---|
527 | double intervalWidth = (maxValue - minValue) / bins;
|
---|
528 | if (intervalWidth < 0) return;
|
---|
529 | if (intervalWidth == 0) {
|
---|
530 | series.Points.AddXY(minValue, row.Values.Count);
|
---|
531 | return;
|
---|
532 | }
|
---|
533 |
|
---|
534 | if (!row.VisualProperties.ExactBins) {
|
---|
535 | intervalWidth = HumanRoundRange(intervalWidth);
|
---|
536 | minValue = Math.Floor(minValue / intervalWidth) * intervalWidth;
|
---|
537 | maxValue = Math.Ceiling(maxValue / intervalWidth) * intervalWidth;
|
---|
538 | }
|
---|
539 |
|
---|
540 | double intervalCenter = intervalWidth / 2;
|
---|
541 |
|
---|
542 | double min = 0.0;
|
---|
543 | if (!Double.IsNaN(Content.VisualProperties.XAxisMinimumFixedValue) && !Content.VisualProperties.XAxisMinimumAuto)
|
---|
544 | min = Content.VisualProperties.XAxisMinimumFixedValue;
|
---|
545 | else min = minValue;
|
---|
546 |
|
---|
547 | double axisInterval = intervalWidth / row.VisualProperties.ScaleFactor;
|
---|
548 |
|
---|
549 | var area = chart.ChartAreas[0];
|
---|
550 | area.AxisX.Interval = axisInterval;
|
---|
551 |
|
---|
552 | series.SetCustomProperty("PointWidth", "1"); // 0.8 is the default value
|
---|
553 |
|
---|
554 | // get the range or intervals which define the grouping of the frequency values
|
---|
555 | var doubleRange = DoubleRange(min, maxValue + intervalWidth, intervalWidth).Skip(1).ToList();
|
---|
556 |
|
---|
557 | // aggregate the row values by unique key and frequency value
|
---|
558 | var valueFrequencies = (from v in row.Values
|
---|
559 | where !IsInvalidValue(v)
|
---|
560 | orderby v
|
---|
561 | group v by v into g
|
---|
562 | select new Tuple<double, double>(g.First(), g.Count())).ToList();
|
---|
563 |
|
---|
564 | // shift the chart to the left so the bars are placed on the intervals
|
---|
565 | if (valueFrequencies.First().Item1 < doubleRange.First())
|
---|
566 | series.Points.Add(new DataPoint(min - intervalWidth, 0));
|
---|
567 |
|
---|
568 | // add data points
|
---|
569 | int j = 0;
|
---|
570 | foreach (var d in doubleRange) {
|
---|
571 | double sum = 0.0;
|
---|
572 | // sum the frequency values that fall within the same interval
|
---|
573 | while (j < valueFrequencies.Count && valueFrequencies[j].Item1 < d) {
|
---|
574 | sum += valueFrequencies[j].Item2;
|
---|
575 | ++j;
|
---|
576 | }
|
---|
577 | series.Points.Add(new DataPoint(d - intervalCenter, sum) { ToolTip = "X: [" + (d - intervalWidth) + "-" + d + "), Y: " + sum });
|
---|
578 | }
|
---|
579 | }
|
---|
580 |
|
---|
581 | #region Helpers
|
---|
582 | public static IEnumerable<double> DoubleRange(double min, double max, double step) {
|
---|
583 | double i;
|
---|
584 | for (i = min; i <= max; i += step)
|
---|
585 | yield return i;
|
---|
586 |
|
---|
587 | if (i != max + step)
|
---|
588 | yield return i;
|
---|
589 | }
|
---|
590 |
|
---|
591 | protected void RemoveCustomPropertyIfExists(Series series, string property) {
|
---|
592 | if (series.IsCustomPropertySet(property)) series.DeleteCustomProperty(property);
|
---|
593 | }
|
---|
594 |
|
---|
595 | private double HumanRoundRange(double range) {
|
---|
596 | double base10 = Math.Pow(10.0, Math.Floor(Math.Log10(range)));
|
---|
597 | double rounding = range / base10;
|
---|
598 | if (rounding <= 1.5) rounding = 1;
|
---|
599 | else if (rounding <= 2.25) rounding = 2;
|
---|
600 | else if (rounding <= 3.75) rounding = 2.5;
|
---|
601 | else if (rounding <= 7.5) rounding = 5;
|
---|
602 | else rounding = 10;
|
---|
603 | return rounding * base10;
|
---|
604 | }
|
---|
605 |
|
---|
606 | private double HumanRoundMax(double max) {
|
---|
607 | double base10;
|
---|
608 | if (max > 0) base10 = Math.Pow(10.0, Math.Floor(Math.Log10(max)));
|
---|
609 | else base10 = Math.Pow(10.0, Math.Ceiling(Math.Log10(-max)));
|
---|
610 | double rounding = (max > 0) ? base10 : -base10;
|
---|
611 | while (rounding < max) rounding += base10;
|
---|
612 | return rounding;
|
---|
613 | }
|
---|
614 |
|
---|
615 | private ChartDashStyle ConvertLineStyle(DataRowVisualProperties.DataRowLineStyle dataRowLineStyle) {
|
---|
616 | switch (dataRowLineStyle) {
|
---|
617 | case DataRowVisualProperties.DataRowLineStyle.Dash:
|
---|
618 | return ChartDashStyle.Dash;
|
---|
619 | case DataRowVisualProperties.DataRowLineStyle.DashDot:
|
---|
620 | return ChartDashStyle.DashDot;
|
---|
621 | case DataRowVisualProperties.DataRowLineStyle.DashDotDot:
|
---|
622 | return ChartDashStyle.DashDotDot;
|
---|
623 | case DataRowVisualProperties.DataRowLineStyle.Dot:
|
---|
624 | return ChartDashStyle.Dot;
|
---|
625 | case DataRowVisualProperties.DataRowLineStyle.NotSet:
|
---|
626 | return ChartDashStyle.NotSet;
|
---|
627 | case DataRowVisualProperties.DataRowLineStyle.Solid:
|
---|
628 | return ChartDashStyle.Solid;
|
---|
629 | default:
|
---|
630 | return ChartDashStyle.NotSet;
|
---|
631 | }
|
---|
632 | }
|
---|
633 |
|
---|
634 | protected static bool IsInvalidValue(double x) {
|
---|
635 | return double.IsNaN(x) || x < (double)decimal.MinValue || x > (double)decimal.MaxValue;
|
---|
636 | }
|
---|
637 | #endregion
|
---|
638 | }
|
---|
639 | }
|
---|