Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/FDC/ScatterPlot.cs @ 7128

Last change on this file since 7128 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Analysis.FitnessLandscape {
31
32  public class PointsEventArgs : EventArgs {
33    public IEnumerable<PointF> NewPoints { get; private set; }
34    public PointsEventArgs(IEnumerable<PointF> points) {
35      NewPoints = points.ToList();
36    }
37  }
38
39  [Item("ScatterPlot", "A scatter plot of 2D data")]
40  [Creatable("Data Visualization")]
41  [StorableClass]
42  public class ScatterPlot : NamedItem {
43
44    public override Image ItemImage {
45      get { return HeuristicLab.Common.Resources.VSImageLibrary.Image; }
46    }
47
48    public event EventHandler<PointsEventArgs> PixelsChanged;
49    public event EventHandler AxisNameChanged;
50
51    protected virtual void OnPixelsChanged(IEnumerable<PointF> p) {
52      EventHandler<PointsEventArgs> handler = PixelsChanged;
53      if (handler != null)
54        handler(this, new PointsEventArgs(p));
55    }
56
57    protected virtual void OnAxesNameChanged() {
58      EventHandler handler = AxisNameChanged;
59      if (handler != null)
60        handler(this, EventArgs.Empty);
61    }
62
63    private IEnumerable<float> GetPointCoordinates() {
64      foreach (var p in points) {
65        yield return p.X;
66        yield return p.Y;
67      }
68    }
69
70    [Storable]
71    private List<float> PointCoordinateList {
72      get {
73        return new List<float>(GetPointCoordinates());
74      }
75      set {
76        points = new HashSet<PointF>();
77        var iter = value.GetEnumerator();
78        while (iter.MoveNext()) {
79          float x = iter.Current;
80          iter.MoveNext();
81          float y = iter.Current;
82          points.Add(new PointF(x, y));
83        }
84      }
85    }
86
87    private HashSet<PointF> points;
88    public IEnumerable<PointF> Points {
89      get { return points; }
90    }
91
92    [Storable]
93    private string xAxisName;
94    public string XAxisName {
95      get { return xAxisName; }
96      set {
97        if (value == xAxisName) return;
98        xAxisName = value;
99        OnAxesNameChanged();
100      }
101    }
102
103    [Storable]
104    private string yAxisName;
105    public string YAxisName {
106      get { return yAxisName; }
107      set {
108        if (value == yAxisName) return;
109        yAxisName = value;
110        OnAxesNameChanged();
111      }
112    }
113
114    public void Add(IEnumerable<PointF> points) {
115      List<PointF> newPoints = new List<PointF>();
116      foreach (var p in points) {
117        if (this.points.Add(p))
118          newPoints.Add(p);
119      }
120      if (newPoints.Count > 0)
121        OnPixelsChanged(newPoints);
122    }
123
124    [StorableConstructor]
125    protected ScatterPlot(bool deserializing) : base(deserializing) { }
126    protected ScatterPlot(ScatterPlot original, Cloner cloner)
127      : base(original, cloner) {
128      points = new HashSet<PointF>(original.points.Select(p => new PointF(p.X, p.Y)));
129      xAxisName = original.xAxisName;
130      yAxisName = original.yAxisName;
131    }
132
133
134    public ScatterPlot() {
135      points = new HashSet<PointF>();
136      Name = "Scatter Plot";
137      xAxisName = "Fitness";
138      yAxisName = "Distance";
139    }
140
141    public override IDeepCloneable Clone(Cloner cloner) {
142      return new ScatterPlot(this, cloner);
143    }
144
145
146    internal void Clear() {
147      var oldPoints = points;
148      points = new HashSet<PointF>();
149      OnPixelsChanged(oldPoints);
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.