[13850] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[13850] | 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 |
|
---|
[14826] | 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 |
|
---|
[13850] | 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 |
|
---|
[14826] | 43 | UpdateChartWithBuckets(CalculateBuckets(data, numBuckets, min, max));
|
---|
| 44 | }
|
---|
[13850] | 45 |
|
---|
[14826] | 46 |
|
---|
| 47 | private void UpdateChartWithBuckets(double[] buckets) {
|
---|
[13850] | 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 | }
|
---|
[14826] | 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 | }
|
---|
[13850] | 84 | }
|
---|
| 85 | }
|
---|