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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Analysis;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core.Views;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 | using HeuristicLab.MainForm.WindowsForms;
|
---|
31 | using RegressionType = HeuristicLab.Analysis.ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType;
|
---|
32 |
|
---|
33 | namespace 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 | scatterPlotControl.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 = 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 })) {
|
---|
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 = 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 })) {
|
---|
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 | scatterPlotControl.Content.Rows.Clear();
|
---|
186 |
|
---|
187 | var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
|
---|
188 | int order = (int)polynomialRegressionOrderNumericUpDown.Value;
|
---|
189 |
|
---|
190 | var rows = scatterPlotControl.Content.Rows.ToList();
|
---|
191 | foreach (var row in rows) {
|
---|
192 | row.VisualProperties.IsRegressionVisibleInLegend = false;
|
---|
193 | row.VisualProperties.RegressionType = regressionType;
|
---|
194 | row.VisualProperties.PolynomialRegressionOrder = order;
|
---|
195 | }
|
---|
196 | scatterPlotControl.Content.Rows.AddRange(rows);
|
---|
197 | }
|
---|
198 | #endregion
|
---|
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();
|
---|
213 | double min = groupValues.FirstOrDefault(x => !double.IsNaN(x)), max = groupValues.LastOrDefault(x => !double.IsNaN(x));
|
---|
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)) {
|
---|
234 | return Color.Black;
|
---|
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 | }
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|