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