Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11608


Ignore:
Timestamp:
12/01/14 16:16:29 (10 years ago)
Author:
ascheibe
Message:

#2279 added support for multiple data rows in histogram control

Location:
trunk/sources/HeuristicLab.Visualization.ChartControlsExtensions/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Visualization.ChartControlsExtensions/3.3/HistogramControl.Designer.cs

    r11171 r11608  
    4545    /// </summary>
    4646    private void InitializeComponent() {
     47      this.components = new System.ComponentModel.Container();
    4748      System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
     49      System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
    4850      System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
    4951      this.chart = new HeuristicLab.Visualization.ChartControlsExtensions.EnhancedChart();
     
    5860      //
    5961      this.chart.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    60                   | System.Windows.Forms.AnchorStyles.Left)
    61                   | System.Windows.Forms.AnchorStyles.Right)));
     62            | System.Windows.Forms.AnchorStyles.Left)
     63            | System.Windows.Forms.AnchorStyles.Right)));
    6264      chartArea1.Name = "ChartArea1";
    6365      this.chart.ChartAreas.Add(chartArea1);
     66      legend1.Alignment = System.Drawing.StringAlignment.Center;
     67      legend1.Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Top;
     68      legend1.Name = "Default";
     69      this.chart.Legends.Add(legend1);
    6470      this.chart.Location = new System.Drawing.Point(0, 27);
    6571      this.chart.Name = "chart";
    6672      series1.ChartArea = "ChartArea1";
     73      series1.Legend = "Default";
    6774      series1.Name = "Series1";
    6875      this.chart.Series.Add(series1);
     
    116123      // HistogramControl
    117124      //
    118       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    119125      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
    120126      this.Controls.Add(this.exactCheckBox);
  • trunk/sources/HeuristicLab.Visualization.ChartControlsExtensions/3.3/HistogramControl.cs

    r11171 r11608  
    2929namespace HeuristicLab.Visualization.ChartControlsExtensions {
    3030  public partial class HistogramControl : UserControl {
    31     private static readonly string SeriesName = "Histogram";
    32 
    33     private List<double> points;
     31    protected static readonly string SeriesName = "Histogram";
     32
     33    protected Dictionary<string, List<double>> points;
    3434    public int NumberOfBins {
    3535      get { return (int)binsNumericUpDown.Value; }
     
    6464    public HistogramControl() {
    6565      InitializeComponent();
    66       points = new List<double>();
     66      points = new Dictionary<string, List<double>>();
    6767      InitializeChart();
    6868    }
    6969
     70    protected void InitNewRow(string name) {
     71      if (!points.ContainsKey(name)) {
     72        points.Add(name, new List<double>());
     73      }
     74    }
     75
     76    protected void InitSeries(string name) {
     77      if (!chart.Series.Any(x => x.Name == name)) {
     78        Series s = chart.Series.Add(name);
     79        s.ChartType = SeriesChartType.Column;
     80        s.BorderColor = Color.Black;
     81        s.BorderWidth = 1;
     82        s.BorderDashStyle = ChartDashStyle.Solid;
     83      }
     84    }
     85
    7086    public void AddPoint(double point) {
    71       points.Add(point);
     87      InitNewRow(SeriesName);
     88      InitSeries(SeriesName);
     89      points[SeriesName].Add(point);
    7290      UpdateHistogram();
    7391    }
    7492
    7593    public void AddPoints(IEnumerable<double> points) {
    76       this.points.AddRange(points);
     94      InitNewRow(SeriesName);
     95      InitSeries(SeriesName);
     96      this.points[SeriesName].AddRange(points);
     97      UpdateHistogram();
     98    }
     99
     100    public void AddPoint(string name, double point, bool replace = false) {
     101      InitNewRow(name);
     102      InitSeries(name);
     103      if (replace) {
     104        points[name].Clear();
     105      }
     106      points[name].Add(point);
     107      UpdateHistogram();
     108    }
     109
     110    public void AddPoints(string name, IEnumerable<double> points, bool replace = false) {
     111      InitNewRow(name);
     112      InitSeries(name);
     113      if (replace) {
     114        this.points[name].Clear();
     115      }
     116      this.points[name].AddRange(points);
    77117      UpdateHistogram();
    78118    }
     
    83123    }
    84124
    85     private void InitializeChart() {
     125    protected void InitializeChart() {
    86126      chart.Series.Clear();
    87       Series s = chart.Series.Add(SeriesName);
    88       s.ChartType = SeriesChartType.Column;
    89       s.BorderColor = Color.Black;
    90       s.BorderWidth = 1;
    91       s.BorderDashStyle = ChartDashStyle.Solid;
    92     }
    93 
    94     private void UpdateHistogram() {
     127    }
     128
     129    protected void UpdateHistogram() {
    95130      if (InvokeRequired) {
    96131        Invoke((Action)UpdateHistogram, null);
    97132        return;
    98133      }
    99       Series histogramSeries = chart.Series[SeriesName];
    100       histogramSeries.Points.Clear();
    101 
    102       if (!points.Any()) return;
     134
     135      double overallMin = double.MaxValue;
     136      double overallMax = double.MinValue;
    103137      int bins = (int)binsNumericUpDown.Value;
    104138
    105       double minValue = points.Min();
    106       double maxValue = points.Max();
    107       double intervalWidth = (maxValue - minValue) / bins;
    108       if (intervalWidth <= 0) return;
    109 
    110       if (!exactCheckBox.Checked) {
    111         intervalWidth = HumanRoundRange(intervalWidth);
    112         minValue = Math.Floor(minValue / intervalWidth) * intervalWidth;
    113         maxValue = Math.Ceiling(maxValue / intervalWidth) * intervalWidth;
    114       }
    115 
    116       double current = minValue, intervalCenter = intervalWidth / 2.0;
    117       int count = 0;
    118       foreach (double v in points.OrderBy(x => x)) {
    119         while (v > current + intervalWidth) {
    120           histogramSeries.Points.AddXY(current + intervalCenter, count);
    121           current += intervalWidth;
    122           count = 0;
     139      chart.Series.Clear();
     140
     141      foreach (var point in points) {
     142        if (!point.Value.Any()) continue;
     143
     144        Series histogramSeries = new Series(point.Key);
     145        chart.Series.Add(histogramSeries);
     146
     147        double minValue = point.Value.Min();
     148        double maxValue = point.Value.Max();
     149        double intervalWidth = (maxValue - minValue) / bins;
     150        if (intervalWidth <= 0) continue;
     151
     152        if (!exactCheckBox.Checked) {
     153          intervalWidth = HumanRoundRange(intervalWidth);
     154          minValue = Math.Floor(minValue / intervalWidth) * intervalWidth;
     155          maxValue = Math.Ceiling(maxValue / intervalWidth) * intervalWidth;
    123156        }
    124         count++;
    125       }
    126       histogramSeries.Points.AddXY(current + intervalCenter, count);
    127 
    128       histogramSeries["PointWidth"] = "1";
    129 
    130       ChartArea chartArea = chart.ChartAreas[histogramSeries.ChartArea];
     157
     158        double current = minValue, intervalCenter = intervalWidth / 2.0;
     159        int count = 0;
     160        foreach (double v in point.Value.OrderBy(x => x)) {
     161          while (v > current + intervalWidth) {
     162            histogramSeries.Points.AddXY(current + intervalCenter, count);
     163            current += intervalWidth;
     164            count = 0;
     165          }
     166          count++;
     167        }
     168        histogramSeries.Points.AddXY(current + intervalCenter, count);
     169        histogramSeries["PointWidth"] = "1";
     170
     171        overallMax = Math.Max(overallMax, maxValue);
     172        overallMin = Math.Min(overallMin, minValue);
     173      }
     174
     175      ChartArea chartArea = chart.ChartAreas[0];
    131176      chartArea.AxisY.Title = "Frequency";
    132       chartArea.AxisX.Minimum = minValue;
    133       chartArea.AxisX.Maximum = maxValue;
    134 
    135       double axisInterval = intervalWidth;
    136       while ((maxValue - minValue) / axisInterval > 10.0) {
     177      chartArea.AxisX.Minimum = overallMin;
     178      chartArea.AxisX.Maximum = overallMax;
     179
     180      double overallIntervalWidth = (overallMax - overallMin) / bins;
     181
     182      double axisInterval = overallIntervalWidth;
     183      while ((overallMax - overallMin) / axisInterval > 10.0) {
    137184        axisInterval *= 2.0;
    138185      }
     
    140187    }
    141188
    142     private double HumanRoundRange(double range) {
     189    protected double HumanRoundRange(double range) {
    143190      double base10 = Math.Pow(10.0, Math.Floor(Math.Log10(range)));
    144191      double rounding = range / base10;
Note: See TracChangeset for help on using the changeset viewer.