Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Analysis/3.3/DataVisualization/ScatterPlot.cs @ 17687

Last change on this file since 17687 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

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