Changeset 15973 for branches/2522_RefactorPluginInfrastructure/HeuristicLab.DataPreprocessing.Views/3.4/ScatterPlotSingleView.cs
- Timestamp:
- 06/28/18 11:13:37 (6 years ago)
- Location:
- branches/2522_RefactorPluginInfrastructure
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2522_RefactorPluginInfrastructure
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/2522_RefactorPluginInfrastructure/HeuristicLab.DataPreprocessing.Views
- Property svn:mergeinfo changed
-
branches/2522_RefactorPluginInfrastructure/HeuristicLab.DataPreprocessing.Views/3.4
- Property svn:ignore
-
old new 1 1 ComparisonFilterView.resx 2 2 FilterView.resx 3 *.user
-
- Property svn:ignore
-
branches/2522_RefactorPluginInfrastructure/HeuristicLab.DataPreprocessing.Views/3.4/ScatterPlotSingleView.cs
r10998 r15973 1 using System; 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 22 using System; 2 23 using System.Collections.Generic; 24 using System.Drawing; 3 25 using System.Linq; 4 using System.Windows.Forms;5 26 using HeuristicLab.Analysis; 27 using HeuristicLab.Common; 6 28 using HeuristicLab.Core.Views; 7 29 using HeuristicLab.MainForm; 30 using RegressionType = HeuristicLab.Analysis.ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType; 8 31 9 32 namespace HeuristicLab.DataPreprocessing.Views { 10 33 11 34 [View("Scatter Plot Single View")] 12 [Content(typeof(ScatterPlotContent), true)] 13 public partial class ScatterPlotSingleView : ItemView { 14 15 public new ScatterPlotContent Content { 16 get { return (ScatterPlotContent)base.Content; } 35 [Content(typeof(SingleScatterPlotContent), true)] 36 public sealed partial class ScatterPlotSingleView : ItemView { 37 private readonly string NoGroupItem = ""; 38 39 public new SingleScatterPlotContent Content { 40 get { return (SingleScatterPlotContent)base.Content; } 17 41 set { base.Content = value; } 18 42 } 19 43 20 public ScatterPlotSingleView() 21 { 44 public ScatterPlotSingleView() { 22 45 InitializeComponent(); 23 } 24 25 public void InitData() { 26 46 47 regressionTypeComboBox.DataSource = Enum.GetValues(typeof(RegressionType)); 48 regressionTypeComboBox.SelectedItem = RegressionType.None; 49 orderComboBox.DataSource = Enum.GetValues(typeof(PreprocessingChartContent.LegendOrder)); 50 orderComboBox.SelectedItem = PreprocessingChartContent.LegendOrder.Alphabetically; 51 } 52 53 protected override void SetEnabledStateOfControls() { 54 base.SetEnabledStateOfControls(); 55 useGradientCheckBox.Enabled = (string)comboBoxGroup.SelectedItem != NoGroupItem; 56 gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked; ; 57 } 58 59 protected override void OnContentChanged() { 60 base.OnContentChanged(); 61 if (Content != null) { 62 InitData(); 63 } 64 } 65 66 private void InitData() { 27 67 IEnumerable<string> variables = Content.PreprocessingData.GetDoubleVariableNames(); 28 68 29 // add variables to combo boxes30 69 comboBoxXVariable.Items.Clear(); 31 70 comboBoxYVariable.Items.Clear(); 71 comboBoxGroup.Items.Clear(); 72 32 73 comboBoxXVariable.Items.AddRange(variables.ToArray()); 33 74 comboBoxYVariable.Items.AddRange(variables.ToArray()); 75 comboBoxGroup.Items.Add(NoGroupItem); 76 foreach (string var in PreprocessingChartContent.GetVariableNamesForGrouping(Content.PreprocessingData, 50)) { 77 comboBoxGroup.Items.Add(var); 78 } 79 comboBoxGroup.SelectedItem = Content.GroupingVariable ?? NoGroupItem; 34 80 35 81 // use x and y variable from content 36 if (Content.SelectedXVariable != null && Content.SelectedYVariable != null ) {82 if (Content.SelectedXVariable != null && Content.SelectedYVariable != null && Content.GroupingVariable != null) { 37 83 comboBoxXVariable.SelectedItem = Content.SelectedXVariable; 38 84 comboBoxYVariable.SelectedItem = Content.SelectedYVariable; 85 comboBoxGroup.SelectedItem = Content.GroupingVariable; 39 86 } else { 40 87 if (variables.Count() >= 2) { 41 88 comboBoxXVariable.SelectedIndex = 0; 42 89 comboBoxYVariable.SelectedIndex = 1; 90 comboBoxGroup.SelectedIndex = 0; 43 91 UpdateScatterPlot(); 44 45 } 46 } 47 } 48 49 protected override void OnContentChanged() { 50 base.OnContentChanged(); 51 if (Content != null) { 52 InitData(); 53 } 54 } 55 56 private void comboBox_SelectedIndexChanged(object sender, EventArgs e) { 57 UpdateScatterPlot(); 92 } 93 } 58 94 } 59 95 60 96 private void UpdateScatterPlot() { 61 if (comboBoxXVariable.SelectedItem != null && comboBoxYVariable.SelectedItem != null) { 62 //get scatter plot with selected x and y variable 63 ScatterPlot scatterPlot = Content.CreateScatterPlot((string)comboBoxXVariable.SelectedItem, (string)comboBoxYVariable.SelectedItem); 97 if (comboBoxXVariable.SelectedItem != null && comboBoxYVariable.SelectedItem != null && comboBoxGroup.SelectedItem != null) { 98 var xVariable = (string)comboBoxXVariable.SelectedItem; 99 var yVariable = (string)comboBoxYVariable.SelectedItem; 100 var groupVariable = (string)comboBoxGroup.SelectedItem; 101 var legendOrder = (PreprocessingChartContent.LegendOrder)orderComboBox.SelectedItem; 102 103 ScatterPlot scatterPlot = ScatterPlotContent.CreateScatterPlot(Content.PreprocessingData, xVariable, yVariable, groupVariable, legendOrder); 104 //rows are saved and removed to avoid firing of visual property changed events 105 var rows = scatterPlot.Rows.ToList(); 106 scatterPlot.Rows.Clear(); 107 var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue; 108 int order = (int)polynomialRegressionOrderNumericUpDown.Value; 109 foreach (var row in rows) { 110 row.VisualProperties.PointSize = 6; 111 row.VisualProperties.IsRegressionVisibleInLegend = false; 112 row.VisualProperties.RegressionType = regressionType; 113 row.VisualProperties.PolynomialRegressionOrder = order; 114 row.VisualProperties.IsVisibleInLegend = !useGradientCheckBox.Checked; 115 } 116 scatterPlot.Rows.AddRange(rows); 117 var vp = scatterPlot.VisualProperties; 118 vp.Title = string.Empty; 119 vp.XAxisTitle = xVariable; 120 vp.YAxisTitle = yVariable; 121 64 122 scatterPlotView.Content = scatterPlot; 65 123 … … 67 125 this.Content.SelectedXVariable = (string)comboBoxXVariable.SelectedItem; 68 126 this.Content.SelectedYVariable = (string)comboBoxYVariable.SelectedItem; 69 } 70 } 71 127 this.Content.GroupingVariable = (string)comboBoxGroup.SelectedItem; 128 } 129 } 130 131 private void comboBoxXVariable_SelectedIndexChanged(object sender, EventArgs e) { 132 var oldPlot = scatterPlotView.Content; 133 UpdateScatterPlot(); 134 var newPlot = scatterPlotView.Content; 135 136 if (oldPlot == null || newPlot == null) return; 137 newPlot.VisualProperties.YAxisMinimumAuto = oldPlot.VisualProperties.YAxisMinimumAuto; 138 newPlot.VisualProperties.YAxisMaximumAuto = oldPlot.VisualProperties.YAxisMaximumAuto; 139 newPlot.VisualProperties.YAxisMinimumFixedValue = oldPlot.VisualProperties.YAxisMinimumFixedValue; 140 newPlot.VisualProperties.YAxisMaximumFixedValue = oldPlot.VisualProperties.YAxisMaximumFixedValue; 141 142 foreach (var x in newPlot.Rows.Zip(oldPlot.Rows, (nr, or) => new { nr, or })) { 143 var newVisuapProperties = (ScatterPlotDataRowVisualProperties)x.or.VisualProperties.Clone(); 144 newVisuapProperties.DisplayName = x.nr.VisualProperties.DisplayName; 145 x.nr.VisualProperties = newVisuapProperties; 146 } 147 } 148 149 private void comboBoxYVariable_SelectedIndexChanged(object sender, EventArgs e) { 150 SuspendRepaint(); 151 var oldPlot = scatterPlotView.Content; 152 UpdateScatterPlot(); 153 var newPlot = scatterPlotView.Content; 154 155 if (oldPlot == null || newPlot == null) return; 156 newPlot.VisualProperties.XAxisMinimumAuto = oldPlot.VisualProperties.XAxisMinimumAuto; 157 newPlot.VisualProperties.XAxisMaximumAuto = oldPlot.VisualProperties.XAxisMaximumAuto; 158 newPlot.VisualProperties.XAxisMinimumFixedValue = oldPlot.VisualProperties.XAxisMinimumFixedValue; 159 newPlot.VisualProperties.XAxisMaximumFixedValue = oldPlot.VisualProperties.XAxisMaximumFixedValue; 160 161 foreach (var x in newPlot.Rows.Zip(oldPlot.Rows, (nr, or) => new { nr, or })) { 162 var newVisuapProperties = (ScatterPlotDataRowVisualProperties)x.or.VisualProperties.Clone(); 163 newVisuapProperties.DisplayName = x.nr.VisualProperties.DisplayName; 164 x.nr.VisualProperties = newVisuapProperties; 165 } 166 ResumeRepaint(true); 167 } 168 169 private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e) { 170 useGradientCheckBox.Enabled = (string)comboBoxGroup.SelectedItem != NoGroupItem && Content.PreprocessingData.GetDoubleVariableNames().Contains((string)comboBoxGroup.SelectedItem); 171 gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked; 172 UpdateScatterPlot(); 173 } 174 175 #region Regression Line 176 private void regressionTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { 177 var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue; 178 polynomialRegressionOrderNumericUpDown.Enabled = regressionType == RegressionType.Polynomial; 179 180 UpdateRegressionLine(); 181 } 182 183 private void polynomialRegressionOrderNumericUpDown_ValueChanged(object sender, EventArgs e) { 184 UpdateRegressionLine(); 185 } 186 187 private void UpdateRegressionLine() { 188 if (Content == null) return; 189 190 var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue; 191 int order = (int)polynomialRegressionOrderNumericUpDown.Value; 192 193 foreach (var row in scatterPlotView.Content.Rows) { 194 row.VisualProperties.IsRegressionVisibleInLegend = false; 195 row.VisualProperties.RegressionType = regressionType; 196 row.VisualProperties.PolynomialRegressionOrder = order; 197 } 198 } 199 #endregion 200 201 private void useGradientCheckBox_CheckedChanged(object sender, EventArgs e) { 202 gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked; 203 204 // remove rows and re-add them later to avoid firing visual property changd events 205 var rows = scatterPlotView.Content.Rows.ToDictionary(r => r.Name, r => r); 206 scatterPlotView.Content.Rows.Clear(); 207 208 if (useGradientCheckBox.Checked) { 209 var groupVariable = (string)comboBoxGroup.SelectedItem; 210 if (groupVariable == NoGroupItem) return; 211 212 var groupValues = Content.PreprocessingData.GetValues<double>(Content.PreprocessingData.GetColumnIndex(groupVariable)) 213 .Distinct().OrderBy(x => x).ToList(); 214 double min = groupValues.FirstOrDefault(x => !double.IsNaN(x)), max = groupValues.LastOrDefault(x => !double.IsNaN(x)); 215 foreach (var group in groupValues) { 216 ScatterPlotDataRow row; 217 if (rows.TryGetValue(group.ToString("R"), out row)) { 218 row.VisualProperties.Color = GetColor(group, min, max); 219 row.VisualProperties.IsVisibleInLegend = false; 220 } 221 } 222 gradientMinimumLabel.Text = min.ToString("G5"); 223 gradientMaximumLabel.Text = max.ToString("G5"); 224 } else { 225 foreach (var row in rows.Values) { 226 row.VisualProperties.Color = Color.Empty; 227 row.VisualProperties.IsVisibleInLegend = true; 228 } 229 } 230 scatterPlotView.Content.Rows.AddRange(rows.Values); 231 } 232 233 private static Color GetColor(double value, double min, double max) { 234 if (double.IsNaN(value)) { 235 return Color.Black; 236 } 237 var colors = ColorGradient.Colors; 238 int index = (int)((colors.Count - 1) * (value - min) / (max - min)); 239 if (index >= colors.Count) index = colors.Count - 1; 240 if (index < 0) index = 0; 241 return colors[index]; 242 } 243 244 private void orderComboBox_SelectedIndexChanged(object sender, EventArgs e) { 245 UpdateScatterPlot(); 246 } 72 247 } 73 74 75 248 } 249
Note: See TracChangeset
for help on using the changeset viewer.