Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionBoxPlotView.cs @ 15621

Last change on this file since 15621 was 15621, checked in by fholzing, 7 years ago

#2880: Changed event from SelectedIndexChanged to SelectionChangeCommitted

File size: 20.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.ComponentModel;
25using System.Linq;
26using System.Windows.Forms;
27using System.Windows.Forms.DataVisualization.Charting;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33
34namespace HeuristicLab.Optimization.Views {
35  [View("Box Plot")]
36  [Content(typeof(RunCollection), false)]
37  public partial class RunCollectionBoxPlotView : AsynchronousContentView {
38    private enum AxisDimension { Color = 0 }
39    private const string BoxPlotSeriesName = "BoxPlotSeries";
40    private const string BoxPlotChartAreaName = "BoxPlotChartArea";
41
42    private bool suppressUpdates = false;
43    private string xAxisValue;
44    private string yAxisValue;
45    private Dictionary<int, Dictionary<object, double>> categoricalMapping;
46    private SortedDictionary<double, Series> seriesCache;
47
48    public RunCollectionBoxPlotView() {
49      InitializeComponent();
50      chart.ContextMenuStrip.Items.Insert(0, openBubbleChartViewToolStripMenuItem);
51
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        run.PropertyChanged += run_PropertyChanged;
98    }
99    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
100      foreach (IRun run in runs)
101        run.PropertyChanged -= run_PropertyChanged;
102    }
103
104    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
105      DeregisterRunEvents(e.OldItems);
106      RegisterRunEvents(e.Items);
107    }
108    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
109      DeregisterRunEvents(e.Items);
110    }
111    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
112      RegisterRunEvents(e.Items);
113    }
114    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
115      if (InvokeRequired)
116        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
117      else {
118        suppressUpdates = Content.UpdateOfRunsInProgress;
119        if (!suppressUpdates) UpdateDataPoints();
120      }
121    }
122
123    private void Content_Reset(object sender, EventArgs e) {
124      if (InvokeRequired)
125        Invoke(new EventHandler(Content_Reset), sender, e);
126      else if (!suppressUpdates) {
127        UpdateDataPoints();
128        UpdateAxisLabels();
129      }
130    }
131    private void Content_ColumnNamesChanged(object sender, EventArgs e) {
132      if (InvokeRequired)
133        Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
134      else {
135        UpdateComboBoxes();
136      }
137    }
138    private void run_PropertyChanged(object sender, PropertyChangedEventArgs e) {
139      if (InvokeRequired)
140        this.Invoke((Action<object, PropertyChangedEventArgs>)run_PropertyChanged, sender, e);
141      else if (!suppressUpdates) {
142        if (e.PropertyName == "Visible")
143          UpdateDataPoints();
144      }
145    }
146
147    private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
148      if (InvokeRequired)
149        Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
150      else UpdateCaption();
151    }
152    #endregion
153
154    #region update comboboxes, datapoints, runs
155    protected override void OnContentChanged() {
156      base.OnContentChanged();
157      this.categoricalMapping.Clear();
158      UpdateComboBoxes();
159      UpdateDataPoints();
160      UpdateCaption();
161    }
162
163    private void UpdateCaption() {
164      Caption = Content != null ? Content.OptimizerName + " Box Plot" : ViewAttribute.GetViewName(GetType());
165    }
166
167    private void UpdateComboBoxes() {
168      string selectedXAxis = (string)this.xAxisComboBox.SelectedItem;
169      string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
170      this.xAxisComboBox.Items.Clear();
171      this.yAxisComboBox.Items.Clear();
172      if (Content != null) {
173        string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
174        this.xAxisComboBox.Items.AddRange(additionalAxisDimension);
175        this.xAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
176        this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
177        this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
178
179        bool changed = false;
180        if (selectedXAxis != null && xAxisComboBox.Items.Contains(selectedXAxis)) {
181          xAxisComboBox.SelectedItem = selectedXAxis;
182          changed = true;
183        }
184        if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
185          yAxisComboBox.SelectedItem = selectedYAxis;
186          changed = true;
187        }
188        if (changed)
189          UpdateDataPoints();
190      }
191    }
192
193    private void UpdateDataPoints() {
194      this.categoricalMapping.Clear();
195      this.chart.Series.Clear();
196      this.seriesCache.Clear();
197      if (Content != null) {
198        foreach (IRun run in this.Content.Where(r => r.Visible))
199          this.AddDataPoint(run);
200        foreach (Series s in this.seriesCache.Values)
201          this.chart.Series.Add(s);
202
203        UpdateStatistics();
204        if (seriesCache.Count > 0) {
205          Series boxPlotSeries = CreateBoxPlotSeries();
206          this.chart.Series.Add(boxPlotSeries);
207        }
208
209        UpdateAxisLabels();
210      }
211      UpdateNoRunsVisibleLabel();
212    }
213
214    private void UpdateStatistics() {
215      DoubleMatrix matrix = new DoubleMatrix(10, seriesCache.Count);
216      matrix.SortableView = false;
217      List<string> columnNames = new List<string>();
218      foreach (Series series in seriesCache.Values) {
219        DataPoint datapoint = series.Points.FirstOrDefault();
220        if (datapoint != null) {
221          IRun run = (IRun)datapoint.Tag;
222          string selectedAxis = xAxisValue;
223          IItem value = null;
224
225          if (Enum.IsDefined(typeof(AxisDimension), selectedAxis)) {
226            AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), selectedAxis);
227            switch (axisDimension) {
228              case AxisDimension.Color:
229                value = new StringValue(run.Color.ToString());
230                break;
231            }
232          } else value = Content.GetValue(run, selectedAxis);
233          string columnName = string.Empty;
234          if (value is DoubleValue || value is IntValue)
235            columnName = selectedAxis + ": ";
236          columnName += value.ToString();
237          columnNames.Add(columnName);
238        }
239      }
240      matrix.ColumnNames = columnNames;
241      matrix.RowNames = new string[] { "Count", "Minimum", "Maximum", "Median", "Average", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile", "Interquartile Range" };
242
243      for (int i = 0; i < seriesCache.Count; i++) {
244        Series series = seriesCache.ElementAt(i).Value;
245        double[] seriesValues = series.Points.Select(p => p.YValues[0]).OrderBy(d => d).ToArray();
246        matrix[0, i] = seriesValues.Length;
247        matrix[1, i] = seriesValues.Min();
248        matrix[2, i] = seriesValues.Max();
249        matrix[3, i] = seriesValues.Median();
250        matrix[4, i] = seriesValues.Average();
251        matrix[5, i] = seriesValues.StandardDeviation();
252        matrix[6, i] = seriesValues.Variance();
253        matrix[7, i] = seriesValues.Quantile(0.25);
254        matrix[8, i] = seriesValues.Quantile(0.75);
255        matrix[9, i] = matrix[8, i] - matrix[7, i];
256      }
257      statisticsMatrixView.Content = matrix;
258    }
259
260    private Series CreateBoxPlotSeries() {
261      Series boxPlotSeries = new Series(BoxPlotSeriesName);
262      string seriesNames = string.Concat(seriesCache.Keys.Select(x => x.ToString() + ";").ToArray());
263      seriesNames = seriesNames.Remove(seriesNames.Length - 1); //delete last ; from string
264
265      boxPlotSeries.ChartArea = BoxPlotChartAreaName;
266      boxPlotSeries.ChartType = SeriesChartType.BoxPlot;
267      boxPlotSeries["BoxPlotSeries"] = seriesNames;
268      boxPlotSeries["BoxPlotShowUnusualValues"] = "true";
269      boxPlotSeries["PointWidth"] = "0.4";
270      boxPlotSeries.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.VerticalCenter;
271      boxPlotSeries.BackSecondaryColor = System.Drawing.Color.FromArgb(130, 224, 64, 10);
272      boxPlotSeries.BorderColor = System.Drawing.Color.FromArgb(64, 64, 64);
273      boxPlotSeries.Color = System.Drawing.Color.FromArgb(224, 64, 10);
274
275      return boxPlotSeries;
276    }
277
278    private void AddDataPoint(IRun run) {
279      double? xValue;
280      double? yValue;
281
282      if (!xAxisComboBox.DroppedDown)
283        this.xAxisValue = (string)xAxisComboBox.SelectedItem;
284      if (!yAxisComboBox.DroppedDown)
285        this.yAxisValue = (string)yAxisComboBox.SelectedItem;
286
287      xValue = GetValue(run, this.xAxisValue);
288      yValue = GetValue(run, this.yAxisValue);
289
290      if (xValue.HasValue && yValue.HasValue) {
291        if (!this.seriesCache.ContainsKey(xValue.Value))
292          seriesCache[xValue.Value] = new Series(xValue.Value.ToString());
293
294        Series series = seriesCache[xValue.Value];
295        DataPoint point = new DataPoint(xValue.Value, yValue.Value);
296        point.Tag = run;
297        series.Points.Add(point);
298      }
299    }
300    #endregion
301
302    #region get values from run
303    private double? GetValue(IRun run, string columnName) {
304      if (run == null || string.IsNullOrEmpty(columnName))
305        return null;
306
307      if (Enum.IsDefined(typeof(AxisDimension), columnName)) {
308        AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
309        return GetValue(run, axisDimension);
310      } else {
311        int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
312        IItem value = Content.GetValue(run, columnIndex);
313        if (value == null)
314          return null;
315
316        DoubleValue doubleValue = value as DoubleValue;
317        IntValue intValue = value as IntValue;
318        TimeSpanValue timeSpanValue = value as TimeSpanValue;
319        double? ret = null;
320        if (doubleValue != null) {
321          if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
322            ret = doubleValue.Value;
323        } else if (intValue != null)
324          ret = intValue.Value;
325        else if (timeSpanValue != null) {
326          ret = timeSpanValue.Value.TotalSeconds;
327        } else
328          ret = GetCategoricalValue(columnIndex, value.ToString());
329
330        return ret;
331      }
332    }
333    private double? GetCategoricalValue(int dimension, string value) {
334      if (!this.categoricalMapping.ContainsKey(dimension)) {
335        this.categoricalMapping[dimension] = new Dictionary<object, double>();
336        var orderedCategories = Content.Where(r => r.Visible && Content.GetValue(r, dimension) != null).Select(r => Content.GetValue(r, dimension).ToString())
337                                       .Distinct().OrderBy(x => x, new NaturalStringComparer());
338        int count = 1;
339        foreach (var category in orderedCategories) {
340          this.categoricalMapping[dimension].Add(category, count);
341          count++;
342        }
343      }
344      if (!this.categoricalMapping[dimension].ContainsKey(value)) return null;
345      return this.categoricalMapping[dimension][value];
346    }
347    private double? GetValue(IRun run, AxisDimension axisDimension) {
348      double? value = double.NaN;
349      switch (axisDimension) {
350        case AxisDimension.Color: {
351            const int colorDimension = -1;
352            if (!categoricalMapping.ContainsKey(colorDimension)) {
353              categoricalMapping[colorDimension] = Content.Where(r => r.Visible)
354                  .Select(r => r.Color.Name)
355                  .Distinct()
356                  .OrderBy(c => c, new NaturalStringComparer())
357                  .Select((c, i) => new { Color = c, Index = i })
358                  .ToDictionary(a => (object)a.Color, a => (double)a.Index);
359
360            }
361            value = GetCategoricalValue(colorDimension, run.Color.Name);
362            break;
363          }
364        default: {
365            throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
366          }
367      }
368      return value;
369    }
370    #endregion
371
372    #region GUI events
373    private void UpdateNoRunsVisibleLabel() {
374      if (this.chart.Series.Count > 0) {
375        noRunsLabel.Visible = false;
376        showStatisticsCheckBox.Enabled = true;
377        splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
378      } else {
379        noRunsLabel.Visible = true;
380        showStatisticsCheckBox.Enabled = false;
381        splitContainer.Panel2Collapsed = true;
382      }
383    }
384
385    private void AxisComboBox_SelectionChangeCommitted(object sender, EventArgs e) {
386      UpdateDataPoints();
387    }
388    private void UpdateAxisLabels() {
389      Axis xAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisX;
390      Axis yAxis = this.chart.ChartAreas[BoxPlotChartAreaName].AxisY;
391      int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
392      //mkommend: combobox.SelectedIndex could not be used as this changes during hovering over possible values
393      var xSAxisSelectedIndex = xAxisValue == null ? 0 : xAxisComboBox.Items.IndexOf(xAxisValue);
394      var ySAxisSelectedIndex = yAxisValue == null ? 0 : xAxisComboBox.Items.IndexOf(yAxisValue);
395      SetCustomAxisLabels(xAxis, xSAxisSelectedIndex - axisDimensionCount);
396      SetCustomAxisLabels(yAxis, ySAxisSelectedIndex - axisDimensionCount);
397      if (xAxisValue != null)
398        xAxis.Title = xAxisValue;
399      if (yAxisValue != null)
400        yAxis.Title = yAxisValue;
401    }
402
403    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
404      this.UpdateAxisLabels();
405    }
406
407    private void SetCustomAxisLabels(Axis axis, int dimension) {
408      axis.CustomLabels.Clear();
409      if (categoricalMapping.ContainsKey(dimension)) {
410        int position = 1;
411        foreach (var pair in categoricalMapping[dimension].Where(x => seriesCache.ContainsKey(x.Value))) {
412          string labelText = pair.Key.ToString();
413          CustomLabel label = new CustomLabel();
414          label.ToolTip = labelText;
415          if (labelText.Length > 25)
416            labelText = labelText.Substring(0, 25) + " ... ";
417          label.Text = labelText;
418          label.GridTicks = GridTickTypes.TickMark;
419          label.FromPosition = position - 0.5;
420          label.ToPosition = position + 0.5;
421          axis.CustomLabels.Add(label);
422          position++;
423        }
424      } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
425        this.chart.ChartAreas[0].RecalculateAxesScale();
426        Axis correspondingAxis = this.chart.ChartAreas[0].Axes.Where(x => x.Name == axis.Name).SingleOrDefault();
427        if (correspondingAxis == null)
428          correspondingAxis = axis;
429        for (double i = correspondingAxis.Minimum; i <= correspondingAxis.Maximum; i += correspondingAxis.LabelStyle.Interval) {
430          TimeSpan time = TimeSpan.FromSeconds(i);
431          string x = string.Format("{0:00}:{1:00}:{2:00}", (int)time.Hours, time.Minutes, time.Seconds);
432          axis.CustomLabels.Add(i - correspondingAxis.LabelStyle.Interval / 2, i + correspondingAxis.LabelStyle.Interval / 2, x);
433        }
434      } else if (chart.ChartAreas[BoxPlotChartAreaName].AxisX == axis) {
435        double position = 1.0;
436        foreach (Series series in chart.Series) {
437          if (series.Name != BoxPlotSeriesName) {
438            string labelText = series.Points[0].XValue.ToString();
439            CustomLabel label = new CustomLabel();
440            label.FromPosition = position - 0.5;
441            label.ToPosition = position + 0.5;
442            label.GridTicks = GridTickTypes.TickMark;
443            label.Text = labelText;
444            axis.CustomLabels.Add(label);
445            position++;
446          }
447        }
448      }
449    }
450
451    private void openBubbleChartViewToolStripMenuItem_Click(object sender, EventArgs e) {
452      RunCollectionBubbleChartView bubbleChartView = new RunCollectionBubbleChartView();
453      bubbleChartView.Content = this.Content;
454      bubbleChartView.xAxisComboBox.SelectedItem = xAxisComboBox.SelectedItem;
455      bubbleChartView.yAxisComboBox.SelectedItem = yAxisComboBox.SelectedItem;
456      bubbleChartView.Show();
457    }
458
459    private void chart_MouseMove(object sender, MouseEventArgs e) {
460      string newTooltipText = string.Empty;
461      string oldTooltipText;
462      HitTestResult h = this.chart.HitTest(e.X, e.Y);
463      if (h.ChartElementType == ChartElementType.AxisLabels) {
464        newTooltipText = ((CustomLabel)h.Object).ToolTip;
465      }
466
467      oldTooltipText = this.tooltip.GetToolTip(chart);
468      if (newTooltipText != oldTooltipText)
469        this.tooltip.SetToolTip(chart, newTooltipText);
470    }
471    #endregion
472
473    private void showStatisticsCheckBox_CheckedChanged(object sender, EventArgs e) {
474      splitContainer.Panel2Collapsed = !showStatisticsCheckBox.Checked;
475    }
476
477    public bool StatisticsVisible {
478      get { return splitContainer.Panel2Collapsed; }
479      set { splitContainer.Panel2Collapsed = value; }
480    }
481
482    public void SetXAxis(string axisName) {
483      xAxisComboBox.SelectedItem = axisName;
484    }
485
486    public void SetYAxis(string axisName) {
487      yAxisComboBox.SelectedItem = axisName;
488    }
489  }
490}
Note: See TracBrowser for help on using the repository browser.