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

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

worked on CEDMA presentation layer (bubble chart, and collection of results) (#419)

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