Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingScatterPlotView.cs @ 10925

Last change on this file since 10925 was 10915, checked in by aesterer, 10 years ago

Splitted scatter plot view in single and multi view

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