- Timestamp:
- 08/14/09 15:35:10 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.CEDMA.Charting/3.3/BubbleChart.cs
r2240 r2289 27 27 using HeuristicLab.Charting; 28 28 using System.Windows.Forms; 29 using HeuristicLab.CEDMA.Core;30 29 using HeuristicLab.PluginInfrastructure; 31 30 using HeuristicLab.Core; 32 31 using System.Diagnostics; 32 using HeuristicLab.SparseMatrix; 33 33 34 34 namespace HeuristicLab.CEDMA.Charting { 35 public class BubbleChart : Chart { 36 private const string X_JITTER = "__X_JITTER"; 37 private const string Y_JITTER = "__Y_JITTER"; 35 public abstract class BubbleChart : Chart { 38 36 private const double maxXJitterPercent = 0.05; 39 37 private const double maxYJitterPercent = 0.05; … … 55 53 private double maxX = double.NegativeInfinity; 56 54 private double maxY = double.NegativeInfinity; 57 private List<ResultsEntry> filteredEntries; 58 private Results results; 59 private Dictionary<IPrimitive, ResultsEntry> primitiveToEntryDictionary; 60 private Random random = new Random(); 61 private Group points; 62 63 public BubbleChart(Results results, PointD lowerLeft, PointD upperRight) 55 56 protected Group points; 57 protected VisualMatrix matrix; 58 protected Dictionary<IPrimitive, VisualMatrixRow> primitiveToMatrixRowDictionary; 59 60 public BubbleChart(VisualMatrix matrix, PointD lowerLeft, PointD upperRight) 64 61 : base(lowerLeft, upperRight) { 65 primitiveToEntryDictionary = new Dictionary<IPrimitive, ResultsEntry>(); 66 this.results = results; 67 filteredEntries = new List<ResultsEntry>(); 68 69 foreach (var resultsEntry in results.GetEntries()) { 70 if (resultsEntry.Get(X_JITTER) == null) 71 resultsEntry.Set(X_JITTER, random.NextDouble() * 2.0 - 1.0); 72 if (resultsEntry.Get(Y_JITTER) == null) 73 resultsEntry.Set(Y_JITTER, random.NextDouble() * 2.0 - 1.0); 74 } 75 results.Changed += new EventHandler(results_Changed); 76 } 77 78 void results_Changed(object sender, EventArgs e) { 79 Repaint(); 80 EnforceUpdate(); 81 } 82 83 public BubbleChart(Results results, double x1, double y1, double x2, double y2) 84 : this(results, new PointD(x1, y1), new PointD(x2, y2)) { 62 primitiveToMatrixRowDictionary = new Dictionary<IPrimitive, VisualMatrixRow>(); 63 this.matrix = matrix; 64 } 65 66 public BubbleChart(VisualMatrix matrix, double x1, double y1, double x2, double y2) 67 : this(matrix, new PointD(x1, y1), new PointD(x2, y2)) { 85 68 } 86 69 … … 103 86 } 104 87 105 internalvoid SetJitter(double xJitterFactor, double yJitterFactor) {88 public void SetJitter(double xJitterFactor, double yJitterFactor) { 106 89 this.xJitterFactor = xJitterFactor * maxXJitterPercent * Size.Width; 107 90 this.yJitterFactor = yJitterFactor * maxYJitterPercent * Size.Height; … … 115 98 } 116 99 117 pr ivatevoid Repaint() {100 protected void Repaint() { 118 101 if (xDimension == null || yDimension == null) return; 119 102 double maxSize = 1; 120 103 double minSize = 1; 121 if (sizeDimension != null && results.OrdinalVariables.Contains(sizeDimension)) {122 var sizes = results.GetEntries()104 if (sizeDimension != null && matrix.OrdinalVariables.Contains(sizeDimension)) { 105 var sizes = matrix.Rows 123 106 .Select(x => Convert.ToDouble(x.Get(sizeDimension))) 124 107 .Where(size => !double.IsInfinity(size) && size != double.MaxValue && size != double.MinValue) … … 132 115 UpdateEnabled = false; 133 116 Group.Clear(); 134 primitiveTo EntryDictionary.Clear();117 primitiveToMatrixRowDictionary.Clear(); 135 118 points = new Group(this); 136 119 Group.Add(new Axis(this, 0, 0, AxisType.Both)); 137 120 UpdateViewSize(0, 0, TransformPixelToWorld(new Size(5, 0)).Width); 138 foreach ( ResultsEntry r in results.GetEntries().Where(x => x.Visible)) {121 foreach (VisualMatrixRow r in matrix.Rows.Where(x => x.Visible)) { 139 122 List<double> xs = new List<double>(); 140 123 List<double> ys = new List<double>(); … … 142 125 List<object> actualYValues = new List<object>(); 143 126 int size; 144 if ( results.OrdinalVariables.Contains(xDimension) && r.Get(xDimension) != null) {145 xs.Add(Convert.ToDouble(r.Get(xDimension)) + (double)r.Get(X_JITTER)* xJitterFactor);127 if (matrix.OrdinalVariables.Contains(xDimension) && r.Get(xDimension) != null) { 128 xs.Add(Convert.ToDouble(r.Get(xDimension)) + r.XJitter * xJitterFactor); 146 129 actualXValues.Add(r.Get(xDimension)); 147 } else if ( results.CategoricalVariables.Contains(xDimension) && r.Get(xDimension) != null) {148 xs.Add( results.IndexOfCategoricalValue(xDimension, r.Get(xDimension)) + (double)r.Get(X_JITTER)* xJitterFactor);130 } else if (matrix.CategoricalVariables.Contains(xDimension) && r.Get(xDimension) != null) { 131 xs.Add(matrix.IndexOfCategoricalValue(xDimension, r.Get(xDimension)) + r.XJitter * xJitterFactor); 149 132 actualXValues.Add(r.Get(xDimension)); 150 } else if ( results.MultiDimensionalCategoricalVariables.Contains(xDimension)) {133 } else if (matrix.MultiDimensionalCategoricalVariables.Contains(xDimension)) { 151 134 var path = xDimension.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()); 152 IEnumerable< ResultsEntry> subEntries = (IEnumerable<ResultsEntry>)r.Get(path.ElementAt(0));153 foreach ( ResultsEntry subEntry in subEntries) {154 if (sub Entry.Get(path.ElementAt(1)) != null) {155 xs.Add( results.IndexOfCategoricalValue(xDimension, subEntry.Get(path.ElementAt(1))) + (double)r.Get(X_JITTER)* xJitterFactor);156 actualXValues.Add(sub Entry.Get(path.ElementAt(1)));157 } 158 } 159 } else if ( results.MultiDimensionalOrdinalVariables.Contains(xDimension)) {135 IEnumerable<VisualMatrixRow> subRows = (IEnumerable<VisualMatrixRow>)r.Get(path.ElementAt(0)); 136 foreach (VisualMatrixRow subRow in subRows) { 137 if (subRow.Get(path.ElementAt(1)) != null) { 138 xs.Add(matrix.IndexOfCategoricalValue(xDimension, subRow.Get(path.ElementAt(1))) + r.YJitter * xJitterFactor); 139 actualXValues.Add(subRow.Get(path.ElementAt(1))); 140 } 141 } 142 } else if (matrix.MultiDimensionalOrdinalVariables.Contains(xDimension)) { 160 143 var path = xDimension.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()); 161 IEnumerable< ResultsEntry> subEntries = (IEnumerable<ResultsEntry>)r.Get(path.ElementAt(0));162 foreach ( ResultsEntry subEntry in subEntries) {163 if (sub Entry.Get(path.ElementAt(1)) != null) {164 xs.Add(Convert.ToDouble(sub Entry.Get(path.ElementAt(1))) + (double)r.Get(X_JITTER)* xJitterFactor);165 actualXValues.Add(sub Entry.Get(path.ElementAt(1)));166 } 167 } 168 } 169 if ( results.OrdinalVariables.Contains(yDimension) && r.Get(yDimension) != null) {170 ys.Add(Convert.ToDouble(r.Get(yDimension)) + (double)r.Get(Y_JITTER)* yJitterFactor);144 IEnumerable<VisualMatrixRow> subRows = (IEnumerable<VisualMatrixRow>)r.Get(path.ElementAt(0)); 145 foreach (VisualMatrixRow subRow in subRows) { 146 if (subRow.Get(path.ElementAt(1)) != null) { 147 xs.Add(Convert.ToDouble(subRow.Get(path.ElementAt(1))) + r.XJitter * xJitterFactor); 148 actualXValues.Add(subRow.Get(path.ElementAt(1))); 149 } 150 } 151 } 152 if (matrix.OrdinalVariables.Contains(yDimension) && r.Get(yDimension) != null) { 153 ys.Add(Convert.ToDouble(r.Get(yDimension)) + r.YJitter * yJitterFactor); 171 154 actualYValues.Add(r.Get(yDimension)); 172 } else if ( results.CategoricalVariables.Contains(yDimension) && r.Get(yDimension) != null) {173 ys.Add( results.IndexOfCategoricalValue(yDimension, r.Get(yDimension)) + (double)r.Get(Y_JITTER)* yJitterFactor);155 } else if (matrix.CategoricalVariables.Contains(yDimension) && r.Get(yDimension) != null) { 156 ys.Add(matrix.IndexOfCategoricalValue(yDimension, r.Get(yDimension)) + r.YJitter * yJitterFactor); 174 157 actualYValues.Add(r.Get(yDimension)); 175 } else if ( results.MultiDimensionalCategoricalVariables.Contains(yDimension)) {158 } else if (matrix.MultiDimensionalCategoricalVariables.Contains(yDimension)) { 176 159 var path = yDimension.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()); 177 IEnumerable< ResultsEntry> subEntries = (IEnumerable<ResultsEntry>)r.Get(path.ElementAt(0));178 foreach ( ResultsEntry subEntry in subEntries) {179 if (sub Entry.Get(path.ElementAt(1)) != null) {180 ys.Add( results.IndexOfCategoricalValue(yDimension, subEntry.Get(path.ElementAt(1))) + (double)r.Get(Y_JITTER)* yJitterFactor);181 actualYValues.Add(sub Entry.Get(path.ElementAt(1)));182 } 183 } 184 } else if ( results.MultiDimensionalOrdinalVariables.Contains(yDimension)) {160 IEnumerable<VisualMatrixRow> subRows = (IEnumerable<VisualMatrixRow>)r.Get(path.ElementAt(0)); 161 foreach (VisualMatrixRow subRow in subRows) { 162 if (subRow.Get(path.ElementAt(1)) != null) { 163 ys.Add(matrix.IndexOfCategoricalValue(yDimension, subRow.Get(path.ElementAt(1))) + r.YJitter * yJitterFactor); 164 actualYValues.Add(subRow.Get(path.ElementAt(1))); 165 } 166 } 167 } else if (matrix.MultiDimensionalOrdinalVariables.Contains(yDimension)) { 185 168 var path = yDimension.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()); 186 IEnumerable< ResultsEntry> subEntries = (IEnumerable<ResultsEntry>)r.Get(path.ElementAt(0));187 foreach ( ResultsEntry subEntry in subEntries) {188 if (sub Entry.Get(path.ElementAt(1)) != null) {189 ys.Add(Convert.ToDouble(sub Entry.Get(path.ElementAt(1))) + (double)r.Get(Y_JITTER)* yJitterFactor);190 actualYValues.Add(sub Entry.Get(path.ElementAt(1)));169 IEnumerable<VisualMatrixRow> subRows = (IEnumerable<VisualMatrixRow>)r.Get(path.ElementAt(0)); 170 foreach (VisualMatrixRow subRow in subRows) { 171 if (subRow.Get(path.ElementAt(1)) != null) { 172 ys.Add(Convert.ToDouble(subRow.Get(path.ElementAt(1))) + r.YJitter * yJitterFactor); 173 actualYValues.Add(subRow.Get(path.ElementAt(1))); 191 174 } 192 175 } … … 208 191 if (double.IsInfinity(x) || x == double.MaxValue || x == double.MinValue) x = double.NaN; 209 192 if (double.IsInfinity(y) || y == double.MaxValue || y == double.MinValue) y = double.NaN; 210 if (!double.IsNaN(x) && !double.IsNaN(y) && IsReasonablePoint(new PointD(x, y))) {193 if (!double.IsNaN(x) && !double.IsNaN(y) && IsReasonablePoint(new PointD(x, y))) { 211 194 string actualXValue = actualXValues[Math.Min(i, actualXValues.Count() - 1)].ToString(); 212 195 string actualYValue = actualYValues[Math.Min(i, actualYValues.Count() - 1)].ToString(); … … 221 204 points.Add(c); 222 205 if (!r.Selected) c.IntoBackground(); 223 primitiveTo EntryDictionary[c] = r;206 primitiveToMatrixRowDictionary[c] = r; 224 207 } 225 208 } … … 230 213 231 214 private bool IsReasonablePoint(PointD pointD) { 232 return pointD.X > LowerLeft.X && pointD.X < UpperRight.X && pointD.Y > LowerLeft.Y && pointD.Y < UpperRight.Y; 215 return pointD.X > LowerLeft.X && pointD.X < UpperRight.X && pointD.Y > LowerLeft.Y && pointD.Y < UpperRight.Y; 233 216 } 234 217 … … 274 257 } 275 258 276 internal ResultsEntry GetResultsEntry(Point point) {277 ResultsEntryr = null;259 protected VisualMatrixRow GetMatrixRow(Point point) { 260 VisualMatrixRow r = null; 278 261 IPrimitive p = points.GetPrimitive(TransformPixelToWorld(point)); 279 262 if (p != null) { 280 primitiveTo EntryDictionary.TryGetValue(p, out r);263 primitiveToMatrixRowDictionary.TryGetValue(p, out r); 281 264 } 282 265 return r; 283 266 } 284 267 285 public override void MouseDrag(Point start, Point end, MouseButtons button) { 286 if (button == MouseButtons.Left && Mode == ChartMode.Select) { 287 PointD a = TransformPixelToWorld(start); 288 PointD b = TransformPixelToWorld(end); 289 double minX = Math.Min(a.X, b.X); 290 double minY = Math.Min(a.Y, b.Y); 291 double maxX = Math.Max(a.X, b.X); 292 double maxY = Math.Max(a.Y, b.Y); 293 HeuristicLab.Charting.Rectangle rect = new HeuristicLab.Charting.Rectangle(this, minX, minY, maxX, maxY); 294 295 List<IPrimitive> primitives = new List<IPrimitive>(); 296 primitives.AddRange(points.Primitives); 297 298 foreach (FixedSizeCircle p in primitives) { 299 if (rect.ContainsPoint(p.Point)) { 300 ResultsEntry r; 301 primitiveToEntryDictionary.TryGetValue(p, out r); 302 if (r != null) r.ToggleSelected(); 303 } 304 } 305 if (primitives.Count() > 0) results.FireChanged(); 306 } else { 307 base.MouseDrag(start, end, button); 308 } 309 } 310 311 public override void MouseClick(Point point, MouseButtons button) { 312 if (button == MouseButtons.Left) { 313 ResultsEntry r = GetResultsEntry(point); 314 if (r != null) { 315 r.ToggleSelected(); 316 results.FireChanged(); 317 } 318 } else { 319 base.MouseClick(point, button); 320 } 321 } 322 323 public override void MouseDoubleClick(Point point, MouseButtons button) { 324 if (button == MouseButtons.Left) { 325 ResultsEntry entry = GetResultsEntry(point); 326 if (entry != null) { 327 var model = (IItem)PersistenceManager.RestoreFromGZip((byte[])entry.Get("PersistedData")); 328 PluginManager.ControlManager.ShowControl(model.CreateView()); 329 } 330 } else { 331 base.MouseDoubleClick(point, button); 332 } 333 } 334 335 internal void ToggleSelected() { 336 foreach (ResultsEntry entry in results.GetEntries()) { 268 public virtual void ToggleSelected() { 269 foreach (VisualMatrixRow row in matrix.Rows) { 270 row.ToggleSelected(); 271 } 272 matrix.FireChanged(); 273 } 274 275 public virtual void ClearSelection() { 276 foreach (VisualMatrixRow entry in matrix.Rows.Where(x => x.Selected)) { 337 277 entry.ToggleSelected(); 338 278 } 339 results.FireChanged(); 340 } 341 342 internal void ClearSelection() { 343 foreach (ResultsEntry entry in results.GetEntries().Where(x=>x.Selected)) { 344 entry.ToggleSelected(); 345 } 346 results.FireChanged(); 347 } 348 349 internal void ApplyFilter(Func<ResultsEntry, bool> filterPred) { 350 foreach (ResultsEntry r in results.GetEntries()) { 279 matrix.FireChanged(); 280 } 281 282 public virtual void ApplyFilter(Func<VisualMatrixRow, bool> filterPred) { 283 foreach (VisualMatrixRow r in matrix.Rows) { 351 284 if (filterPred(r)) { 352 285 r.Visible = false; 353 286 r.Selected = false; 354 filteredEntries.Add(r); 355 } 356 } 357 results.FireChanged(); 358 } 359 360 internal void ClearFilter() { 361 foreach (ResultsEntry r in filteredEntries) { 287 } 288 } 289 matrix.FireChanged(); 290 } 291 292 public virtual void ClearFilter() { 293 foreach (VisualMatrixRow r in matrix.Rows.Where(x => !x.Visible)) { 362 294 r.Visible = true; 363 295 } 364 filteredEntries.Clear(); 365 results.FireChanged(); 296 matrix.FireChanged(); 366 297 } 367 298 }
Note: See TracChangeset
for help on using the changeset viewer.