Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2031

  • moved statistics views to a new plugin (HL.Analysis.Statistics.Views) in trunk
  • fixed namespaces of unit tests
File size: 25.1 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.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        UpdateComboBoxes();
177      }
178    }
179    private void run_Changed(object sender, EventArgs e) {
180      if (InvokeRequired)
181        this.Invoke(new EventHandler(run_Changed), sender, e);
182      else if (!suppressUpdates) {
183        UpdateDataPoints();
184      }
185    }
186
187    private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
188      if (InvokeRequired)
189        Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
190      else UpdateCaption();
191    }
192    #endregion
193
194    #region update comboboxes, datapoints, runs
195    protected override void OnContentChanged() {
196      base.OnContentChanged();
197      UpdateAll();
198    }
199
200    private void UpdateAll() {
201      this.categoricalMapping.Clear();
202      UpdateComboBoxes();
203      UpdateDataPoints();
204      UpdateCaption();
205    }
206
207    private void UpdateCaption() {
208      Caption = Content != null ? Content.OptimizerName + " Sample Size Influence" : ViewAttribute.GetViewName(GetType());
209    }
210
211    private void UpdateSampleSizes(bool forceUpdate = false) {
212      string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
213
214      if (selectedYAxis != null && (xAxisTextBox.Text.Trim() == string.Empty || forceUpdate)) {
215        xAxisTextBox.Clear();
216        List<double> values = new List<double>();
217        foreach (IRun run in Content.Where(x => x.Visible)) {
218          double? cv = GetValue(run, selectedYAxis);
219          if (cv.HasValue) {
220            values.Add(cv.Value);
221          }
222        }
223
224        if (values.Any()) {
225          if (hypergeometricCheckBox.Checked) {
226            xAxisTextBox.Text += ((int)(values.Count() / 16)) + delimitor + " ";
227            xAxisTextBox.Text += ((int)(values.Count() / 8)) + delimitor + " ";
228            xAxisTextBox.Text += (int)(values.Count() / 4);
229          } else {
230            xAxisTextBox.Text += ((int)(values.Count() / 4)) + delimitor + " ";
231            xAxisTextBox.Text += ((int)(values.Count() / 2)) + delimitor + " ";
232            xAxisTextBox.Text += ((int)(values.Count() / 4 * 3)) + delimitor + " ";
233            xAxisTextBox.Text += (int)(values.Count());
234          }
235        }
236      }
237    }
238
239    private void UpdateComboBoxes() {
240      string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
241      this.xAxisTextBox.Text = string.Empty;
242      this.yAxisComboBox.Items.Clear();
243      if (Content != null) {
244        string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
245        UpdateSampleSizes();
246        this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
247        this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
248
249        if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
250          yAxisComboBox.SelectedItem = selectedYAxis;
251          UpdateDataPoints();
252        }
253      }
254    }
255
256    private void UpdateDataPoints() {
257      this.chart.Series.Clear();
258      this.seriesCache.Clear();
259      if (Content != null) {
260        var usableRuns = Content.Where(r => r.Visible).ToList();
261        List<int> groupSizes = ParseGroupSizesFromText(xAxisTextBox.Text);
262
263        if (hypergeometricCheckBox.Checked) {
264          CalculateGroupsHypergeometric(usableRuns, groupSizes);
265        } else {
266          CalculateGroups(usableRuns, groupSizes);
267        }
268
269        foreach (Series s in this.seriesCache.Values)
270          this.chart.Series.Add(s);
271
272        UpdateStatistics();
273        if (seriesCache.Count > 0) {
274          Series boxPlotSeries = CreateBoxPlotSeries();
275          this.chart.Series.Add(boxPlotSeries);
276        }
277
278        UpdateAxisLabels();
279        if (groupSizes.Any())
280          AddSampleSizeText();
281      } else {
282        sampleSizeTextBox.Text = string.Empty;
283      }
284      UpdateNoRunsVisibleLabel();
285    }
286
287    private void CalculateGroups(List<IRun> usableRuns, List<int> groupSizes) {
288      Random rand = new Random();
289
290      foreach (int gs in groupSizes) {
291        int idx = gs;
292        List<IRun> runGroup = new List<IRun>();
293        if (idx > usableRuns.Count()) {
294          idx = usableRuns.Count();
295        }
296
297        for (int i = 0; i < idx; i++) {
298          int r = rand.Next(usableRuns.Count());
299          runGroup.Add(usableRuns[r]);
300        }
301        runGroup.ForEach(x => AddDataPoint(x, idx));
302      }
303    }
304
305    private void CalculateGroupsHypergeometric(List<IRun> usableRuns, List<int> groupSizes) {
306      Random rand = new Random();
307      var runs = new List<IRun>(usableRuns);
308
309      foreach (int gs in groupSizes) {
310        int idx = gs;
311        List<IRun> runGroup = new List<IRun>();
312        if (idx > runs.Count()) {
313          idx = runs.Count();
314        }
315
316        for (int i = 0; i < idx; i++) {
317          int r = rand.Next(runs.Count());
318          runGroup.Add(runs[r]);
319          runs.Remove(runs[r]);
320        }
321        runGroup.ForEach(x => AddDataPoint(x, idx));
322      }
323    }
324
325    private void AddSampleSizeText() {
326      sampleSizeTextBox.Text = string.Empty;
327      var usableRuns = Content.Where(r => r.Visible).ToList();
328
329      if (!yAxisComboBox.DroppedDown)
330        this.yAxisValue = (string)yAxisComboBox.SelectedItem;
331
332      List<double?> yValue = usableRuns.Select(x => GetValue(x, this.yAxisValue)).ToList();
333      if (yValue.Any(x => !x.HasValue)) return;
334
335      double estimatedSampleSize = SampleSizeDetermination.DetermineSampleSizeByEstimatingMeanForLargeSampleSizes(yValue.Select(x => x.Value).ToArray());
336      sampleSizeTextBox.Text = estimatedSampleSize.ToString();
337    }
338
339    private List<int> ParseGroupSizesFromText(string groupsText, bool verbose = true) {
340      string[] gs = groupsText.Split(delimitor.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 " + delimitor + ". ", 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                                          .Distinct().OrderBy(x => x, new NaturalStringComparer());
484        int count = 1;
485        foreach (var category in orderedCategories) {
486          this.categoricalMapping[dimension].Add(category, count);
487          count++;
488        }
489      }
490      return this.categoricalMapping[dimension][value];
491    }
492    private double GetValue(IRun run, AxisDimension axisDimension) {
493      double value = double.NaN;
494      switch (axisDimension) {
495        case AxisDimension.Color: {
496            value = GetCategoricalValue(-1, run.Color.ToString());
497            break;
498          }
499        default: {
500            throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
501          }
502      }
503      return value;
504    }
505    #endregion
506
507    #region GUI events
508    private void UpdateNoRunsVisibleLabel() {
509      if (this.chart.Series.Count > 0) {
510        noRunsLabel.Visible = false;
511        showStatisticsCheckBox.Enabled = true;
512        splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
513      } else {
514        noRunsLabel.Visible = true;
515        showStatisticsCheckBox.Enabled = false;
516        splitContainer.Panel2Collapsed = true;
517      }
518    }
519
520    private void RecalculateButton_Click(object sender, EventArgs e) {
521      UpdateDataPoints();
522    }
523
524    private void hypergeometricCheckBox_CheckedChanged(object sender, EventArgs e) {
525      UpdateSampleSizes(true);
526      UpdateDataPoints();
527    }
528
529    private void AxisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
530      UpdateSampleSizes();
531      UpdateDataPoints();
532    }
533    private void UpdateAxisLabels() {
534      Axis xAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisX;
535      Axis yAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisY;
536      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
537
538      SetCustomAxisLabels(xAxis, -1);
539      SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
540
541      xAxis.Title = "Group Size";
542      if (yAxisComboBox.SelectedItem != null)
543        yAxis.Title = yAxisComboBox.SelectedItem.ToString();
544    }
545
546    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
547      this.UpdateAxisLabels();
548    }
549
550    private void SetCustomAxisLabels(Axis axis, int dimension) {
551      axis.CustomLabels.Clear();
552      if (categoricalMapping.ContainsKey(dimension)) {
553        int position = 1;
554        foreach (var pair in categoricalMapping[dimension].Where(x => seriesCache.ContainsKey(x.Value))) {
555          string labelText = pair.Key.ToString();
556          CustomLabel label = new CustomLabel();
557          label.ToolTip = labelText;
558          if (labelText.Length > 25)
559            labelText = labelText.Substring(0, 25) + " ... ";
560          label.Text = labelText;
561          label.GridTicks = GridTickTypes.TickMark;
562          label.FromPosition = position - 0.5;
563          label.ToPosition = position + 0.5;
564          axis.CustomLabels.Add(label);
565          position++;
566        }
567      } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
568        this.chart.ChartAreas[0].RecalculateAxesScale();
569        Axis correspondingAxis = this.chart.ChartAreas[0].Axes.Where(x => x.Name == axis.Name).SingleOrDefault();
570        if (correspondingAxis == null)
571          correspondingAxis = axis;
572        for (double i = correspondingAxis.Minimum; i <= correspondingAxis.Maximum; i += correspondingAxis.LabelStyle.Interval) {
573          TimeSpan time = TimeSpan.FromSeconds(i);
574          string x = string.Format("{0:00}:{1:00}:{2:00}", (int)time.Hours, time.Minutes, time.Seconds);
575          axis.CustomLabels.Add(i - correspondingAxis.LabelStyle.Interval / 2, i + correspondingAxis.LabelStyle.Interval / 2, x);
576        }
577      } else if (chart.ChartAreas[BoxPlotChartAreaName].AxisX == axis) {
578        double position = 1.0;
579        foreach (Series series in chart.Series) {
580          if (series.Name != BoxPlotSeriesName) {
581            string labelText = series.Points[0].XValue.ToString();
582            CustomLabel label = new CustomLabel();
583            label.FromPosition = position - 0.5;
584            label.ToPosition = position + 0.5;
585            label.GridTicks = GridTickTypes.TickMark;
586            label.Text = labelText;
587            axis.CustomLabels.Add(label);
588            position++;
589          }
590        }
591      }
592    }
593
594    private void chart_MouseMove(object sender, MouseEventArgs e) {
595      string newTooltipText = string.Empty;
596      string oldTooltipText;
597      HitTestResult h = this.chart.HitTest(e.X, e.Y);
598      if (h.ChartElementType == ChartElementType.AxisLabels) {
599        newTooltipText = ((CustomLabel)h.Object).ToolTip;
600      }
601
602      oldTooltipText = this.tooltip.GetToolTip(chart);
603      if (newTooltipText != oldTooltipText)
604        this.tooltip.SetToolTip(chart, newTooltipText);
605    }
606    #endregion
607
608    private void showStatisticsCheckBox_CheckedChanged(object sender, EventArgs e) {
609      splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
610    }
611
612    private void defineSampleSizeButton_Click(object sender, EventArgs e) {
613      int min = 0, max = 0, step = 1;
614      var groupSizes = ParseGroupSizesFromText(xAxisTextBox.Text);
615      if (groupSizes.Count() > 0) {
616        min = groupSizes.Min();
617        max = groupSizes.Max();
618      }
619
620      using (var dialog = new DefineArithmeticProgressionDialog(true, min, max, step)) {
621        if (dialog.ShowDialog(this) == DialogResult.OK) {
622          var values = dialog.Values;
623          string newVals = "";
624          foreach (int v in values) {
625            newVals += v + delimitor + " ";
626          }
627          xAxisTextBox.Text = newVals;
628        }
629      }
630    }
631
632    private void xAxisTextBox_TextChanged(object sender, EventArgs e) {
633      var result = ParseGroupSizesFromText(xAxisTextBox.Text, false);
634
635      if (seriesCache.Count() == result.Count()) {
636        bool changed = false;
637        int i = 0;
638        foreach (var gs in seriesCache.Keys) {
639          if (((int)gs) != result[i]) {
640            changed = true;
641            break;
642          }
643          i++;
644        }
645
646        if (changed) {
647          UpdateDataPoints();
648        }
649      } else {
650        UpdateDataPoints();
651      }
652    }
653  }
654}
Note: See TracBrowser for help on using the repository browser.