Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/DensityTrackbar.cs @ 13816

Last change on this file since 13816 was 13816, checked in by pfleck, 8 years ago

#2597:

  • Fixed typo in class/filename.
  • Added new control that shows density information adjacent to the trackbar.
File size: 5.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Globalization;
25using System.Linq;
26using System.Windows.Forms;
27using System.Windows.Forms.DataVisualization.Charting;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis.Views {
31  public partial class DensityTrackbar : UserControl {
32
33    public decimal Value {
34      get { return TickToValue(trackBar.Value); }
35      set { trackBar.Value = ValueToTick(value); }
36    }
37    public event EventHandler ValueChanged;
38
39    public bool Checked {
40      get { return radioButton.Checked; }
41      set { radioButton.Checked = value; }
42    }
43    public event EventHandler CheckedChanged;
44
45    public DoubleLimit Limits {
46      get { return doubleLimitView.Content; }
47    }
48    public event EventHandler LimitsChanged;
49
50    private IList<double> trainingValues;
51
52    internal DensityTrackbar() {
53      InitializeComponent();
54    }
55
56    public DensityTrackbar(string name, DoubleLimit limit, IList<double> trainingValues) {
57      InitializeComponent();
58
59      this.trainingValues = trainingValues;
60
61      doubleLimitView.Content = limit;
62      doubleLimitView.Content.ValueChanged += doubleLimit_ValueChanged;
63
64      textBox.Text = Value.ToString(CultureInfo.InvariantCulture);
65      radioButton.Text = name;
66
67      UpdateDensityChart();
68    }
69
70    public void UpdateTrainingValues(IList<double> trainingValues) {
71      this.trainingValues = trainingValues;
72      UpdateDensityChart();
73    }
74
75    private void UpdateDensityChart() {
76      var series = chart.Series[0];
77      ClearPointsQuick(series.Points);
78
79      int numBuckets = trackBar.Maximum - trackBar.Minimum;
80      var buckets = new double[numBuckets];
81      var bucketSize = (Limits.Upper - Limits.Lower) / buckets.Length;
82      foreach (var value in trainingValues) {
83        if (bucketSize > 0.0) {
84          int bucketIndex = (int)((value - Limits.Lower) / bucketSize);
85          if (bucketIndex == numBuckets) {
86            bucketIndex--;
87          }
88          buckets[bucketIndex] += 1.0;
89        }
90      }
91
92      // set minimum height of all non-zero buckets on 20% of maximum
93      double min = buckets.Max() / 20.0;
94      for (int i = 0; i < buckets.Length; i++) {
95        if (buckets[i] >= 1 && buckets[i] < min)
96          buckets[i] = min;
97      }
98
99      series.Points.DataBindY(buckets);
100    }
101
102    #region Event Handlers
103    private void trackBar_ValueChanged(object sender, EventArgs e) {
104      textBox.Text = Value.ToString(CultureInfo.InvariantCulture);
105      OnValueChanged();
106    }
107    private void doubleLimit_ValueChanged(object sender, EventArgs e) {
108      UpdateDensityChart();
109      OnLimitsChanged();
110      OnValueChanged();
111    }
112    private void radioButton_CheckedChanged(object sender, EventArgs e) {
113      Checked = radioButton.Checked;
114      trackBar.Enabled = !Checked;
115      textBox.Text = Checked ? "Plotted" : Value.ToString(CultureInfo.InvariantCulture);
116      OnCheckedChanged();
117    }
118    protected override void OnTextChanged(EventArgs e) {
119      base.OnTextChanged(e);
120      radioButton.Text = Text;
121    }
122    #endregion
123
124    protected virtual void OnValueChanged() {
125      if (ValueChanged != null)
126        ValueChanged(this, EventArgs.Empty);
127    }
128    protected virtual void OnCheckedChanged() {
129      if (CheckedChanged != null)
130        CheckedChanged(this, EventArgs.Empty);
131    }
132    protected virtual void OnLimitsChanged() {
133      if (LimitsChanged != null)
134        LimitsChanged(this, EventArgs.Empty);
135    }
136
137    #region Hepers
138
139    private decimal TickToValue(int tick) {
140      return TickToValue(tick, trackBar.Maximum - trackBar.Minimum, (decimal)Limits.Lower, (decimal)Limits.Upper);
141    }
142    private int ValueToTick(decimal value) {
143      return ValueToTick(value, trackBar.Maximum - trackBar.Minimum, (decimal)Limits.Lower, (decimal)Limits.Upper);
144    }
145    private static decimal TickToValue(int tick, int numberOfTicks, decimal lower, decimal upper) {
146      return lower + (upper - lower) * tick / numberOfTicks;
147    }
148    private static int ValueToTick(decimal value, int numberOfTicks, decimal lower, decimal upper) {
149      return (int)((value - lower) / ((upper - lower) / numberOfTicks));
150    }
151
152    // workaround as per http://stackoverflow.com/questions/5744930/datapointcollection-clear-performance
153    private static void ClearPointsQuick(DataPointCollection points) {
154      points.SuspendUpdates();
155      while (points.Count > 0)
156        points.RemoveAt(points.Count - 1);
157      points.ResumeUpdates();
158    }
159
160    #endregion
161
162
163  }
164}
Note: See TracBrowser for help on using the repository browser.