Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/11/09 19:30:54 (16 years ago)
Author:
gkronber
Message:

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

Location:
branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Charting
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Charting/BubbleChart.cs

    r1106 r1108  
    3131namespace HeuristicLab.CEDMA.Charting {
    3232  public class BubbleChart : Chart {
     33    private const string X_JITTER = "__X_JITTER";
     34    private const string Y_JITTER = "__Y_JITTER";
    3335    private const double maxXJitterPercent = 0.05;
    3436    private const double maxYJitterPercent = 0.05;
     
    5052    private double maxX = double.NegativeInfinity;
    5153    private double maxY = double.NegativeInfinity;
    52     private List<Record> records;
     54    private List<ResultsEntry> records;
    5355    private Results results;
    54     private Dictionary<IPrimitive, Record> primitiveToRecordDictionary;
    55     private Dictionary<Record, IPrimitive> recordToPrimitiveDictionary;
     56    private Dictionary<IPrimitive, ResultsEntry> primitiveToEntryDictionary;
     57    private Dictionary<ResultsEntry, IPrimitive> entryToPrimitiveDictionary;
    5658    private Random random = new Random();
    5759    private Group points;
     
    5961    public BubbleChart(Results results, PointD lowerLeft, PointD upperRight)
    6062      : base(lowerLeft, upperRight) {
    61       records = new List<Record>();
    62       primitiveToRecordDictionary = new Dictionary<IPrimitive, Record>();
    63       recordToPrimitiveDictionary = new Dictionary<Record, IPrimitive>();
     63      records = new List<ResultsEntry>();
     64      primitiveToEntryDictionary = new Dictionary<IPrimitive, ResultsEntry>();
     65      entryToPrimitiveDictionary = new Dictionary<ResultsEntry, IPrimitive>();
    6466      this.results = results;
    65       foreach(Record r in results.Records) {
    66         records.Add(r);
    67      }
    68       results.OnRecordAdded += new EventHandler<RecordAddedEventArgs>(results_OnRecordAdded);
    69       results.Changed += new EventHandler(results_Changed);
    7067    }
    7168
     
    7572    }
    7673
    77     public BubbleChart(ResultList results, double x1, double y1, double x2, double y2)
     74    public BubbleChart(Results results, double x1, double y1, double x2, double y2)
    7875      : this(results, new PointD(x1, y1), new PointD(x2, y2)) {
    7976    }
    8077
    81     void results_OnRecordAdded(object sender, RecordAddedEventArgs e) {
    82       lock(records) {
    83         records.Add(e.Record);
    84       }
    85     }
     78    //void results_OnRecordAdded(object sender, RecordAddedEventArgs e) {
     79    //  lock(records) {
     80    //    records.Add(e.Record);
     81    //  }
     82    //}
    8683
    8784    public void SetBubbleSizeDimension(string dimension, bool inverted) {
     
    9390
    9491    public void ShowXvsY(string xDimension, string yDimension) {
    95       if(this.xDimension != xDimension || this.yDimension != yDimension) {
     92      if (this.xDimension != xDimension || this.yDimension != yDimension) {
    9693        this.xDimension = xDimension;
    9794        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        }
    98101        ResetViewSize();
    99102        Repaint();
     
    110113
    111114    private void Repaint() {
    112       if(xDimension == null || yDimension == null) return;
    113       lock(records) {
    114         double maxSize = 1;
    115         double minSize = 1;
    116         if(sizeDimension != null) {
    117           var sizes = records.Select(r => r.Get(sizeDimension)).Where(x => !double.IsInfinity(x) && x != double.MaxValue && x != double.MinValue).OrderBy(x=>x);
     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);
    118124          minSize = sizes.ElementAt((int)(sizes.Count() * 0.1));
    119125          maxSize = sizes.ElementAt((int)(sizes.Count() * 0.9));
    120126        }
    121         UpdateEnabled = false;
    122         Group.Clear();
    123         primitiveToRecordDictionary.Clear();
    124         recordToPrimitiveDictionary.Clear();
    125         points = new Group(this);
    126         Group.Add(new Axis(this, 0, 0, AxisType.Both));
    127         UpdateViewSize(0, 0, 5);
    128         foreach(Record r in records) {
    129           double x = r.Get(xDimension) + r.Get(Record.X_JITTER) * xJitterFactor;
    130           double y = r.Get(yDimension) + r.Get(Record.Y_JITTER) * yJitterFactor;
    131           int size = CalculateSize(r.Get(sizeDimension), minSize, maxSize);
    132 
    133           if(double.IsInfinity(x) || x == double.MaxValue || x == double.MinValue) x = double.NaN;
    134           if(double.IsInfinity(y) || y == double.MaxValue || y == double.MinValue) y = double.NaN;
    135           if(!double.IsNaN(x) && !double.IsNaN(y)) {
    136             UpdateViewSize(x, y, size);
    137             int alpha = CalculateAlpha(size);
    138             Pen pen = new Pen(Color.FromArgb(alpha, r.Selected ? selectionColor : defaultColor));
    139             Brush brush = pen.Brush;
    140             FixedSizeCircle c = new FixedSizeCircle(this, x, y, size, pen, brush);
    141             c.ToolTipText = r.GetToolTipText();
    142             points.Add(c);
    143             if(!r.Selected) c.IntoBackground();
    144             primitiveToRecordDictionary[c] = r;
    145             recordToPrimitiveDictionary[r] = c;
    146           }
    147         }
    148         Group.Add(points);
    149         UpdateEnabled = true;
    150       }
     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;
    151169    }
    152170
    153171    private int CalculateSize(double size, double minSize, double maxSize) {
    154       if(double.IsNaN(size) || double.IsInfinity(size) || size == double.MaxValue || size == double.MinValue) return minBubbleSize;
    155       if(size > maxSize) size = maxSize;
    156       if(size < minSize) size = minSize;
     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;
    157176      double sizeDifference = ((size - minSize) / (maxSize - minSize) * (maxBubbleSize - minBubbleSize));
    158       if(invertSize) return maxBubbleSize - (int)sizeDifference;
     177      if (invertSize) return maxBubbleSize - (int)sizeDifference;
    159178      else return minBubbleSize + (int)sizeDifference;
    160179    }
     
    165184
    166185    private void ZoomToViewSize() {
    167       if(minX < maxX && minY < maxY) {
     186      if (minX < maxX && minY < maxY) {
    168187        // enlarge view by 5% on each side
    169188        double width = maxX - minX;
     
    178197
    179198    private void UpdateViewSize(double x, double y, double size) {
    180       if(x - size < minX) minX = x - size;
    181       if(x + size > maxX) maxX = x + size;
    182       if(y - size < minY) minY = y + size;
    183       if(y + size > maxY) maxY = y + 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;
    184203    }
    185204
     
    191210    }
    192211
    193     internal Record GetRecord(Point point) {
    194       Record r = null;
     212    internal ResultsEntry GetResultsEntry(Point point) {
     213      ResultsEntry r = null;
    195214      IPrimitive p = points.GetPrimitive(TransformPixelToWorld(point));
    196       if(p != null) {
    197         primitiveToRecordDictionary.TryGetValue(p, out r);
     215      if (p != null) {
     216        primitiveToEntryDictionary.TryGetValue(p, out r);
    198217      }
    199218      return r;
     
    202221
    203222    public override void MouseDrag(Point start, Point end, MouseButtons button) {
    204       if(button == MouseButtons.Left && Mode == ChartMode.Select) {
     223      if (button == MouseButtons.Left && Mode == ChartMode.Select) {
    205224        PointD a = TransformPixelToWorld(start);
    206225        PointD b = TransformPixelToWorld(end);
     
    214233        primitives.AddRange(points.Primitives);
    215234
    216         foreach(FixedSizeCircle p in primitives) {
    217           if(rect.ContainsPoint(p.Point)) {
    218             Record r;
    219             primitiveToRecordDictionary.TryGetValue(p, out r);
    220             if(r != null) r.ToggleSelected();
     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();
    221240          }
    222241        }
    223         results.FireChanged();
     242        // results.FireChanged();
    224243      } else {
    225244        base.MouseDrag(start, end, button);
     
    228247
    229248    public override void MouseClick(Point point, MouseButtons button) {
    230       if(button == MouseButtons.Left) {
    231         Record r = GetRecord(point);
    232         if(r != null) r.ToggleSelected();
    233         results.FireChanged();
     249      if (button == MouseButtons.Left) {
     250        ResultsEntry r = GetResultsEntry(point);
     251        if (r != null) r.ToggleSelected();
     252        //        results.FireChanged();
    234253      } else {
    235254        base.MouseClick(point, button);
     
    237256    }
    238257
    239     public override void MouseDoubleClick(Point point, MouseButtons button) {
    240       if(button == MouseButtons.Left) {
    241         Record r = GetRecord(point);
    242         if(r != null) r.OpenModel();
    243       } else {
    244         base.MouseDoubleClick(point, button);
    245       }
    246     }
     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    //}
    247266  }
    248267}
  • branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Charting/BubbleChartView.cs

    r1106 r1108  
    1212
    1313namespace HeuristicLab.CEDMA.Charting {
    14   public partial class BubbleChartView : ViewBase {
     14  public partial class BubbleChartView : ViewBase, IResultsView {
    1515    private Results results;
    1616    private const string CONSTANT_SIZE = "<constant>";
    1717    private Label pleaseSelectAxisLabel = new Label();
    1818
    19     public BubbleChartView(Results results) {
    20       this.results = results;
     19    public BubbleChartView() {
    2120      InitializeComponent();
    22       bubbleChartControl.Chart = new BubbleChart(results, 0, 0, 100, 100);
    23       xAxisComboBox.Items.AddRange(results.OrdinalVariableNames);
    24       yAxisComboBox.Items.AddRange(results.OrdinalVariableNames);
    25       sizeComboBox.Items.Add(CONSTANT_SIZE);
    26       sizeComboBox.Items.AddRange(results.VariableNames);
    27       sizeComboBox.SelectedItem = sizeComboBox.Items[0];
    28       sizeComboBox.Enabled = false;
    29       invertCheckbox.Enabled = false;
    30       sizeLabel.Enabled = false;
    31       yAxisComboBox.SelectedItem = yAxisComboBox.Items[0];
    32       xAxisComboBox.SelectedItem = xAxisComboBox.Items[0];
    3321    }
    3422
     
    3927    private void UpdateChart() {
    4028      if (xAxisComboBox.SelectedItem == null || yAxisComboBox.SelectedItem == null) return;
    41       xJitterlabel.Enabled = true;
    42       xTrackBar.Enabled = true;
    43       yJitterLabel.Enabled = true;
    44       yTrackBar.Enabled = true;
    45       sizeComboBox.Enabled = true;
    46       invertCheckbox.Enabled = true;
    47       sizeLabel.Enabled = true;
    4829      bubbleChartControl.Chart.ShowXvsY((string)xAxisComboBox.SelectedItem, (string)yAxisComboBox.SelectedItem);
    4930    }
     
    6445      }
    6546    }
     47
     48    #region IResultsView Members
     49
     50    public Control Control {
     51      get { return this; }
     52    }
     53
     54    string IResultsView.Name {
     55      get { return "Bubble chart"; }
     56    }
     57
     58    public void ShowResults(Results results) {
     59      this.results = results;
     60      bubbleChartControl.Chart = new BubbleChart(results, 0, 0, 100, 100);
     61      xAxisComboBox.Items.AddRange(Results.OrdinalVariables);
     62      xAxisComboBox.Items.AddRange(Results.CategoricalVariables);
     63      yAxisComboBox.Items.AddRange(Results.OrdinalVariables);
     64      yAxisComboBox.Items.AddRange(Results.CategoricalVariables);
     65      sizeComboBox.Items.Add(CONSTANT_SIZE);
     66      sizeComboBox.Items.AddRange(Results.OrdinalVariables);
     67      sizeComboBox.SelectedItem = sizeComboBox.Items[0];
     68      yAxisComboBox.SelectedItem = yAxisComboBox.Items[0];
     69      xAxisComboBox.SelectedItem = xAxisComboBox.Items[0];
     70    }
     71
     72    #endregion
    6673  }
    6774}
  • branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Charting/HeuristicLab.CEDMA.Charting.csproj

    r1106 r1108  
    6666  </ItemGroup>
    6767  <ItemGroup>
     68    <Compile Include="BubbleChart.cs" />
     69    <Compile Include="BubbleChartControl.cs">
     70      <SubType>UserControl</SubType>
     71    </Compile>
     72    <Compile Include="BubbleChartControl.Designer.cs">
     73      <DependentUpon>BubbleChartControl.cs</DependentUpon>
     74    </Compile>
     75    <Compile Include="BubbleChartView.cs">
     76      <SubType>UserControl</SubType>
     77    </Compile>
     78    <Compile Include="BubbleChartView.Designer.cs">
     79      <DependentUpon>BubbleChartView.cs</DependentUpon>
     80    </Compile>
    6881    <Compile Include="HeuristicLabCedmaChartingPlugin.cs" />
    6982    <Compile Include="Properties\AssemblyInfo.cs" />
     
    98111      <Name>HeuristicLab.Data</Name>
    99112    </ProjectReference>
    100     <ProjectReference Include="..\HeuristicLab.GP.StructureIdentification\HeuristicLab.GP.StructureIdentification.csproj">
    101       <Project>{74223A32-C726-4978-BE78-37113A18373C}</Project>
    102       <Name>HeuristicLab.GP.StructureIdentification</Name>
    103     </ProjectReference>
    104     <ProjectReference Include="..\HeuristicLab.GP\HeuristicLab.GP.csproj">
    105       <Project>{1F1CF3ED-374C-4288-995B-93F6B872F571}</Project>
    106       <Name>HeuristicLab.GP</Name>
    107     </ProjectReference>
    108113    <ProjectReference Include="..\HeuristicLab.Logging\HeuristicLab.Logging.csproj">
    109114      <Project>{4095C92C-5A4C-44BC-9963-5F384CF5CC3F}</Project>
     
    118123    <None Include="HeuristicLab.snk" />
    119124    <None Include="Properties\AssemblyInfo.frame" />
     125  </ItemGroup>
     126  <ItemGroup>
     127    <EmbeddedResource Include="BubbleChartControl.resx">
     128      <DependentUpon>BubbleChartControl.cs</DependentUpon>
     129    </EmbeddedResource>
     130    <EmbeddedResource Include="BubbleChartView.resx">
     131      <DependentUpon>BubbleChartView.cs</DependentUpon>
     132    </EmbeddedResource>
    120133  </ItemGroup>
    121134  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Note: See TracChangeset for help on using the changeset viewer.