Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1424 was 1424, checked in by gkronber, 15 years ago

Fixed tooltip bug in CEDMA BubbleChart. #543 (MouseDrag events in CEDMA BubbleChart cause 100% CPU utilization)

File size: 10.7 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      if (sizeDimension != null && results.OrdinalVariables.Contains(sizeDimension)) {
121        var sizes = records
122          .Select(x => Convert.ToDouble(x.Get(sizeDimension)))
123          .Where(size => !double.IsInfinity(size) && size != double.MaxValue && size != double.MinValue)
124          .OrderBy(r => r);
125        minSize = sizes.ElementAt((int)(sizes.Count() * 0.1));
126        maxSize = sizes.ElementAt((int)(sizes.Count() * 0.9));
127      } else {
128        minSize = 1;
129        maxSize = 1;
130      }
131      UpdateEnabled = false;
132      Group.Clear();
133      primitiveToEntryDictionary.Clear();
134      entryToPrimitiveDictionary.Clear();
135      points = new Group(this);
136      Group.Add(new Axis(this, 0, 0, AxisType.Both));
137      UpdateViewSize(0, 0, 5);
138      foreach (ResultsEntry r in records) {
139        double x, y;
140        int size;
141        if (results.OrdinalVariables.Contains(xDimension)) {
142          x = Convert.ToDouble(r.Get(xDimension)) + (double)r.Get(X_JITTER) * xJitterFactor;
143        } else if (results.CategoricalVariables.Contains(xDimension)) {
144          x = results.IndexOfCategoricalValue(xDimension, r.Get(xDimension)) + (double)r.Get(X_JITTER) * xJitterFactor;
145        } else {
146          x = double.NaN;
147        }
148        if (results.OrdinalVariables.Contains(yDimension)) {
149          y = Convert.ToDouble(r.Get(yDimension)) + (double)r.Get(Y_JITTER) * yJitterFactor;
150        } else if (results.CategoricalVariables.Contains(yDimension)) {
151          y = results.IndexOfCategoricalValue(yDimension, r.Get(yDimension)) + (double)r.Get(Y_JITTER) * yJitterFactor;
152        } else {
153          y = double.NaN;
154        }
155        size = CalculateSize(Convert.ToDouble(r.Get(sizeDimension)), minSize, maxSize);
156
157        if (double.IsInfinity(x) || x == double.MaxValue || x == double.MinValue) x = double.NaN;
158        if (double.IsInfinity(y) || y == double.MaxValue || y == double.MinValue) y = double.NaN;
159        if (!double.IsNaN(x) && !double.IsNaN(y)) {
160          UpdateViewSize(x, y, size);
161          int alpha = CalculateAlpha(size);
162          Pen pen = new Pen(Color.FromArgb(alpha, r.Selected ? selectionColor : defaultColor));
163          Brush brush = pen.Brush;
164          FixedSizeCircle c = new FixedSizeCircle(this, x, y, size, pen, brush);
165          c.ToolTipText = r.GetToolTipText();
166          points.Add(c);
167          if (!r.Selected) c.IntoBackground();
168          primitiveToEntryDictionary[c] = r;
169          entryToPrimitiveDictionary[r] = c;
170        }
171      }
172      Group.Add(points);
173      UpdateEnabled = true;
174    }
175
176    private int CalculateSize(double size, double minSize, double maxSize) {
177      if (double.IsNaN(size) || double.IsInfinity(size) || size == double.MaxValue || size == double.MinValue) return minBubbleSize;
178      if (size > maxSize) size = maxSize;
179      if (size < minSize) size = minSize;
180      if (Math.Abs(maxSize - minSize) < 1.0E-10) return minBubbleSize;
181      double sizeDifference = ((size - minSize) / (maxSize - minSize) * (maxBubbleSize - minBubbleSize));
182      if (invertSize) return maxBubbleSize - (int)sizeDifference;
183      else return minBubbleSize + (int)sizeDifference;
184    }
185
186    private int CalculateAlpha(int size) {
187      return maxAlpha - (int)((double)(size - minBubbleSize) / (double)(maxBubbleSize - minBubbleSize) * (double)(maxAlpha - minAlpha));
188    }
189
190    private void ZoomToViewSize() {
191      if (minX < maxX && minY < maxY) {
192        // enlarge view by 5% on each side
193        double width = maxX - minX;
194        double height = maxY - minY;
195        minX = minX - width * 0.05;
196        maxX = maxX + width * 0.05;
197        minY = minY - height * 0.05;
198        maxY = maxY + height * 0.05;
199        ZoomIn(minX, minY, maxX, maxY);
200      }
201    }
202
203    private void UpdateViewSize(double x, double y, double size) {
204      if (x - size < minX) minX = x - size;
205      if (x + size > maxX) maxX = x + size;
206      if (y - size < minY) minY = y + size;
207      if (y + size > maxY) maxY = y + size;
208    }
209
210    private void ResetViewSize() {
211      minX = double.PositiveInfinity;
212      maxX = double.NegativeInfinity;
213      minY = double.PositiveInfinity;
214      maxY = double.NegativeInfinity;
215    }
216
217    internal ResultsEntry GetResultsEntry(Point point) {
218      ResultsEntry r = null;
219      IPrimitive p = points.GetPrimitive(TransformPixelToWorld(point));
220      if (p != null) {
221        primitiveToEntryDictionary.TryGetValue(p, out r);
222      }
223      return r;
224    }
225
226    public override void MouseDrag(Point start, Point end, MouseButtons button) {
227      if (button == MouseButtons.Left && Mode == ChartMode.Select) {
228        PointD a = TransformPixelToWorld(start);
229        PointD b = TransformPixelToWorld(end);
230        double minX = Math.Min(a.X, b.X);
231        double minY = Math.Min(a.Y, b.Y);
232        double maxX = Math.Max(a.X, b.X);
233        double maxY = Math.Max(a.Y, b.Y);
234        HeuristicLab.Charting.Rectangle rect = new HeuristicLab.Charting.Rectangle(this, minX, minY, maxX, maxY);
235
236        List<IPrimitive> primitives = new List<IPrimitive>();
237        primitives.AddRange(points.Primitives);
238
239        foreach (FixedSizeCircle p in primitives) {
240          if (rect.ContainsPoint(p.Point)) {
241            ResultsEntry r;
242            primitiveToEntryDictionary.TryGetValue(p, out r);
243            if (r != null) r.ToggleSelected();
244          }
245        }
246        if (primitives.Count() > 0) results.FireChanged();
247      } else {
248        base.MouseDrag(start, end, button);
249      }
250    }
251
252    public override void MouseClick(Point point, MouseButtons button) {
253      if (button == MouseButtons.Left) {
254        ResultsEntry r = GetResultsEntry(point);
255        if (r != null) {
256          r.ToggleSelected();
257          results.FireChanged();
258        }
259      } else {
260        base.MouseClick(point, button);
261      }
262    }
263
264    public override void MouseDoubleClick(Point point, MouseButtons button) {
265      if (button == MouseButtons.Left) {
266        ResultsEntry entry = GetResultsEntry(point);
267        if (entry != null) {
268          string serializedData = (string)entry.Get(Ontology.PredicateSerializedData.Uri.Replace(Ontology.CedmaNameSpace, ""));
269          var model = (IItem)PersistenceManager.RestoreFromGZip(Convert.FromBase64String(serializedData));
270          PluginManager.ControlManager.ShowControl(model.CreateView());
271        }
272      } else {
273        base.MouseDoubleClick(point, button);
274      }
275    }
276  }
277}
Note: See TracBrowser for help on using the repository browser.