Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/FDC/ScatterPlot.cs @ 16573

Last change on this file since 16573 was 16573, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.FLA addon to compile with new HL.Persistence

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