Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/ScatterPlotMultiView.cs @ 14902

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

#2709

  • Changed chart sizing to absolute values (pixels).
  • Added chart sizing to Linechart and Histogram.
File size: 21.1 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Analysis;
28using HeuristicLab.Analysis.Views;
29using HeuristicLab.Collections;
30using HeuristicLab.Data;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33using RegressionType = HeuristicLab.Analysis.ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType;
34
35namespace HeuristicLab.DataPreprocessing.Views {
36  [View("Scatter Plot Multi View")]
37  [Content(typeof(MultiScatterPlotContent), true)]
38  public partial class ScatterPlotMultiView : PreprocessingCheckedVariablesView {
39    private readonly IDictionary<string, Label> columnHeaderCache = new Dictionary<string, Label>();
40    private readonly IDictionary<string, VerticalLabel> rowHeaderCache = new Dictionary<string, VerticalLabel>();
41    private readonly IDictionary<Tuple<string/*col*/, string/*row*/>, Control> bodyCache = new Dictionary<Tuple<string, string>, Control>();
42
43    public ScatterPlotMultiView() {
44      InitializeComponent();
45
46      regressionTypeComboBox.DataSource = Enum.GetValues(typeof(RegressionType));
47      regressionTypeComboBox.SelectedItem = RegressionType.None;
48
49      #region Initialize Scrollbars
50      columnHeaderScrollPanel.HorizontalScroll.Enabled = true;
51      columnHeaderScrollPanel.VerticalScroll.Enabled = false;
52      columnHeaderScrollPanel.HorizontalScroll.Visible = false;
53      columnHeaderScrollPanel.VerticalScroll.Visible = false;
54
55      rowHeaderScrollPanel.HorizontalScroll.Enabled = false;
56      rowHeaderScrollPanel.VerticalScroll.Enabled = true;
57      rowHeaderScrollPanel.HorizontalScroll.Visible = false;
58      rowHeaderScrollPanel.VerticalScroll.Visible = false;
59
60      bodyScrollPanel.HorizontalScroll.Enabled = true;
61      bodyScrollPanel.VerticalScroll.Enabled = true;
62      bodyScrollPanel.HorizontalScroll.Visible = true;
63      bodyScrollPanel.VerticalScroll.Visible = true;
64      bodyScrollPanel.AutoScroll = true;
65      #endregion
66
67      bodyScrollPanel.MouseWheel += bodyScrollPanel_MouseWheel;
68    }
69
70    public new MultiScatterPlotContent Content {
71      get { return (MultiScatterPlotContent)base.Content; }
72      set { base.Content = value; }
73    }
74
75    protected override void OnContentChanged() {
76      base.OnContentChanged();
77      if (Content != null) {
78        groupingComboBox.Items.Add("");
79        foreach (string var in PreprocessingChartContent.GetVariableNamesForGrouping(Content.PreprocessingData)) {
80          groupingComboBox.Items.Add(var);
81        }
82        groupingComboBox.SelectedItem = Content.GroupingVariable;
83
84        GenerateCharts();
85      }
86    }
87
88    protected override void SetEnabledStateOfControls() {
89      base.SetEnabledStateOfControls();
90      var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
91      polynomialRegressionOrderNumericUpDown.Enabled = regressionType == RegressionType.Polynomial;
92    }
93
94    protected override void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
95      base.CheckedItemsChanged(sender, checkedItems);
96      if (suppressCheckedChangedUpdate)
97        return;
98      foreach (var variable in checkedItems.Items.Select(i => i.Value.Value)) {
99        if (IsVariableChecked(variable))
100          AddChartToTable(variable);
101        else
102          RemoveChartFromTable(variable);
103      }
104    }
105
106    #region Add and remove charts
107    private void AddChartToTable(string variable) {
108      frameTableLayoutPanel.SuspendLayout();
109
110      // find index to insert
111      var variables = Content.VariableItemList.Select(v => v.Value).ToList();
112      int idx = variables              // all variables
113        .TakeWhile(t => t != variable) // ... until the variable that was checked
114        .Count(IsVariableChecked);     // ... how many checked variables
115
116      // add column header
117      var colH = columnHeaderTableLayoutPanel;
118      AddColumnHelper(colH, idx, _ => GetColumnHeader(variable));
119
120      // add row header
121      var rowH = rowHeaderTableLayoutPanel;
122      AddRowHelper(rowH, idx, _ => GetRowHeader(variable));
123
124      // add body
125      var body = bodyTableLayoutPanel;
126      var vars = GetCheckedVariables();
127      var varsMinus = vars.Except(new[] { variable }).ToList();
128      AddColumnHelper(body, idx, r => GetBody(variable, varsMinus[r])); // exclude "variable" because the row for it does not exist yet
129      AddRowHelper(body, idx, c => GetBody(vars[c], variable));
130
131      frameTableLayoutPanel.ResumeLayout(true);
132    }
133    private void AddColumnHelper(TableLayoutPanel tlp, int idx, Func<int, Control> creatorFunc) {
134      // add column
135      tlp.ColumnCount++;
136      tlp.ColumnStyles.Insert(idx, new ColumnStyle(SizeType.Absolute, GetColumnWidth()));
137      // shift right
138      for (int c = tlp.ColumnCount; c > idx - 1; c--) {
139        for (int r = 0; r < tlp.RowCount; r++) {
140          var control = tlp.GetControlFromPosition(c, r);
141          if (control != null) {
142            tlp.SetColumn(control, c + 1);
143          }
144        }
145      }
146      // add controls
147      for (int r = 0; r < tlp.RowCount; r++) {
148        if (tlp.GetControlFromPosition(idx, r) == null)
149          tlp.Controls.Add(creatorFunc(r), idx, r);
150      }
151
152    }
153    private void AddRowHelper(TableLayoutPanel tlp, int idx, Func<int, Control> creatorFunc) {
154      // add row
155      tlp.RowCount++;
156      tlp.RowStyles.Insert(idx, new RowStyle(SizeType.Absolute, GetRowHeight()));
157      // shift right
158      for (int r = tlp.RowCount; r > idx - 1; r--) {
159        for (int c = 0; c < tlp.ColumnCount; c++) {
160          var control = tlp.GetControlFromPosition(c, r);
161          if (control != null) {
162            tlp.SetRow(control, r + 1);
163          }
164        }
165      }
166      // add controls
167      for (int c = 0; c < tlp.ColumnCount; c++)
168        if (tlp.GetControlFromPosition(c, idx) == null)
169          tlp.Controls.Add(creatorFunc(c), c, idx);
170    }
171
172    private void RemoveChartFromTable(string variable) {
173      frameTableLayoutPanel.SuspendLayout();
174
175      // remove column header
176      var colH = columnHeaderTableLayoutPanel;
177      int colIdx = colH.GetColumn(colH.Controls[variable]);
178      RemoveColumnHelper(colH, colIdx);
179
180      // remove row header
181      var rowH = rowHeaderTableLayoutPanel;
182      int rowIdx = rowH.GetRow(rowH.Controls[variable]);
183      RemoveRowHelper(rowH, rowIdx);
184
185      // remove from body
186      var body = bodyTableLayoutPanel;
187      RemoveColumnHelper(body, colIdx);
188      RemoveRowHelper(body, rowIdx);
189
190      frameTableLayoutPanel.ResumeLayout(true);
191    }
192    private void RemoveColumnHelper(TableLayoutPanel tlp, int idx) {
193      // remove controls
194      for (int r = 0; r < tlp.RowCount; r++)
195        tlp.Controls.Remove(tlp.GetControlFromPosition(idx, r));
196      // shift left
197      for (int c = idx + 1; c < tlp.ColumnCount; c++) {
198        for (int r = 0; r < tlp.RowCount; r++) {
199          var control = tlp.GetControlFromPosition(c, r);
200          if (control != null) {
201            tlp.SetColumn(control, c - 1);
202          }
203        }
204      }
205      // delete column
206      tlp.ColumnStyles.RemoveAt(tlp.ColumnCount - 1);
207      tlp.ColumnCount--;
208    }
209    private void RemoveRowHelper(TableLayoutPanel tlp, int idx) {
210      // remove controls
211      for (int c = 0; c < tlp.ColumnCount; c++)
212        tlp.Controls.Remove(tlp.GetControlFromPosition(c, idx));
213      // shift left
214      for (int r = idx + 1; r < tlp.RowCount; r++) {
215        for (int c = 0; c < tlp.ColumnCount; c++) {
216          var control = tlp.GetControlFromPosition(c, r);
217          if (control != null) {
218            tlp.SetRow(control, r - 1);
219          }
220        }
221      }
222      // delete rows
223      tlp.RowStyles.RemoveAt(tlp.RowCount - 1);
224      tlp.RowCount--;
225    }
226    #endregion
227
228    #region Add/Remove/Update Variable
229    protected override void AddVariable(string name) {
230      base.AddVariable(name);
231      if (IsVariableChecked(name))
232        AddChartToTable(name);
233    }
234    protected override void RemoveVariable(string name) {
235      base.RemoveVariable(name);
236
237      // clear caches
238      columnHeaderCache.Remove(name);
239      rowHeaderCache.Remove(name);
240      var keys = bodyCache.Keys.Where(t => t.Item1 == name || t.Item2 == name).ToList();
241      foreach (var key in keys)
242        bodyCache.Remove(key);
243
244      if (IsVariableChecked(name)) {
245        RemoveChartFromTable(name);
246      }
247    }
248    protected override void UpdateVariable(string name) {
249      base.UpdateVariable(name);
250      RemoveVariable(name);
251      AddVariable(name);
252    }
253    protected override void ResetAllVariables() {
254      GenerateCharts();
255    }
256    #endregion
257
258    #region Creating Headers and Body
259    private Label GetColumnHeader(string variable) {
260      if (!columnHeaderCache.ContainsKey(variable)) {
261        columnHeaderCache.Add(variable, new Label() {
262          Text = variable,
263          TextAlign = ContentAlignment.MiddleCenter,
264          Name = variable,
265          Height = columnHeaderTableLayoutPanel.Height,
266          Dock = DockStyle.Fill,
267          Margin = new Padding(3)
268        });
269      }
270      return columnHeaderCache[variable];
271    }
272    private Label GetRowHeader(string variable) {
273      if (!rowHeaderCache.ContainsKey(variable)) {
274        rowHeaderCache.Add(variable, new VerticalLabel() {
275          Text = variable,
276          TextAlign = ContentAlignment.MiddleCenter,
277          Name = variable,
278          Width = rowHeaderTableLayoutPanel.Width,
279          Height = columnHeaderScrollPanel.Width,
280          Dock = DockStyle.Fill,
281          Margin = new Padding(3)
282        });
283      }
284      return rowHeaderCache[variable];
285    }
286    private Control GetBody(string colVariable, string rowVariable) {
287      var key = Tuple.Create(colVariable, rowVariable);
288      if (!bodyCache.ContainsKey(key)) {
289        if (rowVariable == colVariable) { // use historgram if x and y variable are equal
290          var dataTable = HistogramContent.CreateHistogram(Content.PreprocessingData, rowVariable, (string)groupingComboBox.SelectedItem, DataRowVisualProperties.DataRowHistogramAggregation.Overlapping);
291          foreach (var dataRow in dataTable.Rows)
292            dataRow.VisualProperties.IsVisibleInLegend = false;
293
294          var pcv = new DataTableControl {
295            Name = key.ToString(),
296            Content = dataTable,
297            Dock = DockStyle.Fill,
298            //ShowLegend = false,
299            //XAxisFormat = "G3"
300          };
301          //pcv.ChartDoubleClick += HistogramDoubleClick;
302          bodyCache.Add(key, pcv);
303        } else { //scatter plot
304          var scatterPlot = ScatterPlotContent.CreateScatterPlot(Content.PreprocessingData, colVariable, rowVariable, (string)groupingComboBox.SelectedItem);
305          var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
306          int order = (int)polynomialRegressionOrderNumericUpDown.Value;
307          foreach (var row in scatterPlot.Rows) {
308            row.VisualProperties.PointSize = 3;
309            row.VisualProperties.IsRegressionVisibleInLegend = false;
310            row.VisualProperties.RegressionType = regressionType;
311            row.VisualProperties.PolynomialRegressionOrder = order;
312          }
313          scatterPlot.VisualProperties.Title = string.Empty;
314          var scatterPlotControl = new ScatterPlotControl {
315            Name = key.ToString(),
316            Content = scatterPlot,
317            Dock = DockStyle.Fill,
318            //ShowLegend = false,
319            //XAxisFormat = "G3"
320          };
321          //scatterPlotControl.DoubleClick += ScatterPlotDoubleClick; // ToDo: not working; double click is already handled by the chart
322          bodyCache.Add(key, scatterPlotControl);
323        }
324      }
325      return bodyCache[key];
326    }
327    #endregion
328
329    protected override void CheckedChangedUpdate() {
330      GenerateCharts();
331    }
332
333    #region Generate Charts
334    private void GenerateCharts() {
335      if (suppressCheckedChangedUpdate) return;
336
337      var variables = GetCheckedVariables();
338
339      // Clear old layouts and cache
340      foreach (var tableLayoutPanel in new[] { columnHeaderTableLayoutPanel, rowHeaderTableLayoutPanel, bodyTableLayoutPanel }) {
341        tableLayoutPanel.Controls.Clear();
342        tableLayoutPanel.ColumnStyles.Clear();
343        tableLayoutPanel.RowStyles.Clear();
344      }
345      columnHeaderCache.Clear();
346      rowHeaderCache.Clear();
347      bodyCache.Clear();
348
349      // Set row and column count
350      columnHeaderTableLayoutPanel.ColumnCount = variables.Count;
351      rowHeaderTableLayoutPanel.RowCount = variables.Count;
352      bodyTableLayoutPanel.ColumnCount = variables.Count;
353      bodyTableLayoutPanel.RowCount = variables.Count;
354
355      // Set column and row layout
356      for (int i = 0; i < variables.Count; i++) {
357        columnHeaderTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, GetColumnWidth()));
358        rowHeaderTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, GetRowHeight()));
359        bodyTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, GetColumnWidth()));
360        bodyTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, GetRowHeight()));
361      }
362
363      frameTableLayoutPanel.SuspendLayout();
364      AddHeaderToTableLayoutPanels();
365      AddChartsToTableLayoutPanel();
366      UpdateHeaderMargin();
367      frameTableLayoutPanel.ResumeLayout(true);
368    }
369
370    private void AddHeaderToTableLayoutPanels() {
371      int i = 0;
372      foreach (var variable in GetCheckedVariables()) {
373        columnHeaderTableLayoutPanel.Controls.Add(GetColumnHeader(variable), i, 0);
374        rowHeaderTableLayoutPanel.Controls.Add(GetRowHeader(variable), 0, i);
375        i++;
376      }
377    }
378    private void AddChartsToTableLayoutPanel() {
379      int c = 0;
380      foreach (var colVar in GetCheckedVariables()) {
381        if (!IsVariableChecked(colVar)) continue;
382        int r = 0;
383        foreach (var rowVar in GetCheckedVariables()) {
384          if (!IsVariableChecked(rowVar)) continue;
385          bodyTableLayoutPanel.Controls.Add(GetBody(colVar, rowVar), c, r);
386          r++;
387        }
388        c++;
389      }
390      UpdateRegressionLine();
391    }
392
393    #endregion
394
395    #region DoubleClick Events
396    //Open scatter plot in new tab with new content when double clicked
397    private void ScatterPlotDoubleClick(object sender, EventArgs e) {
398      var scatterPlotControl = (ScatterPlotControl)sender;
399      var scatterContent = new SingleScatterPlotContent(Content.PreprocessingData);
400      ScatterPlot scatterPlot = scatterPlotControl.Content;
401
402      //Extract variable names from scatter plot and set them in content
403      if (scatterPlot.Rows.Count == 1) {
404        string[] variables = scatterPlot.Rows.ElementAt(0).Name.Split(new string[] { " - " }, StringSplitOptions.None); // extract variable names from string
405        scatterContent.SelectedXVariable = variables[0];
406        scatterContent.SelectedYVariable = variables[1];
407      }
408
409      MainFormManager.MainForm.ShowContent(scatterContent, typeof(ScatterPlotSingleView));  // open in new tab
410    }
411
412    //open histogram in new tab with new content when double clicked
413    private void HistogramDoubleClick(object sender, EventArgs e) {
414      DataTableControl pcv = (DataTableControl)sender;
415      HistogramContent histoContent = new HistogramContent(Content.PreprocessingData);  // create new content     
416      //ToDo: histoContent.VariableItemList = Content.CreateVariableItemList();
417      var dataTable = pcv.Content;
418
419      //Set variable item list from with variable from data table
420      if (dataTable.Rows.Count == 1) { // only one data row should be in data table
421        string variableName = dataTable.Rows.ElementAt(0).Name;
422
423        // set only variable name checked
424        foreach (var checkedItem in histoContent.VariableItemList) {
425          histoContent.VariableItemList.SetItemCheckedState(checkedItem, checkedItem.Value == variableName);
426        }
427      }
428      MainFormManager.MainForm.ShowContent(histoContent, typeof(HistogramView));  // open in new tab
429    }
430    #endregion
431
432    #region Scrolling
433    private void bodyScrollPanel_Scroll(object sender, ScrollEventArgs e) {
434      SyncScroll();
435
436      UpdateHeaderMargin();
437    }
438    private void bodyScrollPanel_MouseWheel(object sender, MouseEventArgs e) {
439      // Scrolling with the mouse wheel is not captured in the Scoll event
440      SyncScroll();
441    }
442    private void SyncScroll() {
443      frameTableLayoutPanel.SuspendRepaint();
444
445      columnHeaderScrollPanel.HorizontalScroll.Minimum = bodyScrollPanel.HorizontalScroll.Minimum;
446      columnHeaderScrollPanel.HorizontalScroll.Maximum = bodyScrollPanel.HorizontalScroll.Maximum;
447      rowHeaderScrollPanel.VerticalScroll.Minimum = bodyScrollPanel.VerticalScroll.Minimum;
448      rowHeaderScrollPanel.VerticalScroll.Maximum = bodyScrollPanel.VerticalScroll.Maximum;
449
450      columnHeaderScrollPanel.HorizontalScroll.Value = Math.Max(bodyScrollPanel.HorizontalScroll.Value, 1);
451      rowHeaderScrollPanel.VerticalScroll.Value = Math.Max(bodyScrollPanel.VerticalScroll.Value, 1);
452      // minimum 1 is nececary  because of two factors:
453      // - setting the Value-property of Horizontal/VerticalScroll updates the internal state but the Value-property stays 0
454      // - setting the same number of the Value-property has no effect
455      // since the Value-property is always 0, setting it to 0 would have no effect; so it is set to 1 instead
456
457      frameTableLayoutPanel.ResumeRepaint(true);
458    }
459    // add a margin to the header table layouts if the scollbar is visible to account for the width/height of the scrollbar
460    private void UpdateHeaderMargin() {
461      columnHeaderScrollPanel.Margin = new Padding(0, 0, bodyScrollPanel.VerticalScroll.Visible ? SystemInformation.VerticalScrollBarWidth : 0, 0);
462      rowHeaderScrollPanel.Margin = new Padding(0, 0, 0, bodyScrollPanel.HorizontalScroll.Visible ? SystemInformation.HorizontalScrollBarHeight : 0);
463    }
464    #endregion
465
466    #region Sizing of Charts
467
468    private int GetColumnWidth() { return (int)widthNumericUpDown.Value; }
469    private int GetRowHeight() { return (int)heightNumericUpDown.Value; }
470    private void widthNumericUpDown_ValueChanged(object sender, EventArgs e) {
471      frameTableLayoutPanel.SuspendRepaint();
472      for (int i = 0; i < columnHeaderTableLayoutPanel.ColumnCount; i++) {
473        columnHeaderTableLayoutPanel.ColumnStyles[i].Width = GetColumnWidth();
474        bodyTableLayoutPanel.ColumnStyles[i].Width = GetColumnWidth();
475      }
476      frameTableLayoutPanel.ResumeRepaint(true);
477    }
478    private void heightNumericUpDown_ValueChanged(object sender, EventArgs e) {
479      frameTableLayoutPanel.SuspendRepaint();
480      for (int i = 0; i < rowHeaderTableLayoutPanel.RowCount; i++) {
481        rowHeaderTableLayoutPanel.RowStyles[i].Height = GetRowHeight();
482        bodyTableLayoutPanel.RowStyles[i].Height = GetRowHeight();
483      }
484      frameTableLayoutPanel.ResumeRepaint(true);
485    }
486    #endregion
487
488    #region Regression Line
489    private void regressionTypeComboBox_SelectedValueChanged(object sender, EventArgs e) {
490      var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
491      polynomialRegressionOrderNumericUpDown.Enabled = regressionType == RegressionType.Polynomial;
492      UpdateRegressionLine();
493    }
494
495    private void polynomialRegressionOrderNumericUpDown_ValueChanged(object sender, EventArgs e) {
496      UpdateRegressionLine();
497    }
498
499    private void UpdateRegressionLine() {
500      var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
501      int order = (int)polynomialRegressionOrderNumericUpDown.Value;
502
503      foreach (var control in bodyCache.Values) {
504        var scatterPlotControl = control as ScatterPlotControl;
505        if (scatterPlotControl != null) {
506          foreach (var row in scatterPlotControl.Content.Rows) {
507            row.VisualProperties.IsRegressionVisibleInLegend = false;
508            row.VisualProperties.RegressionType = regressionType;
509            row.VisualProperties.PolynomialRegressionOrder = order;
510          }
511        }
512      }
513    }
514    #endregion
515
516    private void groupingComboBox_SelectedIndexChanged(object sender, EventArgs e) {
517      GenerateCharts();
518    }
519  }
520}
521
Note: See TracBrowser for help on using the repository browser.