Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis.Statistics.Views/3.3/SampleSizeInfluenceView.cs @ 12116

Last change on this file since 12116 was 12116, checked in by ascheibe, 9 years ago

#2348 also added UpdateOfRunsInProgress check in other views

File size: 25.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
25using System.Windows.Forms;
26using System.Windows.Forms.DataVisualization.Charting;
27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33using HeuristicLab.Optimization;
34using HeuristicLab.PluginInfrastructure;
35
36namespace HeuristicLab.Analysis.Statistics.Views {
37  [View("Sample Size Influence", "HeuristicLab.Analysis.Statistics.Views.InfoResources.SampleSizeInfluenceInfo.rtf")]
38  [Content(typeof(RunCollection), false)]
39  public partial class SampleSizeInfluenceView : AsynchronousContentView {
40    private enum AxisDimension { Color = 0 }
41    private const string BoxPlotSeriesName = "BoxPlotSeries";
42    private const string BoxPlotChartAreaName = "BoxPlotChartArea";
43    private const string delimitor = ";";
44
45    private bool suppressUpdates = false;
46    private string yAxisValue;
47    private Dictionary<int, Dictionary<object, double>> categoricalMapping;
48    private SortedDictionary<double, Series> seriesCache;
49
50    public SampleSizeInfluenceView() {
51      InitializeComponent();
52      categoricalMapping = new Dictionary<int, Dictionary<object, double>>();
53      seriesCache = new SortedDictionary<double, Series>();
54      chart.ChartAreas[0].Visible = false;
55      chart.Series.Clear();
56      chart.ChartAreas.Add(BoxPlotChartAreaName);
57      chart.CustomizeAllChartAreas();
58      chart.ChartAreas[BoxPlotChartAreaName].Axes.ToList().ForEach(x => { x.ScaleView.Zoomable = true; x.ScaleView.MinSize = 0; });
59      chart.ChartAreas[BoxPlotChartAreaName].CursorX.Interval = 0.5;
60      chart.ChartAreas[BoxPlotChartAreaName].CursorY.Interval = 1e-5;
61    }
62
63    public new RunCollection Content {
64      get { return (RunCollection)base.Content; }
65      set { base.Content = value; }
66    }
67    public IStringConvertibleMatrix Matrix {
68      get { return this.Content; }
69    }
70
71    #region RunCollection and Run events
72    protected override void RegisterContentEvents() {
73      base.RegisterContentEvents();
74      Content.Reset += new EventHandler(Content_Reset);
75      Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
76      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
77      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
78      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
79      Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
80      Content.OptimizerNameChanged += new EventHandler(Content_AlgorithmNameChanged);
81      RegisterRunEvents(Content);
82    }
83    protected override void DeregisterContentEvents() {
84      base.DeregisterContentEvents();
85      Content.Reset -= new EventHandler(Content_Reset);
86      Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
87      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
88      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
89      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
90      Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
91      Content.OptimizerNameChanged -= new EventHandler(Content_AlgorithmNameChanged);
92      DeregisterRunEvents(Content);
93    }
94
95    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
96      foreach (IRun run in runs) {
97        RegisterRunParametersEvents(run);
98        RegisterRunResultsEvents(run);
99      }
100    }
101
102    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
103      foreach (IRun run in runs) {
104        DeregisterRunParametersEvents(run);
105        DeregisterRunResultsEvents(run);
106      }
107    }
108
109    private void RegisterRunParametersEvents(IRun run) {
110      IObservableDictionary<string, IItem> dict = run.Parameters;
111      dict.ItemsAdded += run_Changed;
112      dict.ItemsRemoved += run_Changed;
113      dict.ItemsReplaced += run_Changed;
114      dict.CollectionReset += run_Changed;
115    }
116
117    private void RegisterRunResultsEvents(IRun run) {
118      IObservableDictionary<string, IItem> dict = run.Results;
119      dict.ItemsAdded += run_Changed;
120      dict.ItemsRemoved += run_Changed;
121      dict.ItemsReplaced += run_Changed;
122      dict.CollectionReset += run_Changed;
123    }
124
125    private void DeregisterRunParametersEvents(IRun run) {
126      IObservableDictionary<string, IItem> dict = run.Parameters;
127      dict.ItemsAdded -= run_Changed;
128      dict.ItemsRemoved -= run_Changed;
129      dict.ItemsReplaced -= run_Changed;
130      dict.CollectionReset -= run_Changed;
131    }
132
133    private void DeregisterRunResultsEvents(IRun run) {
134      IObservableDictionary<string, IItem> dict = run.Results;
135      dict.ItemsAdded -= run_Changed;
136      dict.ItemsRemoved -= run_Changed;
137      dict.ItemsReplaced -= run_Changed;
138      dict.CollectionReset -= run_Changed;
139    }
140
141    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
142      DeregisterRunEvents(e.OldItems);
143      RegisterRunEvents(e.Items);
144      UpdateAll();
145    }
146    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
147      DeregisterRunEvents(e.Items);
148      UpdateComboBoxes();
149    }
150    private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
151      RegisterRunEvents(e.Items);
152      UpdateComboBoxes();
153    }
154    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
155      if (InvokeRequired)
156        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
157      else {
158        suppressUpdates = Content.UpdateOfRunsInProgress;
159        if (!suppressUpdates) UpdateDataPoints();
160      }
161    }
162
163    private void Content_Reset(object sender, EventArgs e) {
164      if (InvokeRequired)
165        Invoke(new EventHandler(Content_Reset), sender, e);
166      else {
167        this.categoricalMapping.Clear();
168        UpdateDataPoints();
169        UpdateAxisLabels();
170      }
171    }
172    private void Content_ColumnNamesChanged(object sender, EventArgs e) {
173      if (InvokeRequired)
174        Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
175      else {
176        if (!suppressUpdates) {
177          UpdateComboBoxes();
178        }
179      }
180    }
181    private void run_Changed(object sender, EventArgs e) {
182      if (InvokeRequired)
183        this.Invoke(new EventHandler(run_Changed), sender, e);
184      else if (!suppressUpdates) {
185        UpdateDataPoints();
186      }
187    }
188
189    private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
190      if (InvokeRequired)
191        Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
192      else UpdateCaption();
193    }
194    #endregion
195
196    #region update comboboxes, datapoints, runs
197    protected override void OnContentChanged() {
198      base.OnContentChanged();
199      UpdateAll();
200    }
201
202    private void UpdateAll() {
203      this.categoricalMapping.Clear();
204      UpdateComboBoxes();
205      UpdateDataPoints();
206      UpdateCaption();
207    }
208
209    private void UpdateCaption() {
210      Caption = Content != null ? Content.OptimizerName + " Sample Size Influence" : ViewAttribute.GetViewName(GetType());
211    }
212
213    private void UpdateSampleSizes(bool forceUpdate = false) {
214      string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
215
216      if (selectedYAxis != null && (xAxisTextBox.Text.Trim() == string.Empty || forceUpdate)) {
217        xAxisTextBox.Clear();
218        List<double> values = new List<double>();
219        foreach (IRun run in Content.Where(x => x.Visible)) {
220          double? cv = GetValue(run, selectedYAxis);
221          if (cv.HasValue) {
222            values.Add(cv.Value);
223          }
224        }
225
226        if (values.Any()) {
227          if (hypergeometricCheckBox.Checked) {
228            xAxisTextBox.Text += ((int)(values.Count() / 16)) + delimitor + " ";
229            xAxisTextBox.Text += ((int)(values.Count() / 8)) + delimitor + " ";
230            xAxisTextBox.Text += (int)(values.Count() / 4);
231          } else {
232            xAxisTextBox.Text += ((int)(values.Count() / 4)) + delimitor + " ";
233            xAxisTextBox.Text += ((int)(values.Count() / 2)) + delimitor + " ";
234            xAxisTextBox.Text += ((int)(values.Count() / 4 * 3)) + delimitor + " ";
235            xAxisTextBox.Text += (int)(values.Count());
236          }
237        }
238      }
239    }
240
241    private void UpdateComboBoxes() {
242      string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
243      this.xAxisTextBox.Text = string.Empty;
244      this.yAxisComboBox.Items.Clear();
245      if (Content != null) {
246        string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
247        UpdateSampleSizes();
248        this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
249        this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
250
251        if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
252          yAxisComboBox.SelectedItem = selectedYAxis;
253          UpdateDataPoints();
254        }
255      }
256    }
257
258    private void UpdateDataPoints() {
259      this.chart.Series.Clear();
260      this.seriesCache.Clear();
261      if (Content != null) {
262        var usableRuns = Content.Where(r => r.Visible).ToList();
263        List<int> groupSizes = ParseGroupSizesFromText(xAxisTextBox.Text);
264
265        if (hypergeometricCheckBox.Checked) {
266          CalculateGroupsHypergeometric(usableRuns, groupSizes);
267        } else {
268          CalculateGroups(usableRuns, groupSizes);
269        }
270
271        foreach (Series s in this.seriesCache.Values)
272          this.chart.Series.Add(s);
273
274        UpdateStatistics();
275        if (seriesCache.Count > 0) {
276          Series boxPlotSeries = CreateBoxPlotSeries();
277          this.chart.Series.Add(boxPlotSeries);
278        }
279
280        UpdateAxisLabels();
281        if (groupSizes.Any())
282          AddSampleSizeText();
283      } else {
284        sampleSizeTextBox.Text = string.Empty;
285      }
286      UpdateNoRunsVisibleLabel();
287    }
288
289    private void CalculateGroups(List<IRun> usableRuns, List<int> groupSizes) {
290      Random rand = new Random();
291
292      foreach (int gs in groupSizes) {
293        int idx = gs;
294        List<IRun> runGroup = new List<IRun>();
295        if (idx > usableRuns.Count()) {
296          idx = usableRuns.Count();
297        }
298
299        for (int i = 0; i < idx; i++) {
300          int r = rand.Next(usableRuns.Count());
301          runGroup.Add(usableRuns[r]);
302        }
303        runGroup.ForEach(x => AddDataPoint(x, idx));
304      }
305    }
306
307    private void CalculateGroupsHypergeometric(List<IRun> usableRuns, List<int> groupSizes) {
308      Random rand = new Random();
309      var runs = new List<IRun>(usableRuns);
310
311      foreach (int gs in groupSizes) {
312        int idx = gs;
313        List<IRun> runGroup = new List<IRun>();
314        if (idx > runs.Count()) {
315          idx = runs.Count();
316        }
317
318        for (int i = 0; i < idx; i++) {
319          int r = rand.Next(runs.Count());
320          runGroup.Add(runs[r]);
321          runs.Remove(runs[r]);
322        }
323        runGroup.ForEach(x => AddDataPoint(x, idx));
324      }
325    }
326
327    private void AddSampleSizeText() {
328      sampleSizeTextBox.Text = string.Empty;
329      var usableRuns = Content.Where(r => r.Visible).ToList();
330
331      if (!yAxisComboBox.DroppedDown)
332        this.yAxisValue = (string)yAxisComboBox.SelectedItem;
333
334      List<double?> yValue = usableRuns.Select(x => GetValue(x, this.yAxisValue)).ToList();
335      if (yValue.Any(x => !x.HasValue)) return;
336
337      double estimatedSampleSize = SampleSizeDetermination.DetermineSampleSizeByEstimatingMean(yValue.Select(x => x.Value).ToArray());
338      sampleSizeTextBox.Text = estimatedSampleSize.ToString();
339    }
340
341    private List<int> ParseGroupSizesFromText(string groupsText, bool verbose = true) {
342      string[] gs = groupsText.Split(delimitor.ToCharArray());
343      List<int> vals = new List<int>();
344
345      foreach (string s in gs) {
346        string ns = s.Trim();
347
348        if (ns != string.Empty) {
349          int v = 0;
350          try {
351            v = int.Parse(ns);
352            vals.Add(v);
353          }
354          catch (Exception ex) {
355            if (verbose) {
356              ErrorHandling.ShowErrorDialog("Can't parse group sizes. Please only use numbers seperated by a " + delimitor + ". ", ex);
357            }
358          }
359        }
360      }
361      return vals;
362    }
363
364    private void UpdateStatistics() {
365      DoubleMatrix matrix = new DoubleMatrix(11, seriesCache.Count);
366      matrix.SortableView = false;
367      List<string> columnNames = new List<string>();
368      foreach (Series series in seriesCache.Values) {
369        DataPoint datapoint = series.Points.FirstOrDefault();
370        if (datapoint != null) {
371          IRun run = (IRun)datapoint.Tag;
372          string selectedAxis = xAxisTextBox.Text;
373          IItem value = null;
374
375          if (Enum.IsDefined(typeof(AxisDimension), selectedAxis)) {
376            AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), selectedAxis);
377            switch (axisDimension) {
378              case AxisDimension.Color: value = new StringValue(run.Color.ToString());
379                break;
380            }
381          }
382
383          string columnName = series.Name;
384          columnNames.Add(columnName);
385        }
386      }
387      matrix.ColumnNames = columnNames;
388      matrix.RowNames = new string[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile", "Lower Confidence Int.", "Upper Confidence Int." };
389
390      for (int i = 0; i < seriesCache.Count; i++) {
391        Series series = seriesCache.ElementAt(i).Value;
392        double[] seriesValues = series.Points.Select(p => p.YValues[0]).OrderBy(d => d).ToArray();
393        Tuple<double, double> confIntervals = seriesValues.ConfidenceIntervals(0.95);
394        matrix[0, i] = seriesValues.Length;
395        matrix[1, i] = seriesValues.Min();
396        matrix[2, i] = seriesValues.Max();
397        matrix[3, i] = seriesValues.Average();
398        matrix[4, i] = seriesValues.Median();
399        matrix[5, i] = seriesValues.StandardDeviation();
400        matrix[6, i] = seriesValues.Variance();
401        matrix[7, i] = seriesValues.Percentile(0.25);
402        matrix[8, i] = seriesValues.Percentile(0.75);
403        matrix[9, i] = confIntervals.Item1;
404        matrix[10, i] = confIntervals.Item2;
405      }
406      statisticsMatrixView.Content = matrix;
407    }
408
409    private Series CreateBoxPlotSeries() {
410      Series boxPlotSeries = new Series(BoxPlotSeriesName);
411      string seriesNames = string.Concat(seriesCache.Keys.Select(x => x.ToString() + ";").ToArray());
412      seriesNames = seriesNames.Remove(seriesNames.Length - 1); //delete last ; from string
413
414      boxPlotSeries.ChartArea = BoxPlotChartAreaName;
415      boxPlotSeries.ChartType = SeriesChartType.BoxPlot;
416      boxPlotSeries["BoxPlotSeries"] = seriesNames;
417      boxPlotSeries["BoxPlotShowUnusualValues"] = "true";
418      boxPlotSeries["PointWidth"] = "0.4";
419      boxPlotSeries.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.VerticalCenter;
420      boxPlotSeries.BackSecondaryColor = System.Drawing.Color.FromArgb(130, 224, 64, 10);
421      boxPlotSeries.BorderColor = System.Drawing.Color.FromArgb(64, 64, 64);
422      boxPlotSeries.Color = System.Drawing.Color.FromArgb(224, 64, 10);
423
424      return boxPlotSeries;
425    }
426
427    private void AddDataPoint(IRun run, int idx) {
428      double xValue;
429      double? yValue;
430
431
432      if (!yAxisComboBox.DroppedDown)
433        this.yAxisValue = (string)yAxisComboBox.SelectedItem;
434
435      xValue = idx;
436      yValue = GetValue(run, this.yAxisValue);
437
438      if (yValue.HasValue) {
439        if (!this.seriesCache.ContainsKey(xValue))
440          seriesCache[xValue] = new Series(xValue.ToString());
441
442        Series series = seriesCache[xValue];
443        DataPoint point = new DataPoint(xValue, yValue.Value);
444        point.Tag = run;
445        series.Points.Add(point);
446      }
447    }
448    #endregion
449
450    #region get values from run
451    private double? GetValue(IRun run, string columnName) {
452      if (run == null || string.IsNullOrEmpty(columnName))
453        return null;
454
455      if (Enum.IsDefined(typeof(AxisDimension), columnName)) {
456        AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
457        return GetValue(run, axisDimension);
458      } else {
459        int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
460        IItem value = Content.GetValue(run, columnIndex);
461        if (value == null)
462          return null;
463
464        DoubleValue doubleValue = value as DoubleValue;
465        IntValue intValue = value as IntValue;
466        TimeSpanValue timeSpanValue = value as TimeSpanValue;
467        double? ret = null;
468        if (doubleValue != null) {
469          if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
470            ret = doubleValue.Value;
471        } else if (intValue != null)
472          ret = intValue.Value;
473        else if (timeSpanValue != null) {
474          ret = timeSpanValue.Value.TotalSeconds;
475        } else
476          ret = GetCategoricalValue(columnIndex, value.ToString());
477
478        return ret;
479      }
480    }
481    private double GetCategoricalValue(int dimension, string value) {
482      if (!this.categoricalMapping.ContainsKey(dimension)) {
483        this.categoricalMapping[dimension] = new Dictionary<object, double>();
484        var orderedCategories = Content.Where(r => r.Visible && Content.GetValue(r, dimension) != null).Select(r => Content.GetValue(r, dimension).ToString())
485                                          .Distinct().OrderBy(x => x, new NaturalStringComparer());
486        int count = 1;
487        foreach (var category in orderedCategories) {
488          this.categoricalMapping[dimension].Add(category, count);
489          count++;
490        }
491      }
492      return this.categoricalMapping[dimension][value];
493    }
494    private double GetValue(IRun run, AxisDimension axisDimension) {
495      double value = double.NaN;
496      switch (axisDimension) {
497        case AxisDimension.Color: {
498            value = GetCategoricalValue(-1, run.Color.ToString());
499            break;
500          }
501        default: {
502            throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
503          }
504      }
505      return value;
506    }
507    #endregion
508
509    #region GUI events
510    private void UpdateNoRunsVisibleLabel() {
511      if (this.chart.Series.Count > 0) {
512        noRunsLabel.Visible = false;
513        showStatisticsCheckBox.Enabled = true;
514        splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
515      } else {
516        noRunsLabel.Visible = true;
517        showStatisticsCheckBox.Enabled = false;
518        splitContainer.Panel2Collapsed = true;
519      }
520    }
521
522    private void RecalculateButton_Click(object sender, EventArgs e) {
523      UpdateDataPoints();
524    }
525
526    private void hypergeometricCheckBox_CheckedChanged(object sender, EventArgs e) {
527      UpdateSampleSizes(true);
528      UpdateDataPoints();
529    }
530
531    private void AxisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
532      UpdateSampleSizes();
533      UpdateDataPoints();
534    }
535    private void UpdateAxisLabels() {
536      Axis xAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisX;
537      Axis yAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisY;
538      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
539
540      SetCustomAxisLabels(xAxis, -1);
541      SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
542
543      xAxis.Title = "Group Size";
544      if (yAxisComboBox.SelectedItem != null)
545        yAxis.Title = yAxisComboBox.SelectedItem.ToString();
546    }
547
548    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
549      this.UpdateAxisLabels();
550    }
551
552    private void SetCustomAxisLabels(Axis axis, int dimension) {
553      axis.CustomLabels.Clear();
554      if (categoricalMapping.ContainsKey(dimension)) {
555        int position = 1;
556        foreach (var pair in categoricalMapping[dimension].Where(x => seriesCache.ContainsKey(x.Value))) {
557          string labelText = pair.Key.ToString();
558          CustomLabel label = new CustomLabel();
559          label.ToolTip = labelText;
560          if (labelText.Length > 25)
561            labelText = labelText.Substring(0, 25) + " ... ";
562          label.Text = labelText;
563          label.GridTicks = GridTickTypes.TickMark;
564          label.FromPosition = position - 0.5;
565          label.ToPosition = position + 0.5;
566          axis.CustomLabels.Add(label);
567          position++;
568        }
569      } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
570        this.chart.ChartAreas[0].RecalculateAxesScale();
571        Axis correspondingAxis = this.chart.ChartAreas[0].Axes.Where(x => x.Name == axis.Name).SingleOrDefault();
572        if (correspondingAxis == null)
573          correspondingAxis = axis;
574        for (double i = correspondingAxis.Minimum; i <= correspondingAxis.Maximum; i += correspondingAxis.LabelStyle.Interval) {
575          TimeSpan time = TimeSpan.FromSeconds(i);
576          string x = string.Format("{0:00}:{1:00}:{2:00}", (int)time.Hours, time.Minutes, time.Seconds);
577          axis.CustomLabels.Add(i - correspondingAxis.LabelStyle.Interval / 2, i + correspondingAxis.LabelStyle.Interval / 2, x);
578        }
579      } else if (chart.ChartAreas[BoxPlotChartAreaName].AxisX == axis) {
580        double position = 1.0;
581        foreach (Series series in chart.Series) {
582          if (series.Name != BoxPlotSeriesName) {
583            string labelText = series.Points[0].XValue.ToString();
584            CustomLabel label = new CustomLabel();
585            label.FromPosition = position - 0.5;
586            label.ToPosition = position + 0.5;
587            label.GridTicks = GridTickTypes.TickMark;
588            label.Text = labelText;
589            axis.CustomLabels.Add(label);
590            position++;
591          }
592        }
593      }
594    }
595
596    private void chart_MouseMove(object sender, MouseEventArgs e) {
597      string newTooltipText = string.Empty;
598      string oldTooltipText;
599      HitTestResult h = this.chart.HitTest(e.X, e.Y);
600      if (h.ChartElementType == ChartElementType.AxisLabels) {
601        newTooltipText = ((CustomLabel)h.Object).ToolTip;
602      }
603
604      oldTooltipText = this.tooltip.GetToolTip(chart);
605      if (newTooltipText != oldTooltipText)
606        this.tooltip.SetToolTip(chart, newTooltipText);
607    }
608    #endregion
609
610    private void showStatisticsCheckBox_CheckedChanged(object sender, EventArgs e) {
611      splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
612    }
613
614    private void defineSampleSizeButton_Click(object sender, EventArgs e) {
615      int min = 0, max = 0, step = 1;
616      var groupSizes = ParseGroupSizesFromText(xAxisTextBox.Text);
617      if (groupSizes.Count() > 0) {
618        min = groupSizes.Min();
619        max = groupSizes.Max();
620      }
621
622      using (var dialog = new DefineArithmeticProgressionDialog(true, min, max, step)) {
623        if (dialog.ShowDialog(this) == DialogResult.OK) {
624          var values = dialog.Values;
625          string newVals = "";
626          foreach (int v in values) {
627            newVals += v + delimitor + " ";
628          }
629          xAxisTextBox.Text = newVals;
630        }
631      }
632    }
633
634    private void xAxisTextBox_TextChanged(object sender, EventArgs e) {
635      var result = ParseGroupSizesFromText(xAxisTextBox.Text, false);
636
637      if (seriesCache.Count() == result.Count()) {
638        bool changed = false;
639        int i = 0;
640        foreach (var gs in seriesCache.Keys) {
641          if (((int)gs) != result[i]) {
642            changed = true;
643            break;
644          }
645          i++;
646        }
647
648        if (changed) {
649          UpdateDataPoints();
650        }
651      } else {
652        UpdateDataPoints();
653      }
654    }
655  }
656}
Note: See TracBrowser for help on using the repository browser.