Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/ScatterPlotSingleView.cs @ 14876

Last change on this file since 14876 was 14725, checked in by mkommend, 8 years ago

#2709: Added grouping for multi scatter plot view.

File size: 10.6 KB
RevLine 
[13502]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[13502]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;
[10882]23using System.Collections.Generic;
[14521]24using System.Drawing;
[10882]25using System.Linq;
26using HeuristicLab.Analysis;
[14521]27using HeuristicLab.Common;
[10882]28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
[14521]30using HeuristicLab.MainForm.WindowsForms;
[14514]31using RegressionType = HeuristicLab.Analysis.ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType;
[10882]32
33namespace HeuristicLab.DataPreprocessing.Views {
34
[10987]35  [View("Scatter Plot Single View")]
[14467]36  [Content(typeof(SingleScatterPlotContent), true)]
[14724]37  public sealed partial class ScatterPlotSingleView : ItemView {
38    private readonly string NoGroupItem = "";
[10882]39
[14467]40    public new SingleScatterPlotContent Content {
41      get { return (SingleScatterPlotContent)base.Content; }
[10882]42      set { base.Content = value; }
43    }
44
[13502]45    public ScatterPlotSingleView() {
[10998]46      InitializeComponent();
[14514]47
48      regressionTypeComboBox.DataSource = Enum.GetValues(typeof(RegressionType));
49      regressionTypeComboBox.SelectedItem = RegressionType.None;
[10998]50    }
51
[14724]52    protected override void SetEnabledStateOfControls() {
53      base.SetEnabledStateOfControls();
54      useGradientCheckBox.Enabled = (string)comboBoxGroup.SelectedItem != NoGroupItem;
55      gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked; ;
56    }
57
58    protected override void OnContentChanged() {
59      base.OnContentChanged();
60      if (Content != null) {
61        InitData();
62      }
63    }
64
65    private void InitData() {
[10992]66      IEnumerable<string> variables = Content.PreprocessingData.GetDoubleVariableNames();
[10972]67
[10882]68      comboBoxXVariable.Items.Clear();
69      comboBoxYVariable.Items.Clear();
[14467]70      comboBoxGroup.Items.Clear();
[14724]71
[10882]72      comboBoxXVariable.Items.AddRange(variables.ToArray());
73      comboBoxYVariable.Items.AddRange(variables.ToArray());
[14521]74      comboBoxGroup.Items.Add(NoGroupItem);
[14725]75      foreach (string var in PreprocessingChartContent.GetVariableNamesForGrouping(Content.PreprocessingData)) {
[14724]76        comboBoxGroup.Items.Add(var);
[13502]77      }
[14725]78      comboBoxGroup.SelectedItem = Content.GroupingVariable;
[10882]79
[10952]80      // use x and y variable from content
[14725]81      if (Content.SelectedXVariable != null && Content.SelectedYVariable != null && Content.GroupingVariable != null) {
[10952]82        comboBoxXVariable.SelectedItem = Content.SelectedXVariable;
83        comboBoxYVariable.SelectedItem = Content.SelectedYVariable;
[14725]84        comboBoxGroup.SelectedItem = Content.GroupingVariable;
[10952]85      } else {
86        if (variables.Count() >= 2) {
87          comboBoxXVariable.SelectedIndex = 0;
88          comboBoxYVariable.SelectedIndex = 1;
[14467]89          comboBoxGroup.SelectedIndex = 0;
[10952]90          UpdateScatterPlot();
91        }
[10882]92      }
93    }
94
95    private void UpdateScatterPlot() {
[14467]96      if (comboBoxXVariable.SelectedItem != null && comboBoxYVariable.SelectedItem != null && comboBoxGroup.SelectedItem != null) {
[14446]97        var xVariable = (string)comboBoxXVariable.SelectedItem;
98        var yVariable = (string)comboBoxYVariable.SelectedItem;
[14467]99        var groupVariable = (string)comboBoxGroup.SelectedItem;
[14521]100
[14725]101        ScatterPlot scatterPlot = ScatterPlotContent.CreateScatterPlot(Content.PreprocessingData, xVariable, yVariable, groupVariable);
[14724]102        //rows are saved and removed to avoid firing of visual property changed events
[14521]103        var rows = scatterPlot.Rows.ToList();
104        scatterPlot.Rows.Clear();
[14514]105        var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
106        int order = (int)polynomialRegressionOrderNumericUpDown.Value;
[14521]107        foreach (var row in rows) {
[14495]108          row.VisualProperties.PointSize = 6;
[14514]109          row.VisualProperties.IsRegressionVisibleInLegend = false;
110          row.VisualProperties.RegressionType = regressionType;
111          row.VisualProperties.PolynomialRegressionOrder = order;
[14521]112          row.VisualProperties.IsVisibleInLegend = !useGradientCheckBox.Checked;
[14514]113        }
[14521]114        scatterPlot.Rows.AddRange(rows);
[14446]115        var vp = scatterPlot.VisualProperties;
116        vp.Title = string.Empty;
117        vp.XAxisTitle = xVariable;
118        vp.YAxisTitle = yVariable;
[10952]119
[14446]120        scatterPlotControl.Content = scatterPlot;
[14467]121
[10952]122        //save selected x and y variable in content
123        this.Content.SelectedXVariable = (string)comboBoxXVariable.SelectedItem;
124        this.Content.SelectedYVariable = (string)comboBoxYVariable.SelectedItem;
[14725]125        this.Content.GroupingVariable = (string)comboBoxGroup.SelectedItem;
[10882]126      }
127    }
[14470]128
129    private void comboBoxXVariable_SelectedIndexChanged(object sender, EventArgs e) {
130      var oldPlot = scatterPlotControl.Content;
131      UpdateScatterPlot();
132      var newPlot = scatterPlotControl.Content;
133
134      if (oldPlot == null || newPlot == null) return;
135      newPlot.VisualProperties.YAxisMinimumAuto = oldPlot.VisualProperties.YAxisMinimumAuto;
136      newPlot.VisualProperties.YAxisMaximumAuto = oldPlot.VisualProperties.YAxisMaximumAuto;
137      newPlot.VisualProperties.YAxisMinimumFixedValue = oldPlot.VisualProperties.YAxisMinimumFixedValue;
138      newPlot.VisualProperties.YAxisMaximumFixedValue = oldPlot.VisualProperties.YAxisMaximumFixedValue;
139
140      foreach (var x in newPlot.Rows.Zip(oldPlot.Rows, (nr, or) => new { nr, or })) {
[14495]141        var newVisuapProperties = (ScatterPlotDataRowVisualProperties)x.or.VisualProperties.Clone();
142        newVisuapProperties.DisplayName = x.nr.VisualProperties.DisplayName;
143        x.nr.VisualProperties = newVisuapProperties;
[14470]144      }
145    }
146
147    private void comboBoxYVariable_SelectedIndexChanged(object sender, EventArgs e) {
148      var oldPlot = scatterPlotControl.Content;
149      UpdateScatterPlot();
150      var newPlot = scatterPlotControl.Content;
151
152      if (oldPlot == null || newPlot == null) return;
153      newPlot.VisualProperties.XAxisMinimumAuto = oldPlot.VisualProperties.XAxisMinimumAuto;
154      newPlot.VisualProperties.XAxisMaximumAuto = oldPlot.VisualProperties.XAxisMaximumAuto;
155      newPlot.VisualProperties.XAxisMinimumFixedValue = oldPlot.VisualProperties.XAxisMinimumFixedValue;
156      newPlot.VisualProperties.XAxisMaximumFixedValue = oldPlot.VisualProperties.XAxisMaximumFixedValue;
157
158      foreach (var x in newPlot.Rows.Zip(oldPlot.Rows, (nr, or) => new { nr, or })) {
[14495]159        var newVisuapProperties = (ScatterPlotDataRowVisualProperties)x.or.VisualProperties.Clone();
160        newVisuapProperties.DisplayName = x.nr.VisualProperties.DisplayName;
161        x.nr.VisualProperties = newVisuapProperties;
[14470]162      }
163    }
164
165    private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e) {
[14724]166      useGradientCheckBox.Enabled = (string)comboBoxGroup.SelectedItem != NoGroupItem && Content.PreprocessingData.GetDoubleVariableNames().Contains((string)comboBoxGroup.SelectedItem);
[14521]167      gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked;
[14470]168      UpdateScatterPlot();
169    }
[14514]170
171    #region Regression Line
172    private void regressionTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) {
173      var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
174      polynomialRegressionOrderNumericUpDown.Enabled = regressionType == RegressionType.Polynomial;
[14521]175
[14514]176      UpdateRegressionLine();
177    }
178
179    private void polynomialRegressionOrderNumericUpDown_ValueChanged(object sender, EventArgs e) {
180      UpdateRegressionLine();
181    }
182
183    private void UpdateRegressionLine() {
[14724]184      if (Content == null) return;
185      scatterPlotControl.Content.Rows.Clear();
186
[14514]187      var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
188      int order = (int)polynomialRegressionOrderNumericUpDown.Value;
189
[14521]190      var rows = scatterPlotControl.Content.Rows.ToList();
191      foreach (var row in rows) {
[14514]192        row.VisualProperties.IsRegressionVisibleInLegend = false;
193        row.VisualProperties.RegressionType = regressionType;
194        row.VisualProperties.PolynomialRegressionOrder = order;
195      }
[14521]196      scatterPlotControl.Content.Rows.AddRange(rows);
[14514]197    }
198    #endregion
[14521]199
200    private void useGradientCheckBox_CheckedChanged(object sender, EventArgs e) {
201      gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked;
202
203      // remove rows and re-add them later to avoid firing visual property changd events
204      var rows = scatterPlotControl.Content.Rows.ToDictionary(r => r.Name, r => r);
205      scatterPlotControl.Content.Rows.Clear();
206
207      if (useGradientCheckBox.Checked) {
208        var groupVariable = (string)comboBoxGroup.SelectedItem;
209        if (groupVariable == NoGroupItem) return;
210
211        var groupValues = Content.PreprocessingData.GetValues<double>(Content.PreprocessingData.GetColumnIndex(groupVariable))
212          .Distinct().OrderBy(x => x).ToList();
[14525]213        double min = groupValues.FirstOrDefault(x => !double.IsNaN(x)), max = groupValues.LastOrDefault(x => !double.IsNaN(x));
[14521]214        foreach (var group in groupValues) {
215          ScatterPlotDataRow row;
216          if (rows.TryGetValue(group.ToString("R"), out row)) {
217            row.VisualProperties.Color = GetColor(group, min, max);
218            row.VisualProperties.IsVisibleInLegend = false;
219          }
220        }
221        gradientMinimumLabel.Text = min.ToString("G5");
222        gradientMaximumLabel.Text = max.ToString("G5");
223      } else {
224        foreach (var row in rows.Values) {
225          row.VisualProperties.Color = Color.Empty;
226          row.VisualProperties.IsVisibleInLegend = true;
227        }
228      }
229      scatterPlotControl.Content.Rows.AddRange(rows.Values);
230    }
231
232    private static Color GetColor(double value, double min, double max) {
233      if (double.IsNaN(value)) {
[14525]234        return Color.Black;
[14521]235      }
236      var colors = ColorGradient.Colors;
237      int index = (int)((colors.Count - 1) * (value - min) / (max - min));
238      if (index >= colors.Count) index = colors.Count - 1;
239      if (index < 0) index = 0;
240      return colors[index];
241    }
[10882]242  }
243}
[14514]244
Note: See TracBrowser for help on using the repository browser.