Free cookie consent management tool by TermsFeed Policy Generator

source: branches/plugins/HeuristicLab.CEDMA.Charting/3.2/BubbleChart.cs @ 3912

Last change on this file since 3912 was 1156, checked in by gkronber, 16 years ago

worked on #419

  • added function to open and display any model
  • added 'hard-coded' implementation of offspring selection GP (work in progress)
  • added properties for max. size and max. height in StandardGP
File size: 10.3 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;
29using HeuristicLab.CEDMA.Core;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Core;
32using HeuristicLab.CEDMA.DB.Interfaces;
33
34namespace HeuristicLab.CEDMA.Charting {
35  public class BubbleChart : Chart {
36    private const string X_JITTER = "__X_JITTER";
37    private const string Y_JITTER = "__Y_JITTER";
38    private const double maxXJitterPercent = 0.05;
39    private const double maxYJitterPercent = 0.05;
40    private const int minBubbleSize = 5;
41    private const int maxBubbleSize = 30;
42    private const int maxAlpha = 255;
43    private const int minAlpha = 50;
44    private static readonly Color defaultColor = Color.Blue;
45    private static readonly Color selectionColor = Color.Red;
46
47    private double xJitterFactor = 0.0;
48    private double yJitterFactor = 0.0;
49    private string xDimension;
50    private string yDimension;
51    private string sizeDimension;
52    private bool invertSize;
53    private double minX = double.PositiveInfinity;
54    private double minY = double.PositiveInfinity;
55    private double maxX = double.NegativeInfinity;
56    private double maxY = double.NegativeInfinity;
57    private List<ResultsEntry> records;
58    private Results results;
59    private Dictionary<IPrimitive, ResultsEntry> primitiveToEntryDictionary;
60    private Dictionary<ResultsEntry, IPrimitive> entryToPrimitiveDictionary;
61    private Random random = new Random();
62    private Group points;
63
64    public BubbleChart(Results results, PointD lowerLeft, PointD upperRight)
65      : base(lowerLeft, upperRight) {
66      records = new List<ResultsEntry>();
67      primitiveToEntryDictionary = new Dictionary<IPrimitive, ResultsEntry>();
68      entryToPrimitiveDictionary = new Dictionary<ResultsEntry, IPrimitive>();
69      this.results = results;
70
71      foreach (var resultsEntry in results.GetEntries()) {
72        if (resultsEntry.Get(X_JITTER) == null)
73          resultsEntry.Set(X_JITTER, random.NextDouble() * 2.0 - 1.0);
74        if (resultsEntry.Get(Y_JITTER) == null)
75          resultsEntry.Set(Y_JITTER, random.NextDouble() * 2.0 - 1.0);
76        records.Add(resultsEntry);
77      }
78
79      results.Changed += new EventHandler(results_Changed);
80    }
81
82    void results_Changed(object sender, EventArgs e) {
83      Repaint();
84      EnforceUpdate();
85    }
86
87    public BubbleChart(Results results, double x1, double y1, double x2, double y2)
88      : this(results, new PointD(x1, y1), new PointD(x2, y2)) {
89    }
90
91    public void SetBubbleSizeDimension(string dimension, bool inverted) {
92      this.sizeDimension = dimension;
93      this.invertSize = inverted;
94      Repaint();
95      EnforceUpdate();
96    }
97
98    public void ShowXvsY(string xDimension, string yDimension) {
99      if (this.xDimension != xDimension || this.yDimension != yDimension) {
100        this.xDimension = xDimension;
101        this.yDimension = yDimension;
102
103        ResetViewSize();
104        Repaint();
105        ZoomToViewSize();
106      }
107    }
108
109    internal void SetJitter(double xJitterFactor, double yJitterFactor) {
110      this.xJitterFactor = xJitterFactor * maxXJitterPercent * Size.Width;
111      this.yJitterFactor = yJitterFactor * maxYJitterPercent * Size.Height;
112      Repaint();
113      EnforceUpdate();
114    }
115
116    private void Repaint() {
117      if (xDimension == null || yDimension == null) return;
118      double maxSize = 1;
119      double minSize = 1;
120      try {
121        if (sizeDimension != null && results.OrdinalVariables.Contains(sizeDimension)) {
122          var sizes = records
123            .Select(x => Convert.ToDouble(x.Get(sizeDimension)))
124            .Where(size => !double.IsInfinity(size) && size != double.MaxValue && size != double.MinValue)
125            .OrderBy(r => r);
126          minSize = sizes.ElementAt((int)(sizes.Count() * 0.1));
127          maxSize = sizes.ElementAt((int)(sizes.Count() * 0.9));
128        }
129      }
130      catch (InvalidCastException) {
131        minSize = 1;
132        maxSize = 1;
133      }
134      UpdateEnabled = false;
135      Group.Clear();
136      primitiveToEntryDictionary.Clear();
137      entryToPrimitiveDictionary.Clear();
138      points = new Group(this);
139      Group.Add(new Axis(this, 0, 0, AxisType.Both));
140      UpdateViewSize(0, 0, 5);
141      foreach (ResultsEntry r in records) {
142        double x, y;
143        int size;
144        try {
145          x = Convert.ToDouble(r.Get(xDimension)) + (double)r.Get(X_JITTER) * xJitterFactor;
146          y = Convert.ToDouble(r.Get(yDimension)) + (double)r.Get(Y_JITTER) * yJitterFactor;
147          size = CalculateSize(Convert.ToDouble(r.Get(sizeDimension)), minSize, maxSize);
148        }
149        catch (InvalidCastException) {
150          x = double.NaN;
151          y = double.NaN;
152          size = minBubbleSize;
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, r.Selected ? selectionColor : defaultColor));
160          Brush brush = pen.Brush;
161          FixedSizeCircle c = new FixedSizeCircle(this, x, y, size, pen, brush);
162          c.ToolTipText = r.GetToolTipText();
163          points.Add(c);
164          if (!r.Selected) c.IntoBackground();
165          primitiveToEntryDictionary[c] = r;
166          entryToPrimitiveDictionary[r] = c;
167        }
168      }
169      Group.Add(points);
170      UpdateEnabled = true;
171    }
172
173    private int CalculateSize(double size, double minSize, double maxSize) {
174      if (double.IsNaN(size) || double.IsInfinity(size) || size == double.MaxValue || size == double.MinValue) return minBubbleSize;
175      if (size > maxSize) size = maxSize;
176      if (size < minSize) size = minSize;
177      if (Math.Abs(maxSize - minSize) < 1.0E-10) return minBubbleSize;
178      double sizeDifference = ((size - minSize) / (maxSize - minSize) * (maxBubbleSize - minBubbleSize));
179      if (invertSize) return maxBubbleSize - (int)sizeDifference;
180      else return minBubbleSize + (int)sizeDifference;
181    }
182
183    private int CalculateAlpha(int size) {
184      return maxAlpha - (int)((double)(size - minBubbleSize) / (double)(maxBubbleSize - minBubbleSize) * (double)(maxAlpha - minAlpha));
185    }
186
187    private void ZoomToViewSize() {
188      if (minX < maxX && minY < maxY) {
189        // enlarge view by 5% on each side
190        double width = maxX - minX;
191        double height = maxY - minY;
192        minX = minX - width * 0.05;
193        maxX = maxX + width * 0.05;
194        minY = minY - height * 0.05;
195        maxY = maxY + height * 0.05;
196        ZoomIn(minX, minY, maxX, maxY);
197      }
198    }
199
200    private void UpdateViewSize(double x, double y, double size) {
201      if (x - size < minX) minX = x - size;
202      if (x + size > maxX) maxX = x + size;
203      if (y - size < minY) minY = y + size;
204      if (y + size > maxY) maxY = y + size;
205    }
206
207    private void ResetViewSize() {
208      minX = double.PositiveInfinity;
209      maxX = double.NegativeInfinity;
210      minY = double.PositiveInfinity;
211      maxY = double.NegativeInfinity;
212    }
213
214    internal ResultsEntry GetResultsEntry(Point point) {
215      ResultsEntry r = null;
216      IPrimitive p = points.GetPrimitive(TransformPixelToWorld(point));
217      if (p != null) {
218        primitiveToEntryDictionary.TryGetValue(p, out r);
219      }
220      return r;
221    }
222
223
224    public override void MouseDrag(Point start, Point end, MouseButtons button) {
225      if (button == MouseButtons.Left && Mode == ChartMode.Select) {
226        PointD a = TransformPixelToWorld(start);
227        PointD b = TransformPixelToWorld(end);
228        double minX = Math.Min(a.X, b.X);
229        double minY = Math.Min(a.Y, b.Y);
230        double maxX = Math.Max(a.X, b.X);
231        double maxY = Math.Max(a.Y, b.Y);
232        HeuristicLab.Charting.Rectangle rect = new HeuristicLab.Charting.Rectangle(this, minX, minY, maxX, maxY);
233
234        List<IPrimitive> primitives = new List<IPrimitive>();
235        primitives.AddRange(points.Primitives);
236
237        foreach (FixedSizeCircle p in primitives) {
238          if (rect.ContainsPoint(p.Point)) {
239            ResultsEntry r;
240            primitiveToEntryDictionary.TryGetValue(p, out r);
241            if (r != null) r.ToggleSelected();
242          }
243        }
244        if (primitives.Count() > 0) results.FireChanged();
245      } else {
246        base.MouseDrag(start, end, button);
247      }
248    }
249
250    public override void MouseClick(Point point, MouseButtons button) {
251      if (button == MouseButtons.Left) {
252        ResultsEntry r = GetResultsEntry(point);
253        if (r != null) r.ToggleSelected();
254        results.FireChanged();
255      } else {
256        base.MouseClick(point, button);
257      }
258    }
259
260    public override void MouseDoubleClick(Point point, MouseButtons button) {
261      if (button == MouseButtons.Left) {
262        ResultsEntry entry = GetResultsEntry(point);
263        if (entry != null) {
264          string serializedData = (string)entry.Get(Ontology.PredicateSerializedData.Uri.Replace(Ontology.CedmaNameSpace, ""));
265          var model = (IItem)PersistenceManager.RestoreFromGZip(Convert.FromBase64String(serializedData));
266          PluginManager.ControlManager.ShowControl(model.CreateView());
267        }
268      } else {
269        base.MouseDoubleClick(point, button);
270      }
271    }
272  }
273}
Note: See TracBrowser for help on using the repository browser.