Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/SampleSizeInfluenceView.cs @ 11670

Last change on this file since 11670 was 11670, checked in by ascheibe, 10 years ago

#2031 worked on sample size influence view

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