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