Changeset 14521 for branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/ScatterPlotSingleView.cs
- Timestamp:
- 12/22/16 12:42:09 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/ScatterPlotSingleView.cs
r14514 r14521 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Drawing; 24 25 using System.Linq; 25 26 using HeuristicLab.Analysis; 27 using HeuristicLab.Common; 26 28 using HeuristicLab.Core.Views; 27 29 using HeuristicLab.MainForm; 30 using HeuristicLab.MainForm.WindowsForms; 28 31 using RegressionType = HeuristicLab.Analysis.ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType; 29 32 … … 33 36 [Content(typeof(SingleScatterPlotContent), true)] 34 37 public partial class ScatterPlotSingleView : ItemView { 38 39 private readonly string NoGroupItem = "-"; 35 40 36 41 public new SingleScatterPlotContent Content { … … 55 60 comboBoxXVariable.Items.AddRange(variables.ToArray()); 56 61 comboBoxYVariable.Items.AddRange(variables.ToArray()); 57 comboBoxGroup.Items.Add( "-");62 comboBoxGroup.Items.Add(NoGroupItem); 58 63 for (int i = 0; i < Content.PreprocessingData.Columns; ++i) { 59 64 if (Content.PreprocessingData.VariableHasType<double>(i)) { 60 double distinctValueCount = Content.PreprocessingData.GetValues<double>(i).GroupBy(x => x).Count();61 if (distinctValueCount <= 20)62 65 //double distinctValueCount = Content.PreprocessingData.GetValues<double>(i).GroupBy(x => x).Count(); 66 //if (distinctValueCount <= 20) 67 comboBoxGroup.Items.Add(Content.PreprocessingData.GetVariableName(i)); 63 68 } 64 69 } … … 86 91 } 87 92 93 protected override void SetEnabledStateOfControls() { 94 base.SetEnabledStateOfControls(); 95 useGradientCheckBox.Enabled = (string)comboBoxGroup.SelectedItem != NoGroupItem; 96 gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked; ; 97 } 98 88 99 private void UpdateScatterPlot() { 89 100 if (comboBoxXVariable.SelectedItem != null && comboBoxYVariable.SelectedItem != null && comboBoxGroup.SelectedItem != null) { … … 91 102 var yVariable = (string)comboBoxYVariable.SelectedItem; 92 103 var groupVariable = (string)comboBoxGroup.SelectedItem; 104 105 bool groupingActive = useGradientCheckBox.Checked && groupVariable != NoGroupItem; 106 double min = 0, max = 1; 107 if (groupingActive) { 108 var groupValues = Content.PreprocessingData.GetValues<double>(Content.PreprocessingData.GetColumnIndex(groupVariable)) 109 .Distinct().OrderBy(x => x).ToList(); 110 min = groupValues.First(); 111 max = groupValues.Last(); 112 } 93 113 ScatterPlot scatterPlot = Content.CreateScatterPlot(xVariable, yVariable, groupVariable); 114 var rows = scatterPlot.Rows.ToList(); 115 scatterPlot.Rows.Clear(); 94 116 var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue; 95 117 int order = (int)polynomialRegressionOrderNumericUpDown.Value; 96 foreach (var row in scatterPlot.Rows) {118 foreach (var row in rows) { 97 119 row.VisualProperties.PointSize = 6; 98 120 row.VisualProperties.IsRegressionVisibleInLegend = false; 99 121 row.VisualProperties.RegressionType = regressionType; 100 122 row.VisualProperties.PolynomialRegressionOrder = order; 101 } 123 row.VisualProperties.IsVisibleInLegend = !useGradientCheckBox.Checked; 124 if (groupingActive) 125 row.VisualProperties.Color = GetColor(double.Parse(row.Name), min, max); 126 } 127 scatterPlot.Rows.AddRange(rows); 102 128 var vp = scatterPlot.VisualProperties; 103 129 vp.Title = string.Empty; … … 106 132 107 133 scatterPlotControl.Content = scatterPlot; 134 135 if (groupingActive) { 136 gradientMinimumLabel.Text = min.ToString("G5"); 137 gradientMaximumLabel.Text = max.ToString("G5"); 138 } 108 139 109 140 //save selected x and y variable in content … … 152 183 153 184 private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e) { 185 useGradientCheckBox.Enabled = (string)comboBoxGroup.SelectedItem != NoGroupItem; 186 gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked; 154 187 UpdateScatterPlot(); 155 188 } … … 159 192 var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue; 160 193 polynomialRegressionOrderNumericUpDown.Enabled = regressionType == RegressionType.Polynomial; 194 161 195 UpdateRegressionLine(); 162 196 } … … 170 204 int order = (int)polynomialRegressionOrderNumericUpDown.Value; 171 205 172 foreach (var row in scatterPlotControl.Content.Rows) { 206 var rows = scatterPlotControl.Content.Rows.ToList(); 207 scatterPlotControl.Content.Rows.Clear(); 208 foreach (var row in rows) { 173 209 row.VisualProperties.IsRegressionVisibleInLegend = false; 174 210 row.VisualProperties.RegressionType = regressionType; 175 211 row.VisualProperties.PolynomialRegressionOrder = order; 176 212 } 213 scatterPlotControl.Content.Rows.AddRange(rows); 177 214 } 178 215 #endregion 216 217 private void useGradientCheckBox_CheckedChanged(object sender, EventArgs e) { 218 gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked; 219 220 // remove rows and re-add them later to avoid firing visual property changd events 221 var rows = scatterPlotControl.Content.Rows.ToDictionary(r => r.Name, r => r); 222 scatterPlotControl.Content.Rows.Clear(); 223 224 if (useGradientCheckBox.Checked) { 225 var groupVariable = (string)comboBoxGroup.SelectedItem; 226 if (groupVariable == NoGroupItem) return; 227 228 var groupValues = Content.PreprocessingData.GetValues<double>(Content.PreprocessingData.GetColumnIndex(groupVariable)) 229 .Distinct().OrderBy(x => x).ToList(); 230 double min = groupValues.First(), max = groupValues.Last(); 231 foreach (var group in groupValues) { 232 ScatterPlotDataRow row; 233 if (rows.TryGetValue(group.ToString("R"), out row)) { 234 row.VisualProperties.Color = GetColor(group, min, max); 235 row.VisualProperties.IsVisibleInLegend = false; 236 } 237 } 238 gradientMinimumLabel.Text = min.ToString("G5"); 239 gradientMaximumLabel.Text = max.ToString("G5"); 240 } else { 241 foreach (var row in rows.Values) { 242 row.VisualProperties.Color = Color.Empty; 243 row.VisualProperties.IsVisibleInLegend = true; 244 } 245 } 246 scatterPlotControl.Content.Rows.AddRange(rows.Values); 247 } 248 249 private static Color GetColor(double value, double min, double max) { 250 if (double.IsNaN(value)) { 251 return Color.DarkGray; 252 } 253 var colors = ColorGradient.Colors; 254 int index = (int)((colors.Count - 1) * (value - min) / (max - min)); 255 if (index >= colors.Count) index = colors.Count - 1; 256 if (index < 0) index = 0; 257 return colors[index]; 258 } 259 260 private void BatchRowUpdate(Action<ScatterPlotDataRow> rowAction) { 261 var scatterPlot = scatterPlotControl.Content; 262 var rows = scatterPlot.Rows.ToList(); 263 // remove rows and re-add them later to avoid firing visual property changd events 264 scatterPlot.Rows.Clear(); 265 foreach (var row in rows) { 266 rowAction(row); 267 } 268 scatterPlot.Rows.AddRange(rows); 269 } 179 270 } 180 271 }
Note: See TracChangeset
for help on using the changeset viewer.