Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Views/3.4/Controls/DensityChart.cs

Last change on this file was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using System.Collections.Generic;
23using System.Linq;
24using System.Windows.Forms;
25
26namespace HeuristicLab.Problems.DataAnalysis.Views {
27  public partial class DensityChart : UserControl {
28    public DensityChart() {
29      InitializeComponent();
30    }
31
32    public void UpdateChart(IList<string> data, double minimumHeight = 0.1) {
33      if (data == null || !data.Any())
34        return;
35      UpdateChartWithBuckets(CalculateBuckets(data));
36    }
37
38
39    public void UpdateChart(IList<double> data, double min, double max, int numBuckets, double minimumHeight = 0.1) {
40      if (data == null || numBuckets < 0 || min > max || max < min)
41        return;
42
43      UpdateChartWithBuckets(CalculateBuckets(data, numBuckets, min, max));
44    }
45
46
47    private void UpdateChartWithBuckets(double[] buckets) {
48      // set minimum height of all non-zero buckets on 10% of maximum
49      double minHeight = buckets.Max() * 0.1;
50      for (int i = 0; i < buckets.Length; i++) {
51        if (buckets[i] >= 1 && buckets[i] < minHeight)
52          buckets[i] = minHeight;
53      }
54
55      var points = chart.Series[0].Points;
56      if (points.Count == buckets.Length) {
57        for (int i = 0; i < buckets.Length; i++)
58          points[i].SetValueY(buckets[i]);
59        chart.ChartAreas[0].RecalculateAxesScale();
60        chart.Refresh();
61      } else {
62        chart.Series[0].Points.DataBindY(buckets);
63      }
64    }
65
66    private double[] CalculateBuckets(IList<double> data, int numBuckets, double min, double max) {
67      var buckets = new double[numBuckets];
68      var bucketSize = (max - min) / numBuckets;
69      if (bucketSize > 0.0) {
70        for (int i = 0; i < data.Count; i++) {
71          int bucketIndex = (int)((data[i] - min) / bucketSize);
72          if (bucketIndex == numBuckets) {
73            bucketIndex--;
74          }
75          if (bucketIndex >= 0 && bucketIndex < buckets.Length)
76            buckets[bucketIndex] += 1.0;
77        }
78      }
79      return buckets;
80    }
81    private double[] CalculateBuckets(IList<string> data) {
82      return data.GroupBy(val => val).OrderBy(g => g.Key).Select(g => (double)g.Count()).Concat(new double[] { 0.0 }).ToArray();
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.