[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Drawing;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using HeuristicLab.Charting;
|
---|
[562] | 28 | using System.Windows.Forms;
|
---|
[1287] | 29 | using HeuristicLab.CEDMA.Core;
|
---|
| 30 | using HeuristicLab.PluginInfrastructure;
|
---|
| 31 | using HeuristicLab.Core;
|
---|
| 32 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
[561] | 33 |
|
---|
| 34 | namespace HeuristicLab.CEDMA.Charting {
|
---|
| 35 | public class BubbleChart : Chart {
|
---|
[1287] | 36 | private const string X_JITTER = "__X_JITTER";
|
---|
| 37 | private const string Y_JITTER = "__Y_JITTER";
|
---|
[561] | 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;
|
---|
[562] | 45 | private static readonly Color selectionColor = Color.Red;
|
---|
[561] | 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;
|
---|
[1287] | 57 | private List<ResultsEntry> records;
|
---|
| 58 | private Results results;
|
---|
| 59 | private Dictionary<IPrimitive, ResultsEntry> primitiveToEntryDictionary;
|
---|
| 60 | private Dictionary<ResultsEntry, IPrimitive> entryToPrimitiveDictionary;
|
---|
[561] | 61 | private Random random = new Random();
|
---|
[562] | 62 | private Group points;
|
---|
[561] | 63 |
|
---|
[1287] | 64 | public BubbleChart(Results results, PointD lowerLeft, PointD upperRight)
|
---|
[561] | 65 | : base(lowerLeft, upperRight) {
|
---|
[1287] | 66 | records = new List<ResultsEntry>();
|
---|
| 67 | primitiveToEntryDictionary = new Dictionary<IPrimitive, ResultsEntry>();
|
---|
| 68 | entryToPrimitiveDictionary = new Dictionary<ResultsEntry, IPrimitive>();
|
---|
[562] | 69 | this.results = results;
|
---|
[1287] | 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 |
|
---|
[566] | 79 | results.Changed += new EventHandler(results_Changed);
|
---|
[561] | 80 | }
|
---|
[562] | 81 |
|
---|
[566] | 82 | void results_Changed(object sender, EventArgs e) {
|
---|
| 83 | Repaint();
|
---|
| 84 | EnforceUpdate();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[1287] | 87 | public BubbleChart(Results results, double x1, double y1, double x2, double y2)
|
---|
[562] | 88 | : this(results, new PointD(x1, y1), new PointD(x2, y2)) {
|
---|
[561] | 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) {
|
---|
[1287] | 99 | if (this.xDimension != xDimension || this.yDimension != yDimension) {
|
---|
[561] | 100 | this.xDimension = xDimension;
|
---|
| 101 | this.yDimension = yDimension;
|
---|
[1287] | 102 |
|
---|
[561] | 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() {
|
---|
[1287] | 117 | if (xDimension == null || yDimension == null) return;
|
---|
| 118 | double maxSize = 1;
|
---|
| 119 | double minSize = 1;
|
---|
[1424] | 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 {
|
---|
[1287] | 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;
|
---|
[1424] | 141 | if (results.OrdinalVariables.Contains(xDimension)) {
|
---|
[1287] | 142 | x = Convert.ToDouble(r.Get(xDimension)) + (double)r.Get(X_JITTER) * xJitterFactor;
|
---|
[1424] | 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)) {
|
---|
[1287] | 149 | y = Convert.ToDouble(r.Get(yDimension)) + (double)r.Get(Y_JITTER) * yJitterFactor;
|
---|
[1424] | 150 | } else if (results.CategoricalVariables.Contains(yDimension)) {
|
---|
| 151 | y = results.IndexOfCategoricalValue(yDimension, r.Get(yDimension)) + (double)r.Get(Y_JITTER) * yJitterFactor;
|
---|
| 152 | } else {
|
---|
[1287] | 153 | y = double.NaN;
|
---|
| 154 | }
|
---|
[1424] | 155 | size = CalculateSize(Convert.ToDouble(r.Get(sizeDimension)), minSize, maxSize);
|
---|
| 156 |
|
---|
[1287] | 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 | }
|
---|
[561] | 171 | }
|
---|
[1287] | 172 | Group.Add(points);
|
---|
| 173 | UpdateEnabled = true;
|
---|
[561] | 174 | }
|
---|
| 175 |
|
---|
| 176 | private int CalculateSize(double size, double minSize, double maxSize) {
|
---|
[1287] | 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;
|
---|
[561] | 181 | double sizeDifference = ((size - minSize) / (maxSize - minSize) * (maxBubbleSize - minBubbleSize));
|
---|
[1287] | 182 | if (invertSize) return maxBubbleSize - (int)sizeDifference;
|
---|
[561] | 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() {
|
---|
[1287] | 191 | if (minX < maxX && minY < maxY) {
|
---|
[561] | 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) {
|
---|
[1287] | 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;
|
---|
[561] | 208 | }
|
---|
| 209 |
|
---|
| 210 | private void ResetViewSize() {
|
---|
| 211 | minX = double.PositiveInfinity;
|
---|
| 212 | maxX = double.NegativeInfinity;
|
---|
| 213 | minY = double.PositiveInfinity;
|
---|
| 214 | maxY = double.NegativeInfinity;
|
---|
| 215 | }
|
---|
[562] | 216 |
|
---|
[1287] | 217 | internal ResultsEntry GetResultsEntry(Point point) {
|
---|
| 218 | ResultsEntry r = null;
|
---|
[567] | 219 | IPrimitive p = points.GetPrimitive(TransformPixelToWorld(point));
|
---|
[1287] | 220 | if (p != null) {
|
---|
| 221 | primitiveToEntryDictionary.TryGetValue(p, out r);
|
---|
[567] | 222 | }
|
---|
| 223 | return r;
|
---|
[562] | 224 | }
|
---|
| 225 |
|
---|
[566] | 226 | public override void MouseDrag(Point start, Point end, MouseButtons button) {
|
---|
[1287] | 227 | if (button == MouseButtons.Left && Mode == ChartMode.Select) {
|
---|
[566] | 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);
|
---|
[569] | 234 | HeuristicLab.Charting.Rectangle rect = new HeuristicLab.Charting.Rectangle(this, minX, minY, maxX, maxY);
|
---|
[566] | 235 |
|
---|
| 236 | List<IPrimitive> primitives = new List<IPrimitive>();
|
---|
| 237 | primitives.AddRange(points.Primitives);
|
---|
| 238 |
|
---|
[1287] | 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();
|
---|
[566] | 244 | }
|
---|
| 245 | }
|
---|
[1287] | 246 | if (primitives.Count() > 0) results.FireChanged();
|
---|
[566] | 247 | } else {
|
---|
| 248 | base.MouseDrag(start, end, button);
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
[569] | 251 |
|
---|
| 252 | public override void MouseClick(Point point, MouseButtons button) {
|
---|
[1287] | 253 | if (button == MouseButtons.Left) {
|
---|
| 254 | ResultsEntry r = GetResultsEntry(point);
|
---|
[1424] | 255 | if (r != null) {
|
---|
| 256 | r.ToggleSelected();
|
---|
| 257 | results.FireChanged();
|
---|
| 258 | }
|
---|
[569] | 259 | } else {
|
---|
| 260 | base.MouseClick(point, button);
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | public override void MouseDoubleClick(Point point, MouseButtons button) {
|
---|
[1287] | 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 | }
|
---|
[569] | 272 | } else {
|
---|
| 273 | base.MouseDoubleClick(point, button);
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
[561] | 276 | }
|
---|
| 277 | }
|
---|