1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
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.Common;
|
---|
30 | using HeuristicLab.Core.Views;
|
---|
31 | using HeuristicLab.MainForm;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Analysis.Views {
|
---|
34 | [View("ScatterPlot View")]
|
---|
35 | [Content(typeof(ScatterPlot), true)]
|
---|
36 | public partial class ScatterPlotView : NamedItemView, IConfigureableView {
|
---|
37 | protected List<Series> invisibleSeries;
|
---|
38 | protected Dictionary<IObservableList<Point2D<double>>, ScatterPlotDataRow> pointsRowsTable;
|
---|
39 | private double xMin, xMax, yMin, yMax;
|
---|
40 |
|
---|
41 | public new ScatterPlot Content {
|
---|
42 | get { return (ScatterPlot)base.Content; }
|
---|
43 | set { base.Content = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public ScatterPlotView() {
|
---|
47 | InitializeComponent();
|
---|
48 | pointsRowsTable = new Dictionary<IObservableList<Point2D<double>>, ScatterPlotDataRow>();
|
---|
49 | invisibleSeries = new List<Series>();
|
---|
50 | chart.CustomizeAllChartAreas();
|
---|
51 | chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | #region Event Handler Registration
|
---|
55 | protected override void DeregisterContentEvents() {
|
---|
56 | foreach (ScatterPlotDataRow row in Content.Rows)
|
---|
57 | DeregisterScatterPlotDataRowEvents(row);
|
---|
58 | Content.VisualPropertiesChanged -= new EventHandler(Content_VisualPropertiesChanged);
|
---|
59 | Content.Rows.ItemsAdded -= new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsAdded);
|
---|
60 | Content.Rows.ItemsRemoved -= new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsRemoved);
|
---|
61 | Content.Rows.ItemsReplaced -= new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsReplaced);
|
---|
62 | Content.Rows.CollectionReset -= new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_CollectionReset);
|
---|
63 | base.DeregisterContentEvents();
|
---|
64 | }
|
---|
65 | protected override void RegisterContentEvents() {
|
---|
66 | base.RegisterContentEvents();
|
---|
67 | Content.VisualPropertiesChanged += new EventHandler(Content_VisualPropertiesChanged);
|
---|
68 | Content.Rows.ItemsAdded += new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsAdded);
|
---|
69 | Content.Rows.ItemsRemoved += new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsRemoved);
|
---|
70 | Content.Rows.ItemsReplaced += new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsReplaced);
|
---|
71 | Content.Rows.CollectionReset += new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_CollectionReset);
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected virtual void RegisterScatterPlotDataRowEvents(ScatterPlotDataRow row) {
|
---|
75 | row.NameChanged += new EventHandler(Row_NameChanged);
|
---|
76 | row.VisualPropertiesChanged += new EventHandler(Row_VisualPropertiesChanged);
|
---|
77 | pointsRowsTable.Add(row.Points, row);
|
---|
78 | row.Points.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsAdded);
|
---|
79 | row.Points.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsRemoved);
|
---|
80 | row.Points.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsReplaced);
|
---|
81 | row.Points.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_CollectionReset);
|
---|
82 | }
|
---|
83 | protected virtual void DeregisterScatterPlotDataRowEvents(ScatterPlotDataRow row) {
|
---|
84 | row.Points.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsAdded);
|
---|
85 | row.Points.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsRemoved);
|
---|
86 | row.Points.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsReplaced);
|
---|
87 | row.Points.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_CollectionReset);
|
---|
88 | pointsRowsTable.Remove(row.Points);
|
---|
89 | row.VisualPropertiesChanged -= new EventHandler(Row_VisualPropertiesChanged);
|
---|
90 | row.NameChanged -= new EventHandler(Row_NameChanged);
|
---|
91 | }
|
---|
92 | #endregion
|
---|
93 |
|
---|
94 | protected override void OnContentChanged() {
|
---|
95 | base.OnContentChanged();
|
---|
96 | invisibleSeries.Clear();
|
---|
97 | chart.Titles[0].Text = string.Empty;
|
---|
98 | chart.ChartAreas[0].AxisX.Title = string.Empty;
|
---|
99 | chart.ChartAreas[0].AxisY.Title = string.Empty;
|
---|
100 | chart.Series.Clear();
|
---|
101 | if (Content != null) {
|
---|
102 | chart.Titles[0].Text = Content.Name;
|
---|
103 | AddScatterPlotDataRows(Content.Rows);
|
---|
104 | ConfigureChartArea(chart.ChartAreas[0]);
|
---|
105 | RecalculateMinMaxPointValues();
|
---|
106 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected override void SetEnabledStateOfControls() {
|
---|
111 | base.SetEnabledStateOfControls();
|
---|
112 | chart.Enabled = Content != null;
|
---|
113 | }
|
---|
114 |
|
---|
115 | public void ShowConfiguration() {
|
---|
116 | if (Content != null) {
|
---|
117 | using (ScatterPlotVisualPropertiesDialog dialog = new ScatterPlotVisualPropertiesDialog(Content)) {
|
---|
118 | dialog.ShowDialog(this);
|
---|
119 | }
|
---|
120 | } else MessageBox.Show("Nothing to configure.");
|
---|
121 | }
|
---|
122 |
|
---|
123 | protected virtual void AddScatterPlotDataRows(IEnumerable<ScatterPlotDataRow> rows) {
|
---|
124 | foreach (var row in rows) {
|
---|
125 | RegisterScatterPlotDataRowEvents(row);
|
---|
126 | Series series = new Series(row.Name);
|
---|
127 | if (row.VisualProperties.DisplayName.Trim() != String.Empty) series.LegendText = row.VisualProperties.DisplayName;
|
---|
128 | else series.LegendText = row.Name;
|
---|
129 | ConfigureSeries(series, row);
|
---|
130 | FillSeriesWithRowValues(series, row);
|
---|
131 | chart.Series.Add(series);
|
---|
132 | }
|
---|
133 | ConfigureChartArea(chart.ChartAreas[0]);
|
---|
134 | RecalculateMinMaxPointValues();
|
---|
135 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
136 | UpdateYCursorInterval();
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected virtual void RemoveScatterPlotDataRows(IEnumerable<ScatterPlotDataRow> rows) {
|
---|
140 | foreach (var row in rows) {
|
---|
141 | DeregisterScatterPlotDataRowEvents(row);
|
---|
142 | Series series = chart.Series[row.Name];
|
---|
143 | chart.Series.Remove(series);
|
---|
144 | if (invisibleSeries.Contains(series))
|
---|
145 | invisibleSeries.Remove(series);
|
---|
146 | }
|
---|
147 | RecalculateMinMaxPointValues();
|
---|
148 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
149 | }
|
---|
150 |
|
---|
151 | private void ConfigureSeries(Series series, ScatterPlotDataRow row) {
|
---|
152 | series.BorderWidth = 1;
|
---|
153 | series.BorderDashStyle = ChartDashStyle.Solid;
|
---|
154 | series.BorderColor = Color.Empty;
|
---|
155 |
|
---|
156 | if (row.VisualProperties.Color != Color.Empty)
|
---|
157 | series.Color = row.VisualProperties.Color;
|
---|
158 | else series.Color = Color.Empty;
|
---|
159 | series.IsVisibleInLegend = row.VisualProperties.IsVisibleInLegend;
|
---|
160 | series.ChartType = SeriesChartType.FastPoint;
|
---|
161 | series.MarkerSize = row.VisualProperties.PointSize;
|
---|
162 | series.MarkerStyle = ConvertPointStyle(row.VisualProperties.PointStyle);
|
---|
163 | series.XAxisType = AxisType.Primary;
|
---|
164 | series.YAxisType = AxisType.Primary;
|
---|
165 |
|
---|
166 | if (row.VisualProperties.DisplayName.Trim() != String.Empty) series.LegendText = row.VisualProperties.DisplayName;
|
---|
167 | else series.LegendText = row.Name;
|
---|
168 |
|
---|
169 | string xAxisTitle = string.IsNullOrEmpty(Content.VisualProperties.XAxisTitle)
|
---|
170 | ? "X"
|
---|
171 | : Content.VisualProperties.XAxisTitle;
|
---|
172 | string yAxisTitle = string.IsNullOrEmpty(Content.VisualProperties.YAxisTitle)
|
---|
173 | ? "Y"
|
---|
174 | : Content.VisualProperties.YAxisTitle;
|
---|
175 | series.ToolTip =
|
---|
176 | series.LegendText + Environment.NewLine +
|
---|
177 | xAxisTitle + " = " + "#VALX," + Environment.NewLine +
|
---|
178 | yAxisTitle + " = " + "#VAL";
|
---|
179 | }
|
---|
180 |
|
---|
181 | private void ConfigureChartArea(ChartArea area) {
|
---|
182 | if (Content.VisualProperties.TitleFont != null) chart.Titles[0].Font = Content.VisualProperties.TitleFont;
|
---|
183 | if (!Content.VisualProperties.TitleColor.IsEmpty) chart.Titles[0].ForeColor = Content.VisualProperties.TitleColor;
|
---|
184 | chart.Titles[0].Text = Content.VisualProperties.Title;
|
---|
185 |
|
---|
186 | if (Content.VisualProperties.AxisTitleFont != null) area.AxisX.TitleFont = Content.VisualProperties.AxisTitleFont;
|
---|
187 | if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisX.TitleForeColor = Content.VisualProperties.AxisTitleColor;
|
---|
188 | area.AxisX.Title = Content.VisualProperties.XAxisTitle;
|
---|
189 | area.AxisX.MajorGrid.Enabled = Content.VisualProperties.XAxisGrid;
|
---|
190 |
|
---|
191 | if (Content.VisualProperties.AxisTitleFont != null) area.AxisY.TitleFont = Content.VisualProperties.AxisTitleFont;
|
---|
192 | if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisY.TitleForeColor = Content.VisualProperties.AxisTitleColor;
|
---|
193 | area.AxisY.Title = Content.VisualProperties.YAxisTitle;
|
---|
194 | area.AxisY.MajorGrid.Enabled = Content.VisualProperties.YAxisGrid;
|
---|
195 | }
|
---|
196 |
|
---|
197 | private void RecalculateAxesScale(ChartArea area) {
|
---|
198 | // Reset the axes bounds so that RecalculateAxesScale() will assign new bounds
|
---|
199 | /*area.AxisX.Minimum = double.NaN;
|
---|
200 | area.AxisX.Maximum = double.NaN;
|
---|
201 | area.AxisY.Minimum = double.NaN;
|
---|
202 | area.AxisY.Maximum = double.NaN;*/
|
---|
203 | area.AxisX.Minimum = CalculateMinBound(xMin);
|
---|
204 | area.AxisX.Maximum = CalculateMaxBound(xMax);
|
---|
205 | if (area.AxisX.Minimum == area.AxisX.Maximum) {
|
---|
206 | area.AxisX.Minimum = xMin - 0.5;
|
---|
207 | area.AxisX.Maximum = xMax + 0.5;
|
---|
208 | }
|
---|
209 | area.AxisY.Minimum = CalculateMinBound(yMin);
|
---|
210 | area.AxisY.Maximum = CalculateMaxBound(yMax);
|
---|
211 | if (area.AxisY.Minimum == area.AxisY.Maximum) {
|
---|
212 | area.AxisY.Minimum = yMin - 0.5;
|
---|
213 | area.AxisY.Maximum = yMax + 0.5;
|
---|
214 | }
|
---|
215 | if (xMax - xMin > 0) area.CursorX.Interval = Math.Pow(10, Math.Floor(Math.Log10(area.AxisX.Maximum - area.AxisX.Minimum) - 3));
|
---|
216 | else area.CursorX.Interval = 1;
|
---|
217 | area.AxisX.IsMarginVisible = false;
|
---|
218 |
|
---|
219 | if (!Content.VisualProperties.XAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.XAxisMinimumFixedValue)) area.AxisX.Minimum = Content.VisualProperties.XAxisMinimumFixedValue;
|
---|
220 | if (!Content.VisualProperties.XAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.XAxisMaximumFixedValue)) area.AxisX.Maximum = Content.VisualProperties.XAxisMaximumFixedValue;
|
---|
221 | if (!Content.VisualProperties.YAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.YAxisMinimumFixedValue)) area.AxisY.Minimum = Content.VisualProperties.YAxisMinimumFixedValue;
|
---|
222 | if (!Content.VisualProperties.YAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.YAxisMaximumFixedValue)) area.AxisY.Maximum = Content.VisualProperties.YAxisMaximumFixedValue;
|
---|
223 | }
|
---|
224 |
|
---|
225 | private static double CalculateMinBound(double min) {
|
---|
226 | if (min == 0) return 0;
|
---|
227 | var scale = Math.Pow(10, Math.Floor(Math.Log10(Math.Abs(min))));
|
---|
228 | return scale * (Math.Floor(min / scale));
|
---|
229 | }
|
---|
230 |
|
---|
231 | private static double CalculateMaxBound(double max) {
|
---|
232 | if (max == 0) return 0;
|
---|
233 | var scale = Math.Pow(10, Math.Floor(Math.Log10(Math.Abs(max))));
|
---|
234 | return scale * (Math.Ceiling(max / scale));
|
---|
235 | }
|
---|
236 |
|
---|
237 | protected virtual void UpdateYCursorInterval() {
|
---|
238 | double interestingValuesRange = (
|
---|
239 | from series in chart.Series
|
---|
240 | where series.Enabled
|
---|
241 | let values = (from point in series.Points
|
---|
242 | where !point.IsEmpty
|
---|
243 | select point.YValues[0]).DefaultIfEmpty(1.0)
|
---|
244 | let range = values.Max() - values.Min()
|
---|
245 | where range > 0.0
|
---|
246 | select range
|
---|
247 | ).DefaultIfEmpty(1.0).Min();
|
---|
248 |
|
---|
249 | double digits = (int)Math.Log10(interestingValuesRange) - 3;
|
---|
250 | double yZoomInterval = Math.Pow(10, digits);
|
---|
251 | this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
|
---|
252 | }
|
---|
253 |
|
---|
254 | #region Event Handlers
|
---|
255 | #region Content Event Handlers
|
---|
256 | protected override void Content_NameChanged(object sender, EventArgs e) {
|
---|
257 | if (InvokeRequired)
|
---|
258 | Invoke(new EventHandler(Content_NameChanged), sender, e);
|
---|
259 | else {
|
---|
260 | chart.Titles[0].Text = Content.Name;
|
---|
261 | base.Content_NameChanged(sender, e);
|
---|
262 | }
|
---|
263 | }
|
---|
264 | private void Content_VisualPropertiesChanged(object sender, EventArgs e) {
|
---|
265 | if (InvokeRequired)
|
---|
266 | Invoke(new EventHandler(Content_VisualPropertiesChanged), sender, e);
|
---|
267 | else {
|
---|
268 | ConfigureChartArea(chart.ChartAreas[0]);
|
---|
269 | RecalculateAxesScale(chart.ChartAreas[0]); // axes min/max could have changed
|
---|
270 | }
|
---|
271 | }
|
---|
272 | #endregion
|
---|
273 | #region Rows Event Handlers
|
---|
274 | private void Rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<ScatterPlotDataRow> e) {
|
---|
275 | if (InvokeRequired)
|
---|
276 | Invoke(new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsAdded), sender, e);
|
---|
277 | else {
|
---|
278 | AddScatterPlotDataRows(e.Items);
|
---|
279 | }
|
---|
280 | }
|
---|
281 | private void Rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ScatterPlotDataRow> e) {
|
---|
282 | if (InvokeRequired)
|
---|
283 | Invoke(new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsRemoved), sender, e);
|
---|
284 | else {
|
---|
285 | RemoveScatterPlotDataRows(e.Items);
|
---|
286 | }
|
---|
287 | }
|
---|
288 | private void Rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<ScatterPlotDataRow> e) {
|
---|
289 | if (InvokeRequired)
|
---|
290 | Invoke(new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsReplaced), sender, e);
|
---|
291 | else {
|
---|
292 | RemoveScatterPlotDataRows(e.OldItems);
|
---|
293 | AddScatterPlotDataRows(e.Items);
|
---|
294 | }
|
---|
295 | }
|
---|
296 | private void Rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<ScatterPlotDataRow> e) {
|
---|
297 | if (InvokeRequired)
|
---|
298 | Invoke(new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_CollectionReset), sender, e);
|
---|
299 | else {
|
---|
300 | RemoveScatterPlotDataRows(e.OldItems);
|
---|
301 | AddScatterPlotDataRows(e.Items);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | #endregion
|
---|
305 | #region Row Event Handlers
|
---|
306 | private void Row_VisualPropertiesChanged(object sender, EventArgs e) {
|
---|
307 | if (InvokeRequired)
|
---|
308 | Invoke(new EventHandler(Row_VisualPropertiesChanged), sender, e);
|
---|
309 | else {
|
---|
310 | ScatterPlotDataRow row = (ScatterPlotDataRow)sender;
|
---|
311 | Series series = chart.Series[row.Name];
|
---|
312 | series.Points.Clear();
|
---|
313 | ConfigureSeries(series, row);
|
---|
314 | FillSeriesWithRowValues(series, row);
|
---|
315 | RecalculateMinMaxPointValues();
|
---|
316 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
317 | }
|
---|
318 | }
|
---|
319 | private void Row_NameChanged(object sender, EventArgs e) {
|
---|
320 | if (InvokeRequired)
|
---|
321 | Invoke(new EventHandler(Row_NameChanged), sender, e);
|
---|
322 | else {
|
---|
323 | ScatterPlotDataRow row = (ScatterPlotDataRow)sender;
|
---|
324 | chart.Series[row.Name].Name = row.Name;
|
---|
325 | }
|
---|
326 | }
|
---|
327 | #endregion
|
---|
328 | #region Points Event Handlers
|
---|
329 | private void Points_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
|
---|
330 | if (InvokeRequired)
|
---|
331 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsAdded), sender, e);
|
---|
332 | else {
|
---|
333 | ScatterPlotDataRow row = null;
|
---|
334 | pointsRowsTable.TryGetValue((IObservableList<Point2D<double>>)sender, out row);
|
---|
335 | if (row != null) {
|
---|
336 | Series rowSeries = chart.Series[row.Name];
|
---|
337 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
338 | rowSeries.Points.Clear();
|
---|
339 | FillSeriesWithRowValues(rowSeries, row);
|
---|
340 | RecalculateMinMaxPointValues();
|
---|
341 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
342 | UpdateYCursorInterval();
|
---|
343 | }
|
---|
344 | }
|
---|
345 | }
|
---|
346 | }
|
---|
347 | private void Points_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
|
---|
348 | if (InvokeRequired)
|
---|
349 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsRemoved), sender, e);
|
---|
350 | else {
|
---|
351 | ScatterPlotDataRow row = null;
|
---|
352 | pointsRowsTable.TryGetValue((IObservableList<Point2D<double>>)sender, out row);
|
---|
353 | if (row != null) {
|
---|
354 | Series rowSeries = chart.Series[row.Name];
|
---|
355 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
356 | rowSeries.Points.Clear();
|
---|
357 | FillSeriesWithRowValues(rowSeries, row);
|
---|
358 | RecalculateMinMaxPointValues();
|
---|
359 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
360 | UpdateYCursorInterval();
|
---|
361 | }
|
---|
362 | }
|
---|
363 | }
|
---|
364 | }
|
---|
365 | private void Points_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
|
---|
366 | if (InvokeRequired)
|
---|
367 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsReplaced), sender, e);
|
---|
368 | else {
|
---|
369 | ScatterPlotDataRow row = null;
|
---|
370 | pointsRowsTable.TryGetValue((IObservableList<Point2D<double>>)sender, out row);
|
---|
371 | if (row != null) {
|
---|
372 | Series rowSeries = chart.Series[row.Name];
|
---|
373 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
374 | rowSeries.Points.Clear();
|
---|
375 | FillSeriesWithRowValues(rowSeries, row);
|
---|
376 | RecalculateMinMaxPointValues();
|
---|
377 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
378 | UpdateYCursorInterval();
|
---|
379 | }
|
---|
380 | }
|
---|
381 | }
|
---|
382 | }
|
---|
383 | private void Points_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
|
---|
384 | if (InvokeRequired)
|
---|
385 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_CollectionReset), sender, e);
|
---|
386 | else {
|
---|
387 | ScatterPlotDataRow row = null;
|
---|
388 | pointsRowsTable.TryGetValue((IObservableList<Point2D<double>>)sender, out row);
|
---|
389 | if (row != null) {
|
---|
390 | Series rowSeries = chart.Series[row.Name];
|
---|
391 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
392 | rowSeries.Points.Clear();
|
---|
393 | FillSeriesWithRowValues(rowSeries, row);
|
---|
394 | RecalculateMinMaxPointValues();
|
---|
395 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
396 | UpdateYCursorInterval();
|
---|
397 | }
|
---|
398 | }
|
---|
399 | }
|
---|
400 | }
|
---|
401 | #endregion
|
---|
402 | #endregion
|
---|
403 |
|
---|
404 | #region Chart Event Handlers
|
---|
405 | private void chart_MouseDown(object sender, MouseEventArgs e) {
|
---|
406 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
407 | if (result.ChartElementType == ChartElementType.LegendItem) {
|
---|
408 | ToggleSeriesVisible(result.Series);
|
---|
409 | }
|
---|
410 | }
|
---|
411 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
412 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
413 | if (result.ChartElementType == ChartElementType.LegendItem)
|
---|
414 | this.Cursor = Cursors.Hand;
|
---|
415 | else
|
---|
416 | this.Cursor = Cursors.Default;
|
---|
417 | }
|
---|
418 | private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
|
---|
419 | foreach (LegendItem legendItem in e.LegendItems) {
|
---|
420 | var series = chart.Series[legendItem.SeriesName];
|
---|
421 | if (series != null) {
|
---|
422 | bool seriesIsInvisible = invisibleSeries.Contains(series);
|
---|
423 | foreach (LegendCell cell in legendItem.Cells) {
|
---|
424 | cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black;
|
---|
425 | }
|
---|
426 | }
|
---|
427 | }
|
---|
428 | }
|
---|
429 | #endregion
|
---|
430 |
|
---|
431 | private void ToggleSeriesVisible(Series series) {
|
---|
432 | if (!invisibleSeries.Contains(series)) {
|
---|
433 | series.Points.Clear();
|
---|
434 | invisibleSeries.Add(series);
|
---|
435 | RecalculateMinMaxPointValues();
|
---|
436 | } else {
|
---|
437 | invisibleSeries.Remove(series);
|
---|
438 | if (Content != null) {
|
---|
439 |
|
---|
440 | var row = (from r in Content.Rows
|
---|
441 | where r.Name == series.Name
|
---|
442 | select r).Single();
|
---|
443 | FillSeriesWithRowValues(series, row);
|
---|
444 | RecalculateMinMaxPointValues();
|
---|
445 | this.chart.Legends[series.Legend].ForeColor = Color.Black;
|
---|
446 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
447 | UpdateYCursorInterval();
|
---|
448 | }
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | private void RecalculateMinMaxPointValues() {
|
---|
453 | yMin = xMin = double.MaxValue;
|
---|
454 | yMax = xMax = double.MinValue;
|
---|
455 | foreach (var s in chart.Series.Where(x => x.Enabled)) {
|
---|
456 | foreach (var p in s.Points) {
|
---|
457 | double x = p.XValue, y = p.YValues[0];
|
---|
458 | if (xMin > x) xMin = x;
|
---|
459 | if (xMax < x) xMax = x;
|
---|
460 | if (yMin > y) yMin = y;
|
---|
461 | if (yMax < y) yMax = y;
|
---|
462 | }
|
---|
463 | }
|
---|
464 | }
|
---|
465 |
|
---|
466 | private void FillSeriesWithRowValues(Series series, ScatterPlotDataRow row) {
|
---|
467 | for (int i = 0; i < row.Points.Count; i++) {
|
---|
468 | var value = row.Points[i];
|
---|
469 | DataPoint point = new DataPoint();
|
---|
470 | if (IsInvalidValue(value.X) || IsInvalidValue(value.Y))
|
---|
471 | point.IsEmpty = true;
|
---|
472 | else {
|
---|
473 | point.XValue = value.X;
|
---|
474 | point.YValues = new double[] { value.Y };
|
---|
475 | }
|
---|
476 | series.Points.Add(point);
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | #region Helpers
|
---|
481 | private MarkerStyle ConvertPointStyle(ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle pointStyle) {
|
---|
482 | switch (pointStyle) {
|
---|
483 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle:
|
---|
484 | return MarkerStyle.Circle;
|
---|
485 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Cross:
|
---|
486 | return MarkerStyle.Cross;
|
---|
487 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Diamond:
|
---|
488 | return MarkerStyle.Diamond;
|
---|
489 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Square:
|
---|
490 | return MarkerStyle.Square;
|
---|
491 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Star4:
|
---|
492 | return MarkerStyle.Star4;
|
---|
493 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Star5:
|
---|
494 | return MarkerStyle.Star5;
|
---|
495 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Star6:
|
---|
496 | return MarkerStyle.Star6;
|
---|
497 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Star10:
|
---|
498 | return MarkerStyle.Star10;
|
---|
499 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Triangle:
|
---|
500 | return MarkerStyle.Triangle;
|
---|
501 | default:
|
---|
502 | return MarkerStyle.None;
|
---|
503 | }
|
---|
504 | }
|
---|
505 |
|
---|
506 | protected static bool IsInvalidValue(double x) {
|
---|
507 | return double.IsNaN(x) || x < (double)decimal.MinValue || x > (double)decimal.MaxValue;
|
---|
508 | }
|
---|
509 | #endregion
|
---|
510 | }
|
---|
511 | }
|
---|