Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Charting/BubbleChart.cs @ 564

Last change on this file since 564 was 562, checked in by gkronber, 16 years ago
  • implemented selection of points
  • implemented functionality to open the underlying FunctionTree of a point

#268 (Possibility to open the function-tree of any model (represented as point in the scatter-plot))

File size: 8.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using System.Drawing;
26using System.Linq;
27using HeuristicLab.Charting;
28using System.Windows.Forms;
29
30namespace HeuristicLab.CEDMA.Charting {
31  public class BubbleChart : Chart {
32    private const double maxXJitterPercent = 0.05;
33    private const double maxYJitterPercent = 0.05;
34    private const int minBubbleSize = 5;
35    private const int maxBubbleSize = 30;
36    private const int maxAlpha = 255;
37    private const int minAlpha = 50;
38    private static readonly Color defaultColor = Color.Blue;
39    private static readonly Color selectionColor = Color.Red;
40
41    private double xJitterFactor = 0.0;
42    private double yJitterFactor = 0.0;
43    private string xDimension;
44    private string yDimension;
45    private string sizeDimension;
46    private bool invertSize;
47    private double minX = double.PositiveInfinity;
48    private double minY = double.PositiveInfinity;
49    private double maxX = double.NegativeInfinity;
50    private double maxY = double.NegativeInfinity;
51    private List<Record> records;
52    private ResultList results;
53    private Dictionary<IPrimitive, Record> primitiveToRecordDictionary;
54    private Random random = new Random();
55    private Group points;
56
57    public BubbleChart(ResultList results, PointD lowerLeft, PointD upperRight)
58      : base(lowerLeft, upperRight) {
59      records = new List<Record>();
60      primitiveToRecordDictionary = new Dictionary<IPrimitive, Record>();
61      this.results = results;
62      results.OnRecordAdded += new EventHandler<RecordAddedEventArgs>(results_OnRecordAdded);
63    }
64
65    public BubbleChart(ResultList results, double x1, double y1, double x2, double y2)
66      : this(results, new PointD(x1, y1), new PointD(x2, y2)) {
67    }
68
69    void results_OnRecordAdded(object sender, RecordAddedEventArgs e) {
70      lock(records) {
71        e.Record.OnSelectionChanged += new EventHandler(Record_OnSelectionChanged);
72        records.Add(e.Record);
73      }
74    }
75
76    void Record_OnSelectionChanged(object sender, EventArgs e) {
77      Record r = (Record)sender;
78      foreach(KeyValuePair<IPrimitive, Record> pair in primitiveToRecordDictionary) {
79        if(pair.Value == r) {
80          IPrimitive primitive = pair.Key;
81          if(r.Selected) {
82            int alpha = primitive.Pen.Color.A;
83            primitive.Pen.Color = Color.FromArgb(alpha, selectionColor);
84            primitive.Brush = primitive.Pen.Brush;
85            primitive.IntoForeground();
86          } else {
87            int alpha = primitive.Pen.Color.A;
88            primitive.Pen.Color = Color.FromArgb(alpha, defaultColor);
89            primitive.Brush = primitive.Pen.Brush;
90            primitive.IntoBackground();
91          }
92          primitive.EnforceUpdate();
93        }
94      }
95    }
96
97    //public void AddDimension(string name) {
98    //  dimensions.Add(name);
99    //  values.Add(name, new List<double>());
100    //}
101    //public void RemoveDimension(string name) {
102    //  dimensions.Remove(name);
103    //  values.Remove(name);
104    //}
105
106    //public void AddDataPoint(string dimension, double value) {
107    //  values[dimension].Add(value);
108    //}
109
110    public void SetBubbleSizeDimension(string dimension, bool inverted) {
111      this.sizeDimension = dimension;
112      this.invertSize = inverted;
113      Repaint();
114      EnforceUpdate();
115    }
116
117    public void ShowXvsY(string xDimension, string yDimension) {
118      if(this.xDimension != xDimension || this.yDimension != yDimension) {
119        this.xDimension = xDimension;
120        this.yDimension = yDimension;
121        ResetViewSize();
122        Repaint();
123        ZoomToViewSize();
124      }
125    }
126
127    internal void SetJitter(double xJitterFactor, double yJitterFactor) {
128      this.xJitterFactor = xJitterFactor * maxXJitterPercent * Size.Width;
129      this.yJitterFactor = yJitterFactor * maxYJitterPercent * Size.Height;
130      Repaint();
131      EnforceUpdate();
132    }
133
134    private void Repaint() {
135      lock(records) {
136        double maxSize = 1;
137        double minSize = 1;
138        if(sizeDimension != null) {
139          var sizes = records.Select(r => r.Get(sizeDimension));
140          maxSize = sizes.Max();
141          minSize = sizes.Min();
142        }
143        UpdateEnabled = false;
144        Group.Clear();
145        primitiveToRecordDictionary.Clear();
146        points = new Group(this);
147        Group.Add(new Axis(this, 0, 0, AxisType.Both));
148        UpdateViewSize(0, 0, 5);
149        foreach(Record r in records) {
150          double x = r.Get(xDimension) + (random.NextDouble() * 2.0 - 1.0) * xJitterFactor;
151          double y = r.Get(yDimension) + (random.NextDouble() * 2.0 - 1.0) * yJitterFactor;
152          int size = CalculateSize(r.Get(sizeDimension), minSize, maxSize);
153
154          if(double.IsInfinity(x) || x == double.MaxValue || x == double.MinValue) x = double.NaN;
155          if(double.IsInfinity(y) || y == double.MaxValue || y == double.MinValue) y = double.NaN;
156          if(!double.IsNaN(x) && !double.IsNaN(y)) {
157            UpdateViewSize(x, y, size);
158            int alpha = CalculateAlpha(size);
159            Pen pen = new Pen(Color.FromArgb(alpha, defaultColor));
160            Brush brush = pen.Brush;
161            FixedSizeCircle c = new FixedSizeCircle(this, x, y, size, pen, brush);
162            c.ToolTipText = CreateToolTipText(r);
163            points.Add(c);
164            primitiveToRecordDictionary[c] = r;
165          }
166        }
167        Group.Add(points);
168        UpdateEnabled = true;
169      }
170    }
171
172    private string CreateToolTipText(Record r) {
173      StringBuilder b = new StringBuilder();
174      foreach(KeyValuePair<string, double> v in r.Values) {
175        b.Append(v.Key).Append(" = ").Append(v.Value).AppendLine();
176      }
177      return b.ToString();
178    }
179
180    private int CalculateSize(double size, double minSize, double maxSize) {
181      if(double.IsNaN(size)) return minBubbleSize;
182      double sizeDifference = ((size - minSize) / (maxSize - minSize) * (maxBubbleSize - minBubbleSize));
183      if(invertSize) return maxBubbleSize - (int)sizeDifference;
184      else return minBubbleSize + (int)sizeDifference;
185    }
186
187    private int CalculateAlpha(int size) {
188      return maxAlpha - (int)((double)(size - minBubbleSize) / (double)(maxBubbleSize - minBubbleSize) * (double)(maxAlpha - minAlpha));
189    }
190
191    private void ZoomToViewSize() {
192      if(minX < maxX && minY < maxY) {
193        // enlarge view by 5% on each side
194        double width = maxX - minX;
195        double height = maxY - minY;
196        minX = minX - width * 0.05;
197        maxX = maxX + width * 0.05;
198        minY = minY - height * 0.05;
199        maxY = maxY + height * 0.05;
200        ZoomIn(minX, minY, maxX, maxY);
201      }
202    }
203
204    private void UpdateViewSize(double x, double y, double size) {
205      if(x - size < minX) minX = x - size;
206      if(x + size > maxX) maxX = x + size;
207      if(y - size < minY) minY = y + size;
208      if(y + size > maxY) maxY = y + size;
209    }
210
211    private void ResetViewSize() {
212      minX = double.PositiveInfinity;
213      maxX = double.NegativeInfinity;
214      minY = double.PositiveInfinity;
215      maxY = double.NegativeInfinity;
216    }
217
218    public override void MouseClick(Point point, MouseButtons button) {
219      if(button == MouseButtons.Left) {
220        Record r;
221        IPrimitive p = points.GetPrimitive(TransformPixelToWorld(point));
222        if(p != null) {
223          primitiveToRecordDictionary.TryGetValue(p, out r);
224          if(r != null) r.ToggleSelected();
225        }
226      } else base.MouseClick(point, button);
227    }
228
229    public override void MouseDoubleClick(Point point, MouseButtons button) {
230      if(button == MouseButtons.Left) {
231        Record r;
232        IPrimitive p = points.GetPrimitive(TransformPixelToWorld(point));
233        if(p != null) {
234          primitiveToRecordDictionary.TryGetValue(p, out r);
235          if(r != null) results.OpenModel(r);
236        }
237      } else base.MouseDoubleClick(point, button);
238    }
239  }
240}
Note: See TracBrowser for help on using the repository browser.