Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14983 was 14983, checked in by pfleck, 7 years ago

#2709 Adapted DataTable/ScatterPlotControl to the recent (re-) merge of the -View and -Control.

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