Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis/3.3/DataVisualization/ScatterPlot.cs @ 8404

Last change on this file since 8404 was 8404, checked in by ascheibe, 12 years ago

#1911 added missing getters

File size: 12.8 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Linq;
27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Analysis {
34  [Item("ScatterPlot", "A scatter plot of 2D data")]
35  [StorableClass]
36  public class ScatterPlot : NamedItem, IStringConvertibleMatrix {
37    public static new Image StaticItemImage {
38      get { return HeuristicLab.Common.Resources.VSImageLibrary.Performance; }
39    }
40
41    private ScatterPlotVisualProperties visualProperties;
42    public ScatterPlotVisualProperties VisualProperties {
43      get { return visualProperties; }
44      set {
45        if (visualProperties != value) {
46          if (value == null) throw new ArgumentNullException("VisualProperties");
47          if (visualProperties != null) visualProperties.PropertyChanged -= new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
48          visualProperties = value;
49          visualProperties.PropertyChanged += new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
50          OnVisualPropertiesChanged();
51        }
52      }
53    }
54
55    private NamedItemCollection<ScatterPlotDataRow> rows;
56    public NamedItemCollection<ScatterPlotDataRow> Rows {
57      get { return rows; }
58      private set {
59        if (rows != null) throw new InvalidOperationException("Rows already set");
60        rows = value;
61        if (rows != null) RegisterRowsEvents();
62      }
63    }
64
65    #region Persistence Properties
66    [Storable(Name = "VisualProperties")]
67    private ScatterPlotVisualProperties StorableVisualProperties {
68      get { return visualProperties; }
69      set {
70        visualProperties = value;
71        visualProperties.PropertyChanged += new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
72      }
73    }
74    [Storable(Name = "Rows")]
75    private IEnumerable<ScatterPlotDataRow> StorableRows {
76      get { return rows; }
77      set { Rows = new NamedItemCollection<ScatterPlotDataRow>(value); }
78    }
79    #endregion
80
81    [StorableConstructor]
82    protected ScatterPlot(bool deserializing) : base(deserializing) { }
83    protected ScatterPlot(ScatterPlot original, Cloner cloner)
84      : base(original, cloner) {
85      VisualProperties = cloner.Clone(original.visualProperties);
86      Rows = cloner.Clone(original.rows);
87    }
88    public ScatterPlot()
89      : base() {
90      VisualProperties = new ScatterPlotVisualProperties();
91      Rows = new NamedItemCollection<ScatterPlotDataRow>();
92    }
93    public ScatterPlot(string name, string description)
94      : base(name, description) {
95      VisualProperties = new ScatterPlotVisualProperties(name);
96      Rows = new NamedItemCollection<ScatterPlotDataRow>();
97    }
98    public ScatterPlot(string name, string description, ScatterPlotVisualProperties visualProperties)
99      : base(name, description) {
100      VisualProperties = visualProperties;
101      Rows = new NamedItemCollection<ScatterPlotDataRow>();
102    }
103
104    // BackwardsCompatibility3.3
105    #region Backwards compatible code, remove with 3.4
106    private ObservableList<PointF> points;
107    [Storable(Name = "points")]
108    private ObservableList<PointF> StorablePoints {
109      set { points = value; }
110      get { return points; }
111    }
112    private string xAxisName;
113    [Storable(Name = "xAxisName")]
114    private string StorableXAxisName {
115      set { xAxisName = value; }
116      get { return xAxisName; }
117    }
118    private string yAxisName;
119    [Storable(Name = "yAxisName")]
120    private string StorableYAxisName {
121      set { yAxisName = value; }
122      get { return yAxisName; }
123    }
124    [StorableHook(HookType.AfterDeserialization)]
125    private void AfterDeserialization() {
126      if (VisualProperties == null) VisualProperties = new ScatterPlotVisualProperties(name);
127      if (string.IsNullOrEmpty(VisualProperties.XAxisTitle) && !string.IsNullOrEmpty(xAxisName)) VisualProperties.XAxisTitle = xAxisName;
128      if (string.IsNullOrEmpty(VisualProperties.YAxisTitle) && !string.IsNullOrEmpty(yAxisName)) VisualProperties.YAxisTitle = yAxisName;
129      if (rows == null) Rows = new NamedItemCollection<ScatterPlotDataRow>();
130      if ((Rows.Count == 0) && (points != null)) Rows.Add(new ScatterPlotDataRow(name, null, points.Select(p => new Point2D<double>(p.X, p.Y))));
131    }
132    #endregion
133
134    public override IDeepCloneable Clone(Cloner cloner) {
135      return new ScatterPlot(this, cloner);
136    }
137
138    public event EventHandler VisualPropertiesChanged;
139    protected virtual void OnVisualPropertiesChanged() {
140      EventHandler handler = VisualPropertiesChanged;
141      if (handler != null) handler(this, EventArgs.Empty);
142    }
143
144    private void VisualProperties_PropertyChanged(object sender, PropertyChangedEventArgs e) {
145      OnVisualPropertiesChanged();
146    }
147
148    protected virtual void RegisterRowsEvents() {
149      rows.ItemsAdded += new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(rows_ItemsAdded);
150      rows.ItemsRemoved += new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(rows_ItemsRemoved);
151      rows.ItemsReplaced += new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(rows_ItemsReplaced);
152      rows.CollectionReset += new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(rows_CollectionReset);
153    }
154    private void rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<ScatterPlotDataRow> e) {
155      foreach (ScatterPlotDataRow row in e.Items)
156        this.RegisterRowEvents(row);
157
158      this.OnColumnsChanged();
159      this.OnColumnNamesChanged();
160      this.OnReset();
161    }
162    private void rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ScatterPlotDataRow> e) {
163      foreach (ScatterPlotDataRow row in e.Items)
164        this.DeregisterRowEvents(row);
165
166      this.OnColumnsChanged();
167      this.OnColumnNamesChanged();
168      this.OnReset();
169    }
170    private void rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<ScatterPlotDataRow> e) {
171      foreach (ScatterPlotDataRow row in e.OldItems)
172        this.DeregisterRowEvents(row);
173      foreach (ScatterPlotDataRow row in e.Items)
174        this.RegisterRowEvents(row);
175
176      this.OnColumnsChanged();
177      this.OnColumnNamesChanged();
178      this.OnReset();
179    }
180    private void rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<ScatterPlotDataRow> e) {
181      foreach (ScatterPlotDataRow row in e.OldItems)
182        this.DeregisterRowEvents(row);
183      foreach (ScatterPlotDataRow row in e.Items)
184        this.RegisterRowEvents(row);
185
186      if (e.OldItems.Count() != e.Items.Count())
187        this.OnColumnsChanged();
188      this.OnColumnNamesChanged();
189      this.OnReset();
190    }
191
192    protected virtual void RegisterRowEvents(ScatterPlotDataRow row) {
193      row.Points.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsAdded);
194      row.Points.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsMoved);
195      row.Points.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsRemoved);
196      row.Points.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsReplaced);
197      row.Points.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_CollectionReset);
198    }
199    protected virtual void DeregisterRowEvents(ScatterPlotDataRow row) {
200      row.Points.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsAdded);
201      row.Points.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsMoved);
202      row.Points.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsRemoved);
203      row.Points.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsReplaced);
204      row.Points.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_CollectionReset);
205    }
206
207    private void Points_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
208      this.OnReset();
209    }
210    private void Points_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
211      this.OnReset();
212    }
213    private void Points_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
214      this.OnReset();
215    }
216    private void Points_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
217      this.OnReset();
218    }
219    private void Points_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
220      this.OnReset();
221    }
222
223    #region IStringConvertibleMatrix Members
224    int IStringConvertibleMatrix.Rows {
225      get { return rows.Count == 0 ? 0 : rows.Max(r => r.Points.Count); }
226      set { throw new NotSupportedException(); }
227    }
228    int IStringConvertibleMatrix.Columns {
229      get { return rows.Count; }
230      set { throw new NotSupportedException(); }
231    }
232    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
233      get { return rows.Select(r => r.Name); }
234      set { throw new NotSupportedException(); }
235    }
236    IEnumerable<string> IStringConvertibleMatrix.RowNames {
237      get { return Enumerable.Empty<string>(); }
238      set { throw new NotSupportedException(); }
239    }
240
241    bool IStringConvertibleMatrix.SortableView {
242      get { return true; }
243      set { throw new NotSupportedException(); }
244    }
245    bool IStringConvertibleMatrix.ReadOnly {
246      get { return true; }
247    }
248
249    string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
250      if (columnIndex < rows.Count) {
251        string columnName = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
252        if (rows.ContainsKey(columnName) && rowIndex < rows[columnName].Points.Count)
253          return string.Format("{0};{1}", rows[columnName].Points[rowIndex].X, rows[columnName].Points[rowIndex].Y);
254      }
255      return string.Empty;
256    }
257
258    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
259      throw new NotSupportedException();
260    }
261    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
262      throw new NotSupportedException();
263    }
264
265    public event EventHandler<EventArgs<int, int>> ItemChanged;
266    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
267      var handler = ItemChanged;
268      if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
269      OnToStringChanged();
270    }
271    public event EventHandler Reset;
272    protected virtual void OnReset() {
273      var handler = Reset;
274      if (handler != null) handler(this, EventArgs.Empty);
275    }
276    public event EventHandler ColumnsChanged;
277    protected virtual void OnColumnsChanged() {
278      var handler = ColumnsChanged;
279      if (handler != null) handler(this, EventArgs.Empty);
280    }
281    public event EventHandler RowsChanged;
282    protected virtual void OnRowsChanged() {
283      var handler = RowsChanged;
284      if (handler != null) handler(this, EventArgs.Empty);
285    }
286    public event EventHandler ColumnNamesChanged;
287    protected virtual void OnColumnNamesChanged() {
288      var handler = ColumnNamesChanged;
289      if (handler != null) handler(this, EventArgs.Empty);
290    }
291    public event EventHandler RowNamesChanged;
292    protected virtual void OnRowNamesChanged() {
293      var handler = RowNamesChanged;
294      if (handler != null) handler(this, EventArgs.Empty);
295    }
296    public event EventHandler SortableViewChanged;
297    protected virtual void OnSortableViewChanged() {
298      var handler = SortableViewChanged;
299      if (handler != null) handler(this, EventArgs.Empty);
300    }
301    #endregion
302  }
303}
Note: See TracBrowser for help on using the repository browser.