Free cookie consent management tool by TermsFeed Policy Generator

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

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

worked on presentation layer for CEDMA (brushing) (#419)

File size: 9.9 KB
RevLine 
[561]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;
[562]28using System.Windows.Forms;
[1106]29using HeuristicLab.CEDMA.Core;
[561]30
31namespace HeuristicLab.CEDMA.Charting {
32  public class BubbleChart : Chart {
[1108]33    private const string X_JITTER = "__X_JITTER";
34    private const string Y_JITTER = "__Y_JITTER";
[561]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;
[562]42    private static readonly Color selectionColor = Color.Red;
[561]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;
[1108]54    private List<ResultsEntry> records;
[1106]55    private Results results;
[1108]56    private Dictionary<IPrimitive, ResultsEntry> primitiveToEntryDictionary;
57    private Dictionary<ResultsEntry, IPrimitive> entryToPrimitiveDictionary;
[561]58    private Random random = new Random();
[562]59    private Group points;
[561]60
[1106]61    public BubbleChart(Results results, PointD lowerLeft, PointD upperRight)
[561]62      : base(lowerLeft, upperRight) {
[1108]63      records = new List<ResultsEntry>();
64      primitiveToEntryDictionary = new Dictionary<IPrimitive, ResultsEntry>();
65      entryToPrimitiveDictionary = new Dictionary<ResultsEntry, IPrimitive>();
[562]66      this.results = results;
[1109]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);
[561]77    }
[562]78
[566]79    void results_Changed(object sender, EventArgs e) {
80      Repaint();
81      EnforceUpdate();
82    }
83
[1108]84    public BubbleChart(Results results, double x1, double y1, double x2, double y2)
[562]85      : this(results, new PointD(x1, y1), new PointD(x2, y2)) {
[561]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) {
[1108]96      if (this.xDimension != xDimension || this.yDimension != yDimension) {
[561]97        this.xDimension = xDimension;
98        this.yDimension = yDimension;
[1108]99
[561]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() {
[1108]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);
[570]123          minSize = sizes.ElementAt((int)(sizes.Count() * 0.1));
124          maxSize = sizes.ElementAt((int)(sizes.Count() * 0.9));
[561]125        }
[1108]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);
[561]145        }
[1108]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        }
[561]165      }
[1108]166      Group.Add(points);
167      UpdateEnabled = true;
[561]168    }
169
170    private int CalculateSize(double size, double minSize, double maxSize) {
[1108]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;
[561]175      double sizeDifference = ((size - minSize) / (maxSize - minSize) * (maxBubbleSize - minBubbleSize));
[1108]176      if (invertSize) return maxBubbleSize - (int)sizeDifference;
[561]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() {
[1108]185      if (minX < maxX && minY < maxY) {
[561]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) {
[1108]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;
[561]202    }
203
204    private void ResetViewSize() {
205      minX = double.PositiveInfinity;
206      maxX = double.NegativeInfinity;
207      minY = double.PositiveInfinity;
208      maxY = double.NegativeInfinity;
209    }
[562]210
[1108]211    internal ResultsEntry GetResultsEntry(Point point) {
212      ResultsEntry r = null;
[567]213      IPrimitive p = points.GetPrimitive(TransformPixelToWorld(point));
[1108]214      if (p != null) {
215        primitiveToEntryDictionary.TryGetValue(p, out r);
[567]216      }
217      return r;
[562]218    }
219
[566]220
221    public override void MouseDrag(Point start, Point end, MouseButtons button) {
[1108]222      if (button == MouseButtons.Left && Mode == ChartMode.Select) {
[566]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);
[569]229        HeuristicLab.Charting.Rectangle rect = new HeuristicLab.Charting.Rectangle(this, minX, minY, maxX, maxY);
[566]230
231        List<IPrimitive> primitives = new List<IPrimitive>();
232        primitives.AddRange(points.Primitives);
233
[1108]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();
[566]239          }
240        }
[1109]241        if(primitives.Count() > 0) results.FireChanged();
[566]242      } else {
243        base.MouseDrag(start, end, button);
244      }
245    }
[569]246
247    public override void MouseClick(Point point, MouseButtons button) {
[1108]248      if (button == MouseButtons.Left) {
249        ResultsEntry r = GetResultsEntry(point);
250        if (r != null) r.ToggleSelected();
[1109]251        results.FireChanged();
[569]252      } else {
253        base.MouseClick(point, button);
254      }
255    }
256
[1108]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    //}
[561]265  }
266}
Note: See TracBrowser for help on using the repository browser.