source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Charting/BubbleChart.cs @ 1151

Last change on this file since 1151 was 1151, checked in by gkronber, 12 years ago

worked on #419 (Refactor CEDMA plugins):

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