Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2709 Better initial axis intervals for scatterplots.

File size: 6.2 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.Linq;
25using HeuristicLab.Analysis;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.DataPreprocessing.Views {
30
31  [View("Scatter Plot Single View")]
32  [Content(typeof(SingleScatterPlotContent), true)]
33  public partial class ScatterPlotSingleView : ItemView {
34
35    public new SingleScatterPlotContent Content {
36      get { return (SingleScatterPlotContent)base.Content; }
37      set { base.Content = value; }
38    }
39
40    public ScatterPlotSingleView() {
41      InitializeComponent();
42    }
43
44    public void InitData() {
45      IEnumerable<string> variables = Content.PreprocessingData.GetDoubleVariableNames();
46
47      // add variables to combo boxes
48      comboBoxXVariable.Items.Clear();
49      comboBoxYVariable.Items.Clear();
50      comboBoxGroup.Items.Clear();
51      comboBoxXVariable.Items.AddRange(variables.ToArray());
52      comboBoxYVariable.Items.AddRange(variables.ToArray());
53      comboBoxGroup.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            comboBoxGroup.Items.Add(Content.PreprocessingData.GetVariableName(i));
59        }
60      }
61
62      // use x and y variable from content
63      if (Content.SelectedXVariable != null && Content.SelectedYVariable != null && Content.SelectedGroupVariable != null) {
64        comboBoxXVariable.SelectedItem = Content.SelectedXVariable;
65        comboBoxYVariable.SelectedItem = Content.SelectedYVariable;
66        comboBoxGroup.SelectedItem = Content.SelectedGroupVariable;
67      } else {
68        if (variables.Count() >= 2) {
69          comboBoxXVariable.SelectedIndex = 0;
70          comboBoxYVariable.SelectedIndex = 1;
71          comboBoxGroup.SelectedIndex = 0;
72          UpdateScatterPlot();
73          if (scatterPlotControl.Content != null)
74            foreach (var row in scatterPlotControl.Content.Rows)
75              row.VisualProperties.PointSize = 6;
76        }
77      }
78    }
79
80    protected override void OnContentChanged() {
81      base.OnContentChanged();
82      if (Content != null) {
83        InitData();
84      }
85    }
86
87    private void UpdateScatterPlot() {
88      if (comboBoxXVariable.SelectedItem != null && comboBoxYVariable.SelectedItem != null && comboBoxGroup.SelectedItem != null) {
89        var xVariable = (string)comboBoxXVariable.SelectedItem;
90        var yVariable = (string)comboBoxYVariable.SelectedItem;
91        var groupVariable = (string)comboBoxGroup.SelectedItem;
92        ScatterPlot scatterPlot = Content.CreateScatterPlot(xVariable, yVariable, groupVariable);
93        var vp = scatterPlot.VisualProperties;
94        vp.Title = string.Empty;
95        vp.XAxisTitle = xVariable;
96        vp.YAxisTitle = yVariable;
97
98        scatterPlotControl.Content = scatterPlot;
99
100        //save selected x and y variable in content
101        this.Content.SelectedXVariable = (string)comboBoxXVariable.SelectedItem;
102        this.Content.SelectedYVariable = (string)comboBoxYVariable.SelectedItem;
103        this.Content.SelectedGroupVariable = (string)comboBoxGroup.SelectedItem;
104      }
105    }
106
107    private void comboBoxXVariable_SelectedIndexChanged(object sender, EventArgs e) {
108      var oldPlot = scatterPlotControl.Content;
109      UpdateScatterPlot();
110      var newPlot = scatterPlotControl.Content;
111
112
113      if (oldPlot == null || newPlot == null) return;
114      newPlot.VisualProperties.YAxisMinimumAuto = oldPlot.VisualProperties.YAxisMinimumAuto;
115      newPlot.VisualProperties.YAxisMaximumAuto = oldPlot.VisualProperties.YAxisMaximumAuto;
116      newPlot.VisualProperties.YAxisMinimumFixedValue = oldPlot.VisualProperties.YAxisMinimumFixedValue;
117      newPlot.VisualProperties.YAxisMaximumFixedValue = oldPlot.VisualProperties.YAxisMaximumFixedValue;
118
119      foreach (var x in newPlot.Rows.Zip(oldPlot.Rows, (nr, or) => new { nr, or })) {
120        x.nr.VisualProperties.PointSize = x.or.VisualProperties.PointSize;
121        x.nr.VisualProperties.PointStyle = x.or.VisualProperties.PointStyle;
122        x.nr.VisualProperties.Color = x.or.VisualProperties.Color;
123      }
124    }
125
126    private void comboBoxYVariable_SelectedIndexChanged(object sender, EventArgs e) {
127      var oldPlot = scatterPlotControl.Content;
128      UpdateScatterPlot();
129      var newPlot = scatterPlotControl.Content;
130
131      if (oldPlot == null || newPlot == null) return;
132      newPlot.VisualProperties.XAxisMinimumAuto = oldPlot.VisualProperties.XAxisMinimumAuto;
133      newPlot.VisualProperties.XAxisMaximumAuto = oldPlot.VisualProperties.XAxisMaximumAuto;
134      newPlot.VisualProperties.XAxisMinimumFixedValue = oldPlot.VisualProperties.XAxisMinimumFixedValue;
135      newPlot.VisualProperties.XAxisMaximumFixedValue = oldPlot.VisualProperties.XAxisMaximumFixedValue;
136
137      foreach (var x in newPlot.Rows.Zip(oldPlot.Rows, (nr, or) => new { nr, or })) {
138        x.nr.VisualProperties.PointSize = x.or.VisualProperties.PointSize;
139        x.nr.VisualProperties.PointStyle = x.or.VisualProperties.PointStyle;
140        x.nr.VisualProperties.Color = x.or.VisualProperties.Color;
141      }
142    }
143
144    private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e) {
145      UpdateScatterPlot();
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.