Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/14/09 15:35:10 (15 years ago)
Author:
mkommend
Message:
  • added VisualMatrix to represent data in BubbleChart
  • made BubbleChart abstract and added inherited ModelingBubbleChart, which reacts as the BubbleChart
  • moved classes between CEDMA.Core and CEDMA.Charting
  • deleted unnecessary classes (results, resultsentry)

(ticket #723)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.CEDMA.Charting/3.3/BubbleChart.cs

    r2240 r2289  
    2727using HeuristicLab.Charting;
    2828using System.Windows.Forms;
    29 using HeuristicLab.CEDMA.Core;
    3029using HeuristicLab.PluginInfrastructure;
    3130using HeuristicLab.Core;
    3231using System.Diagnostics;
     32using HeuristicLab.SparseMatrix;
    3333
    3434namespace 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 {
    3836    private const double maxXJitterPercent = 0.05;
    3937    private const double maxYJitterPercent = 0.05;
     
    5553    private double maxX = double.NegativeInfinity;
    5654    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)
    6461      : 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)) {
    8568    }
    8669
     
    10386    }
    10487
    105     internal void SetJitter(double xJitterFactor, double yJitterFactor) {
     88    public void SetJitter(double xJitterFactor, double yJitterFactor) {
    10689      this.xJitterFactor = xJitterFactor * maxXJitterPercent * Size.Width;
    10790      this.yJitterFactor = yJitterFactor * maxYJitterPercent * Size.Height;
     
    11598    }
    11699
    117     private void Repaint() {
     100    protected void Repaint() {
    118101      if (xDimension == null || yDimension == null) return;
    119102      double maxSize = 1;
    120103      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
    123106          .Select(x => Convert.ToDouble(x.Get(sizeDimension)))
    124107          .Where(size => !double.IsInfinity(size) && size != double.MaxValue && size != double.MinValue)
     
    132115      UpdateEnabled = false;
    133116      Group.Clear();
    134       primitiveToEntryDictionary.Clear();
     117      primitiveToMatrixRowDictionary.Clear();
    135118      points = new Group(this);
    136119      Group.Add(new Axis(this, 0, 0, AxisType.Both));
    137120      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)) {
    139122        List<double> xs = new List<double>();
    140123        List<double> ys = new List<double>();
     
    142125        List<object> actualYValues = new List<object>();
    143126        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);
    146129          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);
    149132          actualXValues.Add(r.Get(xDimension));
    150         } else if (results.MultiDimensionalCategoricalVariables.Contains(xDimension)) {
     133        } else if (matrix.MultiDimensionalCategoricalVariables.Contains(xDimension)) {
    151134          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 (subEntry.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(subEntry.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)) {
    160143          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 (subEntry.Get(path.ElementAt(1)) != null) {
    164               xs.Add(Convert.ToDouble(subEntry.Get(path.ElementAt(1))) + (double)r.Get(X_JITTER) * xJitterFactor);
    165               actualXValues.Add(subEntry.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);
    171154          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);
    174157          actualYValues.Add(r.Get(yDimension));
    175         } else if (results.MultiDimensionalCategoricalVariables.Contains(yDimension)) {
     158        } else if (matrix.MultiDimensionalCategoricalVariables.Contains(yDimension)) {
    176159          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 (subEntry.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(subEntry.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)) {
    185168          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 (subEntry.Get(path.ElementAt(1)) != null) {
    189               ys.Add(Convert.ToDouble(subEntry.Get(path.ElementAt(1))) + (double)r.Get(Y_JITTER) * yJitterFactor);
    190               actualYValues.Add(subEntry.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)));
    191174            }
    192175          }
     
    208191          if (double.IsInfinity(x) || x == double.MaxValue || x == double.MinValue) x = double.NaN;
    209192          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))) {
    211194            string actualXValue = actualXValues[Math.Min(i, actualXValues.Count() - 1)].ToString();
    212195            string actualYValue = actualYValues[Math.Min(i, actualYValues.Count() - 1)].ToString();
     
    221204            points.Add(c);
    222205            if (!r.Selected) c.IntoBackground();
    223             primitiveToEntryDictionary[c] = r;
     206            primitiveToMatrixRowDictionary[c] = r;
    224207          }
    225208        }
     
    230213
    231214    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;
    233216    }
    234217
     
    274257    }
    275258
    276     internal ResultsEntry GetResultsEntry(Point point) {
    277       ResultsEntry r = null;
     259    protected VisualMatrixRow GetMatrixRow(Point point) {
     260      VisualMatrixRow r = null;
    278261      IPrimitive p = points.GetPrimitive(TransformPixelToWorld(point));
    279262      if (p != null) {
    280         primitiveToEntryDictionary.TryGetValue(p, out r);
     263        primitiveToMatrixRowDictionary.TryGetValue(p, out r);
    281264      }
    282265      return r;
    283266    }
    284267
    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)) {
    337277        entry.ToggleSelected();
    338278      }
    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) {
    351284        if (filterPred(r)) {
    352285          r.Visible = false;
    353286          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)) {
    362294        r.Visible = true;
    363295      }
    364       filteredEntries.Clear();
    365       results.FireChanged();
     296      matrix.FireChanged();
    366297    }
    367298  }
Note: See TracChangeset for help on using the changeset viewer.