- Timestamp:
- 07/03/17 10:57:53 (7 years ago)
- Location:
- trunk/sources/HeuristicLab.DataPreprocessing.Views
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.DataPreprocessing.Views
- Property svn:mergeinfo changed
-
trunk/sources/HeuristicLab.DataPreprocessing.Views/3.4/ScatterPlotSingleView.cs
r14185 r15110 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 RegressionType = HeuristicLab.Analysis.ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType; 28 31 29 32 namespace HeuristicLab.DataPreprocessing.Views { 30 33 31 34 [View("Scatter Plot Single View")] 32 [Content(typeof(ScatterPlotContent), true)] 33 public partial class ScatterPlotSingleView : ItemView { 34 35 public new ScatterPlotContent Content { 36 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; } 37 41 set { base.Content = value; } 38 42 } … … 40 44 public ScatterPlotSingleView() { 41 45 InitializeComponent(); 42 } 43 44 public void InitData() { 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.Appearance; 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() { 45 67 IEnumerable<string> variables = Content.PreprocessingData.GetDoubleVariableNames(); 46 68 47 // add variables to combo boxes48 69 comboBoxXVariable.Items.Clear(); 49 70 comboBoxYVariable.Items.Clear(); 50 comboBoxColor.Items.Clear(); 71 comboBoxGroup.Items.Clear(); 72 51 73 comboBoxXVariable.Items.AddRange(variables.ToArray()); 52 74 comboBoxYVariable.Items.AddRange(variables.ToArray()); 53 comboBoxColor.Items.Add("-"); 54 for (int i = 0; i < Content.PreprocessingData.Columns; ++i) { 55 if (Content.PreprocessingData.VariableHasType<double>(i)) { 56 double distinctValueCount = Content.PreprocessingData.GetValues<double>(i).GroupBy(x => x).Count(); 57 if (distinctValueCount <= 20) 58 comboBoxColor.Items.Add(Content.PreprocessingData.GetVariableName(i)); 59 } 60 } 75 comboBoxGroup.Items.Add(NoGroupItem); 76 foreach (string var in Content.PreprocessingData.VariableNames) { 77 comboBoxGroup.Items.Add(var); 78 } 79 comboBoxGroup.SelectedItem = Content.GroupingVariable ?? NoGroupItem; 61 80 62 81 // use x and y variable from content 63 if (Content.SelectedXVariable != null && Content.SelectedYVariable != null && Content. SelectedColorVariable != null) {82 if (Content.SelectedXVariable != null && Content.SelectedYVariable != null && Content.GroupingVariable != null) { 64 83 comboBoxXVariable.SelectedItem = Content.SelectedXVariable; 65 84 comboBoxYVariable.SelectedItem = Content.SelectedYVariable; 66 comboBox Color.SelectedItem = Content.SelectedColorVariable;85 comboBoxGroup.SelectedItem = Content.GroupingVariable; 67 86 } else { 68 87 if (variables.Count() >= 2) { 69 88 comboBoxXVariable.SelectedIndex = 0; 70 89 comboBoxYVariable.SelectedIndex = 1; 71 comboBox Color.SelectedIndex = 0;90 comboBoxGroup.SelectedIndex = 0; 72 91 UpdateScatterPlot(); 73 92 } … … 75 94 } 76 95 77 protected override void OnContentChanged() {78 base.OnContentChanged();79 if (Content != null) {80 InitData();81 }82 }83 84 private void comboBox_SelectedIndexChanged(object sender, EventArgs e) {85 UpdateScatterPlot();86 }87 88 96 private void UpdateScatterPlot() { 89 if (comboBoxXVariable.SelectedItem != null && comboBoxYVariable.SelectedItem != null && comboBoxColor.SelectedItem != null) { 90 //get scatter plot with selected x and y variable 91 ScatterPlot scatterPlot = Content.CreateScatterPlot( 92 (string)comboBoxXVariable.SelectedItem, 93 (string)comboBoxYVariable.SelectedItem, 94 (string)comboBoxColor.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 95 122 scatterPlotView.Content = scatterPlot; 96 123 … … 98 125 this.Content.SelectedXVariable = (string)comboBoxXVariable.SelectedItem; 99 126 this.Content.SelectedYVariable = (string)comboBoxYVariable.SelectedItem; 100 this.Content.SelectedColorVariable = (string)comboBoxColor.SelectedItem; 101 } 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 var oldPlot = scatterPlotView.Content; 151 UpdateScatterPlot(); 152 var newPlot = scatterPlotView.Content; 153 154 if (oldPlot == null || newPlot == null) return; 155 newPlot.VisualProperties.XAxisMinimumAuto = oldPlot.VisualProperties.XAxisMinimumAuto; 156 newPlot.VisualProperties.XAxisMaximumAuto = oldPlot.VisualProperties.XAxisMaximumAuto; 157 newPlot.VisualProperties.XAxisMinimumFixedValue = oldPlot.VisualProperties.XAxisMinimumFixedValue; 158 newPlot.VisualProperties.XAxisMaximumFixedValue = oldPlot.VisualProperties.XAxisMaximumFixedValue; 159 160 foreach (var x in newPlot.Rows.Zip(oldPlot.Rows, (nr, or) => new { nr, or })) { 161 var newVisuapProperties = (ScatterPlotDataRowVisualProperties)x.or.VisualProperties.Clone(); 162 newVisuapProperties.DisplayName = x.nr.VisualProperties.DisplayName; 163 x.nr.VisualProperties = newVisuapProperties; 164 } 165 } 166 167 private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e) { 168 useGradientCheckBox.Enabled = (string)comboBoxGroup.SelectedItem != NoGroupItem && Content.PreprocessingData.GetDoubleVariableNames().Contains((string)comboBoxGroup.SelectedItem); 169 gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked; 170 UpdateScatterPlot(); 171 } 172 173 #region Regression Line 174 private void regressionTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { 175 var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue; 176 polynomialRegressionOrderNumericUpDown.Enabled = regressionType == RegressionType.Polynomial; 177 178 UpdateRegressionLine(); 179 } 180 181 private void polynomialRegressionOrderNumericUpDown_ValueChanged(object sender, EventArgs e) { 182 UpdateRegressionLine(); 183 } 184 185 private void UpdateRegressionLine() { 186 if (Content == null) return; 187 188 var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue; 189 int order = (int)polynomialRegressionOrderNumericUpDown.Value; 190 191 foreach (var row in scatterPlotView.Content.Rows) { 192 row.VisualProperties.IsRegressionVisibleInLegend = false; 193 row.VisualProperties.RegressionType = regressionType; 194 row.VisualProperties.PolynomialRegressionOrder = order; 195 } 196 } 197 #endregion 198 199 private void useGradientCheckBox_CheckedChanged(object sender, EventArgs e) { 200 gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked; 201 202 // remove rows and re-add them later to avoid firing visual property changd events 203 var rows = scatterPlotView.Content.Rows.ToDictionary(r => r.Name, r => r); 204 scatterPlotView.Content.Rows.Clear(); 205 206 if (useGradientCheckBox.Checked) { 207 var groupVariable = (string)comboBoxGroup.SelectedItem; 208 if (groupVariable == NoGroupItem) return; 209 210 var groupValues = Content.PreprocessingData.GetValues<double>(Content.PreprocessingData.GetColumnIndex(groupVariable)) 211 .Distinct().OrderBy(x => x).ToList(); 212 double min = groupValues.FirstOrDefault(x => !double.IsNaN(x)), max = groupValues.LastOrDefault(x => !double.IsNaN(x)); 213 foreach (var group in groupValues) { 214 ScatterPlotDataRow row; 215 if (rows.TryGetValue(group.ToString("R"), out row)) { 216 row.VisualProperties.Color = GetColor(group, min, max); 217 row.VisualProperties.IsVisibleInLegend = false; 218 } 219 } 220 gradientMinimumLabel.Text = min.ToString("G5"); 221 gradientMaximumLabel.Text = max.ToString("G5"); 222 } else { 223 foreach (var row in rows.Values) { 224 row.VisualProperties.Color = Color.Empty; 225 row.VisualProperties.IsVisibleInLegend = true; 226 } 227 } 228 scatterPlotView.Content.Rows.AddRange(rows.Values); 229 } 230 231 private static Color GetColor(double value, double min, double max) { 232 if (double.IsNaN(value)) { 233 return Color.Black; 234 } 235 var colors = ColorGradient.Colors; 236 int index = (int)((colors.Count - 1) * (value - min) / (max - min)); 237 if (index >= colors.Count) index = colors.Count - 1; 238 if (index < 0) index = 0; 239 return colors[index]; 240 } 241 242 private void orderComboBox_SelectedIndexChanged(object sender, EventArgs e) { 243 UpdateScatterPlot(); 102 244 } 103 245 } 104 246 } 247
Note: See TracChangeset
for help on using the changeset viewer.