Free cookie consent management tool by TermsFeed Policy Generator

Changeset 572


Ignore:
Timestamp:
09/14/08 22:18:29 (16 years ago)
Author:
gkronber
Message:

added brushing for histograms. #271 (Functionality to show frequency distribution of any variable (histograms))

File:
1 edited

Legend:

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

    r571 r572  
    3737    private double maxFrequency;
    3838    private const int N_BUCKETS = 50;
     39    private const int MAX_BUCKETS = 100;
    3940    private List<Record> records;
    4041    private ResultList results;
     
    5859      foreach(Record r in results.Records) {
    5960        records.Add(r);
    60         r.OnSelectionChanged += new EventHandler(Record_OnSelectionChanged);
    6161      }
    6262      results.OnRecordAdded += new EventHandler<RecordAddedEventArgs>(results_OnRecordAdded);
     
    7373    void results_OnRecordAdded(object sender, RecordAddedEventArgs e) {
    7474      lock(records) {
    75         e.Record.OnSelectionChanged += new EventHandler(Record_OnSelectionChanged);
    7675        records.Add(e.Record);
    77       }
    78     }
    79 
    80     void Record_OnSelectionChanged(object sender, EventArgs e) {
    81       Record r = (Record)sender;
    82       IPrimitive primitive;
    83       recordToPrimitiveDictionary.TryGetValue(r, out primitive);
    84       if(primitive != null) {
    85         ((FixedSizeCircle)primitive).UpdateEnabled = false;
    86         bars.UpdateEnabled = false;
    87         if(r.Selected) {
    88           int alpha = primitive.Pen.Color.A;
    89           primitive.Pen.Color = Color.FromArgb(alpha, selectionColor);
    90           primitive.Brush = primitive.Pen.Brush;
    91           primitive.IntoForeground();
    92         } else {
    93           int alpha = primitive.Pen.Color.A;
    94           primitive.Pen.Color = Color.FromArgb(alpha, defaultColor);
    95           primitive.Brush = primitive.Pen.Brush;
    96           primitive.IntoBackground();
    97         }
    98         ((FixedSizeCircle)primitive).UpdateEnabled = true;
    99         bars.UpdateEnabled = true;
    10076      }
    10177    }
     
    11995      Group.Add(new Axis(this, 0, 0, AxisType.Both));
    12096      UpdateViewSize(0, 0);
    121       var values = records.Select(r => r.Get(dimension)).Where(
    122         x => !double.IsNaN(x) && !double.IsInfinity(x) && x != double.MinValue && x != double.MaxValue).OrderBy(x => x);
    123       IEnumerable<IGrouping<double,double>> frequencies;
    124       double bucketSize;
    125       if(dimension == Record.TARGET_VARIABLE || dimension == Record.TREE_HEIGHT || dimension == Record.TREE_SIZE) {
    126         frequencies = values.GroupBy(x => x);
    127         bucketSize = 1.0;
    128       } else {
    129         double min = values.ElementAt((int)(values.Count() * 0.05));
    130         double max = values.ElementAt((int)(values.Count() * 0.95));
    131         bucketSize = (max - min) / N_BUCKETS;
    132         frequencies = values.GroupBy(x => Math.Min(Math.Max(min, Math.Floor((x - min) / bucketSize) * bucketSize + min), max));
    133       }
    13497      Pen defaultPen = new Pen(defaultColor);
    13598      Brush defaultBrush = defaultPen.Brush;
    136       foreach(IGrouping<double, double> g in frequencies) {
     99      PaintHistogram(records, defaultPen, defaultBrush);
     100      Pen selectionPen = new Pen(selectionColor);
     101      Brush selectionBrush = selectionPen.Brush;
     102      PaintHistogram(records.Where(r => r.Selected), selectionPen, selectionBrush);
     103      Group.Add(bars);
     104      UpdateEnabled = true;
     105    }
     106
     107    private void PaintHistogram(IEnumerable<Record> records, Pen pen, Brush brush) {
     108      var values = records.Select(r => new { Record = r, Value = r.Get(dimension) }).Where(
     109        x => !double.IsNaN(x.Value) && !double.IsInfinity(x.Value) && x.Value != double.MinValue && x.Value != double.MaxValue).OrderBy(x => x.Value);
     110      if(values.Count() == 0) return;
     111      double bucketSize = 1.0;
     112      var frequencies = values.GroupBy(x => x.Value);
     113      if(frequencies.Count() > MAX_BUCKETS) {
     114        double min = values.ElementAt((int)(values.Count() * 0.05)).Value;
     115        double max = values.ElementAt((int)(values.Count() * 0.95)).Value;
     116        bucketSize = (max - min) / N_BUCKETS;
     117        frequencies = values.GroupBy(x => Math.Min(Math.Max(min, Math.Floor((x.Value - min) / bucketSize) * bucketSize + min), max));
     118      }
     119      foreach(var g in frequencies) {
    137120        double freq = g.Count();
    138121        double lower = g.Key;
    139         double upper = g.Key+bucketSize;
    140         HeuristicLab.Charting.Rectangle bar = new HeuristicLab.Charting.Rectangle(this, lower, 0, upper, freq, defaultPen, defaultBrush);
    141         primitiveToRecordsDictionary[bar] = records;
    142         if(lower == frequencies.First().Key) bar.ToolTipText = " x < "+upper+" : "+freq;
    143         else if(lower ==frequencies.Last().Key) bar.ToolTipText = "x >= "+lower+" : "+freq;
    144         else bar.ToolTipText = "x in ["+lower+" .. "+upper+"[ : "+freq;
     122        double upper = g.Key + bucketSize;
     123        HeuristicLab.Charting.Rectangle bar = new HeuristicLab.Charting.Rectangle(this, lower, 0, upper, freq, pen, brush);
     124        primitiveToRecordsDictionary[bar] = g.Select(r => r.Record).ToList();
     125        primitiveToRecordsDictionary[bar].ForEach(x => recordToPrimitiveDictionary[x] = bar);
     126        if(lower == frequencies.First().Key) bar.ToolTipText = " x < " + upper + " : " + freq;
     127        else if(lower == frequencies.Last().Key) bar.ToolTipText = "x >= " + lower + " : " + freq;
     128        else bar.ToolTipText = "x in [" + lower + " .. " + upper + "[ : " + freq;
    145129        bars.Add(bar);
    146130        UpdateViewSize(lower, freq);
    147131        UpdateViewSize(upper, freq);
    148132      }
    149       Group.Add(bars);
    150       UpdateEnabled = true;
    151133    }
    152134
     
    166148      if(x < minX) minX = x;
    167149      if(x > maxX) maxX = x;
    168       if(freq  > maxFrequency) maxFrequency = freq;
     150      if(freq > maxFrequency) maxFrequency = freq;
    169151    }
    170152
     
    187169      if(button == MouseButtons.Left) {
    188170        List<Record> rs = GetRecords(point);
     171        UpdateEnabled = false;
    189172        if(rs != null) rs.ForEach(r => r.ToggleSelected());
     173        UpdateEnabled = true;
    190174        results.FireChanged();
    191175      } else {
Note: See TracChangeset for help on using the changeset viewer.