1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Windows.Forms;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
27 | public partial class DensityChart : UserControl {
|
---|
28 | public DensityChart() {
|
---|
29 | InitializeComponent();
|
---|
30 | }
|
---|
31 |
|
---|
32 | public void UpdateChart(IList<double> data, double min, double max, int numBuckets, double minimumHeight = 0.1) {
|
---|
33 | if (data == null || numBuckets < 0 || min > max || max < min)
|
---|
34 | return;
|
---|
35 |
|
---|
36 | var buckets = CalculateBuckets(data, numBuckets, min, max);
|
---|
37 |
|
---|
38 | // set minimum height of all non-zero buckets on 10% of maximum
|
---|
39 | double minHeight = buckets.Max() * 0.1;
|
---|
40 | for (int i = 0; i < buckets.Length; i++) {
|
---|
41 | if (buckets[i] >= 1 && buckets[i] < minHeight)
|
---|
42 | buckets[i] = minHeight;
|
---|
43 | }
|
---|
44 |
|
---|
45 | var points = chart.Series[0].Points;
|
---|
46 | if (points.Count == buckets.Length) {
|
---|
47 | for (int i = 0; i < buckets.Length; i++)
|
---|
48 | points[i].SetValueY(buckets[i]);
|
---|
49 | chart.ChartAreas[0].RecalculateAxesScale();
|
---|
50 | chart.Refresh();
|
---|
51 | } else {
|
---|
52 | chart.Series[0].Points.DataBindY(buckets);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | private double[] CalculateBuckets(IList<double> data, int numBuckets, double min, double max) {
|
---|
57 | var buckets = new double[numBuckets];
|
---|
58 | var bucketSize = (max - min) / numBuckets;
|
---|
59 | if (bucketSize > 0.0) {
|
---|
60 | for (int i = 0; i < data.Count; i++) {
|
---|
61 | int bucketIndex = (int)((data[i] - min) / bucketSize);
|
---|
62 | if (bucketIndex == numBuckets) {
|
---|
63 | bucketIndex--;
|
---|
64 | }
|
---|
65 | if (bucketIndex >= 0 && bucketIndex < buckets.Length)
|
---|
66 | buckets[bucketIndex] += 1.0;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | return buckets;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|