Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2709

  • Added Legend order when grouping for histogram and (single and multi)scatterplot.
  • Removed the limitation of distinct values for the singlescatterplot (for the color gradient).
  • Added a legend-visible checkbox for the multi-scatterplot.
File size: 31.9 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 AggregationType = HeuristicLab.Analysis.DataRowVisualProperties.DataRowHistogramAggregation;
34using RegressionType = HeuristicLab.Analysis.ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType;
35
36namespace HeuristicLab.DataPreprocessing.Views {
37  [View("Scatter Plot Multi View")]
38  [Content(typeof(MultiScatterPlotContent), true)]
39  public partial class ScatterPlotMultiView : PreprocessingCheckedVariablesView {
40    private readonly IDictionary<string, Label> columnHeaderCache = new Dictionary<string, Label>();
41    private readonly IDictionary<string, VerticalLabel> rowHeaderCache = new Dictionary<string, VerticalLabel>();
42    private readonly IDictionary<Tuple<string/*col*/, string/*row*/>, Control> bodyCache = new Dictionary<Tuple<string, string>, Control>();
43
44    public ScatterPlotMultiView() {
45      InitializeComponent();
46
47      oldWidth = (int)widthNumericUpDown.Value;
48      oldHeight = (int)heightNumericUpDown.Value;
49
50      regressionTypeComboBox.DataSource = Enum.GetValues(typeof(RegressionType));
51      regressionTypeComboBox.SelectedItem = RegressionType.None;
52
53      aggregationComboBox.DataSource = Enum.GetValues(typeof(AggregationType));
54      aggregationComboBox.SelectedItem = AggregationType.Overlapping;
55
56      legendOrderComboBox.DataSource = Enum.GetValues(typeof(PreprocessingChartContent.LegendOrder));
57      legendOrderComboBox.SelectedItem = PreprocessingChartContent.LegendOrder.Appearance;
58
59      #region Initialize Scrollbars
60      columnHeaderScrollPanel.HorizontalScroll.Enabled = true;
61      columnHeaderScrollPanel.VerticalScroll.Enabled = false;
62      columnHeaderScrollPanel.HorizontalScroll.Visible = false;
63      columnHeaderScrollPanel.VerticalScroll.Visible = false;
64
65      rowHeaderScrollPanel.HorizontalScroll.Enabled = false;
66      rowHeaderScrollPanel.VerticalScroll.Enabled = true;
67      rowHeaderScrollPanel.HorizontalScroll.Visible = false;
68      rowHeaderScrollPanel.VerticalScroll.Visible = false;
69
70      bodyScrollPanel.HorizontalScroll.Enabled = true;
71      bodyScrollPanel.VerticalScroll.Enabled = true;
72      bodyScrollPanel.HorizontalScroll.Visible = true;
73      bodyScrollPanel.VerticalScroll.Visible = true;
74      bodyScrollPanel.AutoScroll = true;
75      #endregion
76
77      bodyScrollPanel.MouseWheel += bodyScrollPanel_MouseWheel;
78    }
79
80    public new MultiScatterPlotContent Content {
81      get { return (MultiScatterPlotContent)base.Content; }
82      set { base.Content = value; }
83    }
84
85    protected override void OnContentChanged() {
86      base.OnContentChanged();
87      if (Content != null) {
88        groupingComboBox.Items.Add("");
89        foreach (string var in PreprocessingChartContent.GetVariableNamesForGrouping(Content.PreprocessingData)) {
90          groupingComboBox.Items.Add(var);
91        }
92        groupingComboBox.SelectedItem = Content.GroupingVariable;
93
94        // uncheck variables that max 20 vars are selected initially
95        var list = Content.VariableItemList;
96        int numChecked = list.CheckedItems.Count();
97        if (numChecked > 20) {
98          string message = string.Format("Displaying {0} variables ({1} charts) can cause HeuristicLab to become instable. " +
99                                         "Should the number of initially checked variables be reduced to 20?",
100            numChecked, numChecked * numChecked);
101          if (MessageBox.Show(this, message, "Reduce befor continue?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) {
102            suppressCheckedChangedUpdate = true;
103            foreach (var var in list.CheckedItems.Reverse().Take(numChecked - 20)) {
104              Content.VariableItemList.SetItemCheckedState(var.Value, false);
105            }
106            suppressCheckedChangedUpdate = false;
107          }
108        }
109        GenerateCharts(true);
110      }
111    }
112
113    protected override void SetEnabledStateOfControls() {
114      base.SetEnabledStateOfControls();
115      var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
116      polynomialRegressionOrderNumericUpDown.Enabled = regressionType == RegressionType.Polynomial;
117      sizeGroupBox.Enabled = Content != null;
118      pointsGroupBox.Enabled = Content != null;
119      groupingComboBox.Enabled = Content != null;
120      regressionGroupBox.Enabled = Content != null;
121    }
122
123    protected override void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
124      base.CheckedItemsChanged(sender, checkedItems);
125      if (suppressCheckedChangedUpdate) return;
126
127      foreach (var variable in checkedItems.Items) {
128        if (Content.VariableItemList.ItemChecked(variable.Value))
129          ShowChartOnTable(variable.Value.Value, variable.Index);
130        else
131          HideChartFromTable(variable.Value.Value, variable.Index);
132      }
133    }
134
135    #region Show and Hide charts
136    private void ShowChartOnTable(string variable, int idx) {
137      frameTableLayoutPanel.SuspendLayout();
138
139      // show column header
140      var colH = columnHeaderTableLayoutPanel;
141      colH.ColumnStyles[idx].Width = GetColumnWidth();
142      if (colH.GetControlFromPosition(idx, 0) == null)
143        colH.Controls.Add(GetColumnHeader(variable), idx, 0);
144      else
145        colH.GetControlFromPosition(idx, 0).Visible = true;
146
147      // show row header
148      var rowH = rowHeaderTableLayoutPanel;
149      rowH.RowStyles[idx].Height = GetRowHeight();
150      if (rowH.GetControlFromPosition(0, idx) == null)
151        rowH.Controls.Add(GetRowHeader(variable), 0, idx);
152      else
153        rowH.GetControlFromPosition(0, idx).Visible = true;
154
155      // show body
156      var body = bodyTableLayoutPanel;
157      ShowColumnHelper(body, idx, r => GetBody(variable, Content.VariableItemList[r].Value));
158      ShowRowHelper(body, idx, c => GetBody(Content.VariableItemList[c].Value, variable));
159
160      frameTableLayoutPanel.ResumeLayout(true);
161    }
162    private void ShowColumnHelper(TableLayoutPanel tlp, int idx, Func<int, Control> creatorFunc) {
163      tlp.ColumnStyles[idx].Width = GetColumnWidth();
164      for (int r = 0; r < tlp.RowCount; r++) {
165        if (Content.VariableItemList.ItemChecked(Content.VariableItemList[r])) {
166          var control = tlp.GetControlFromPosition(idx, r);
167          if (control == null)
168            tlp.Controls.Add(creatorFunc(r), idx, r);
169          else
170            control.Visible = true;
171        }
172      }
173    }
174    private void ShowRowHelper(TableLayoutPanel tlp, int idx, Func<int, Control> creatorFunc) {
175      tlp.RowStyles[idx].Height = GetRowHeight();
176      for (int c = 0; c < tlp.ColumnCount; c++) {
177        if (Content.VariableItemList.ItemChecked(Content.VariableItemList[c])) {
178          var control = tlp.GetControlFromPosition(c, idx);
179          if (control == null)
180            tlp.Controls.Add(creatorFunc(c), c, idx);
181          else
182            tlp.GetControlFromPosition(c, idx).Visible = true;
183        }
184      }
185    }
186
187    private void HideChartFromTable(string variable, int idx) {
188      frameTableLayoutPanel.SuspendLayout();
189
190      // hide column header
191      var colH = columnHeaderTableLayoutPanel;
192      HideColumnHelper(colH, idx);
193
194      // hide row header
195      var rowH = rowHeaderTableLayoutPanel;
196      HideRowHelper(rowH, idx);
197
198      // hide from body
199      var body = bodyTableLayoutPanel;
200      HideColumnHelper(body, idx);
201      HideRowHelper(body, idx);
202
203      frameTableLayoutPanel.ResumeLayout(true);
204    }
205    private void HideColumnHelper(TableLayoutPanel tlp, int idx) {
206      tlp.ColumnStyles[idx].Width = 0;
207      // hide controls
208      for (int r = 0; r < tlp.RowCount; r++) {
209        //tlp.Controls.Remove(tlp.GetControlFromPosition(idx, r));
210        var control = tlp.GetControlFromPosition(idx, r);
211        if (control != null)
212          control.Visible = false;
213      }
214    }
215    private void HideRowHelper(TableLayoutPanel tlp, int idx) {
216      tlp.RowStyles[idx].Height = 0;
217      // hide controls
218      for (int c = 0; c < tlp.ColumnCount; c++) {
219        //tlp.Controls.Remove(tlp.GetControlFromPosition(c, idx));
220        var control = tlp.GetControlFromPosition(c, idx);
221        if (control != null)
222          control.Visible = false;
223      }
224    }
225    #endregion
226
227    #region Add/Remove/Update Variable
228    protected override void AddVariable(string name) {
229      base.AddVariable(name);
230      if (IsVariableChecked(name))
231        AddChartToTable(name);
232    }
233    protected override void RemoveVariable(string name) {
234      base.RemoveVariable(name);
235
236      if (IsVariableChecked(name)) {
237        RemoveChartFromTable(name);
238      }
239
240      // clear caches
241      columnHeaderCache[name].Dispose();
242      columnHeaderCache.Remove(name);
243      rowHeaderCache[name].Dispose();
244      rowHeaderCache.Remove(name);
245      var keys = bodyCache.Keys.Where(t => t.Item1 == name || t.Item2 == name).ToList();
246      foreach (var key in keys) {
247        bodyCache[key].Dispose();
248        bodyCache.Remove(key);
249      }
250    }
251    protected override void UpdateVariable(string name) {
252      base.UpdateVariable(name);
253      RemoveVariable(name);
254      AddVariable(name);
255    }
256    protected override void ResetAllVariables() {
257      GenerateCharts(true);
258    }
259
260    private void AddChartToTable(string variable) {
261      frameTableLayoutPanel.SuspendLayout();
262
263      // find index to insert
264      var variables = Content.VariableItemList.Select(v => v.Value).ToList();
265      int idx = variables              // all variables
266        .TakeWhile(t => t != variable) // ... until the variable that was checked
267        .Count(IsVariableChecked);     // ... how many checked variables
268
269      // add column header
270      var colH = columnHeaderTableLayoutPanel;
271      AddColumnHelper(colH, idx, _ => GetColumnHeader(variable));
272
273      // add row header
274      var rowH = rowHeaderTableLayoutPanel;
275      AddRowHelper(rowH, idx, _ => GetRowHeader(variable));
276
277      // add body
278      var body = bodyTableLayoutPanel;
279      var vars = GetCheckedVariables();
280      var varsMinus = vars.Except(new[] { variable }).ToList();
281      AddColumnHelper(body, idx, r => GetBody(variable, varsMinus[r])); // exclude "variable" because the row for it does not exist yet
282      AddRowHelper(body, idx, c => GetBody(vars[c], variable));
283
284      frameTableLayoutPanel.ResumeLayout(true);
285    }
286    private void AddColumnHelper(TableLayoutPanel tlp, int idx, Func<int, Control> creatorFunc) {
287      // add column
288      tlp.ColumnCount++;
289      tlp.ColumnStyles.Insert(idx, new ColumnStyle(SizeType.Absolute, GetColumnWidth()));
290      // shift right
291      for (int c = tlp.ColumnCount; c > idx - 1; c--) {
292        for (int r = 0; r < tlp.RowCount; r++) {
293          var control = tlp.GetControlFromPosition(c, r);
294          if (control != null) {
295            tlp.SetColumn(control, c + 1);
296          }
297        }
298      }
299      // add controls
300      for (int r = 0; r < tlp.RowCount; r++) {
301        if (tlp.GetControlFromPosition(idx, r) == null)
302          tlp.Controls.Add(creatorFunc(r), idx, r);
303      }
304
305    }
306    private void AddRowHelper(TableLayoutPanel tlp, int idx, Func<int, Control> creatorFunc) {
307      // add row
308      tlp.RowCount++;
309      tlp.RowStyles.Insert(idx, new RowStyle(SizeType.Absolute, GetRowHeight()));
310      // shift right
311      for (int r = tlp.RowCount; r > idx - 1; r--) {
312        for (int c = 0; c < tlp.ColumnCount; c++) {
313          var control = tlp.GetControlFromPosition(c, r);
314          if (control != null) {
315            tlp.SetRow(control, r + 1);
316          }
317        }
318      }
319      // add controls
320      for (int c = 0; c < tlp.ColumnCount; c++)
321        if (tlp.GetControlFromPosition(c, idx) == null)
322          tlp.Controls.Add(creatorFunc(c), c, idx);
323    }
324
325    private void RemoveChartFromTable(string variable) {
326      frameTableLayoutPanel.SuspendLayout();
327
328      // remove column header
329      var colH = columnHeaderTableLayoutPanel;
330      int colIdx = colH.GetColumn(colH.Controls[variable]);
331      RemoveColumnHelper(colH, colIdx);
332
333      // remove row header
334      var rowH = rowHeaderTableLayoutPanel;
335      int rowIdx = rowH.GetRow(rowH.Controls[variable]);
336      RemoveRowHelper(rowH, rowIdx);
337
338      // remove from body
339      var body = bodyTableLayoutPanel;
340      RemoveColumnHelper(body, colIdx);
341      RemoveRowHelper(body, rowIdx);
342
343      frameTableLayoutPanel.ResumeLayout(true);
344    }
345    private void RemoveColumnHelper(TableLayoutPanel tlp, int idx) {
346      // remove controls
347      for (int r = 0; r < tlp.RowCount; r++)
348        tlp.Controls.Remove(tlp.GetControlFromPosition(idx, r));
349      // shift left
350      for (int c = idx + 1; c < tlp.ColumnCount; c++) {
351        for (int r = 0; r < tlp.RowCount; r++) {
352          var control = tlp.GetControlFromPosition(c, r);
353          if (control != null) {
354            tlp.SetColumn(control, c - 1);
355          }
356        }
357      }
358      // delete column
359      tlp.ColumnStyles.RemoveAt(tlp.ColumnCount - 1);
360      tlp.ColumnCount--;
361    }
362    private void RemoveRowHelper(TableLayoutPanel tlp, int idx) {
363      // remove controls
364      for (int c = 0; c < tlp.ColumnCount; c++)
365        tlp.Controls.Remove(tlp.GetControlFromPosition(c, idx));
366      // shift left
367      for (int r = idx + 1; r < tlp.RowCount; r++) {
368        for (int c = 0; c < tlp.ColumnCount; c++) {
369          var control = tlp.GetControlFromPosition(c, r);
370          if (control != null) {
371            tlp.SetRow(control, r - 1);
372          }
373        }
374      }
375      // delete rows
376      tlp.RowStyles.RemoveAt(tlp.RowCount - 1);
377      tlp.RowCount--;
378    }
379    #endregion
380
381    #region Creating Headers and Body
382    private Label GetColumnHeader(string variable) {
383      if (!columnHeaderCache.ContainsKey(variable)) {
384        columnHeaderCache.Add(variable, new Label() {
385          Text = variable,
386          TextAlign = ContentAlignment.MiddleCenter,
387          Name = variable,
388          Height = columnHeaderTableLayoutPanel.Height,
389          Dock = DockStyle.Fill,
390          Margin = new Padding(3)
391        });
392      }
393      return columnHeaderCache[variable];
394    }
395    private Label GetRowHeader(string variable) {
396      if (!rowHeaderCache.ContainsKey(variable)) {
397        rowHeaderCache.Add(variable, new VerticalLabel() {
398          Text = variable,
399          TextAlign = ContentAlignment.MiddleCenter,
400          Name = variable,
401          Width = rowHeaderTableLayoutPanel.Width,
402          Height = columnHeaderScrollPanel.Width,
403          Dock = DockStyle.Fill,
404          Margin = new Padding(3)
405        });
406      }
407      return rowHeaderCache[variable];
408    }
409    private Control GetBody(string colVariable, string rowVariable) {
410      var key = Tuple.Create(colVariable, rowVariable);
411      if (!bodyCache.ContainsKey(key)) {
412        if (rowVariable == colVariable) { // use historgram if x and y variable are equal
413          var dataTable = HistogramContent.CreateHistogram(
414            Content.PreprocessingData,
415            rowVariable,
416            (string)groupingComboBox.SelectedItem,
417            (AggregationType)aggregationComboBox.SelectedItem,
418            (PreprocessingChartContent.LegendOrder)legendOrderComboBox.SelectedItem);
419          dataTable.VisualProperties.Title = string.Empty;
420          foreach (var dataRow in dataTable.Rows) {
421            dataRow.VisualProperties.IsVisibleInLegend = legendCheckbox.Checked && groupingComboBox.SelectedIndex > 0;
422          }
423          var pcv = new DataTableView {
424            Name = key.ToString(),
425            Content = dataTable,
426            Dock = DockStyle.Fill,
427            ShowName = false
428            //ShowLegend = false,
429            //XAxisFormat = "G3"
430          };
431          //pcv.ChartDoubleClick += HistogramDoubleClick;
432          bodyCache.Add(key, pcv);
433        } else { //scatter plot
434          var scatterPlot = ScatterPlotContent.CreateScatterPlot(Content.PreprocessingData,
435            colVariable,
436            rowVariable,
437            (string)groupingComboBox.SelectedItem,
438            (PreprocessingChartContent.LegendOrder)legendOrderComboBox.SelectedItem);
439          var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
440          int order = (int)polynomialRegressionOrderNumericUpDown.Value;
441          int i = 0;
442          var colors = PreprocessingChartView.Colors;
443          foreach (var row in scatterPlot.Rows) {
444            row.VisualProperties.PointSize = (int)pointSizeNumericUpDown.Value;
445            row.VisualProperties.Color = Color.FromArgb((int)(pointOpacityNumericUpDown.Value * 255),
446              row.VisualProperties.Color.IsEmpty ? colors[i++ % colors.Length] : row.VisualProperties.Color);
447            row.VisualProperties.IsVisibleInLegend = legendCheckbox.Checked && groupingComboBox.SelectedIndex > 0;
448            row.VisualProperties.IsRegressionVisibleInLegend = false;
449            row.VisualProperties.RegressionType = regressionType;
450            row.VisualProperties.PolynomialRegressionOrder = order;
451          }
452          scatterPlot.VisualProperties.Title = string.Empty;
453          var scatterPlotView = new ScatterPlotView {
454            Name = key.ToString(),
455            Content = scatterPlot,
456            Dock = DockStyle.Fill,
457            ShowName = false
458            //ShowLegend = false,
459            //XAxisFormat = "G3"
460          };
461          //scatterPlotView.DoubleClick += ScatterPlotDoubleClick; // ToDo: not working; double click is already handled by the chart
462          bodyCache.Add(key, scatterPlotView);
463        }
464      }
465      return bodyCache[key];
466    }
467    #endregion
468
469    protected override void CheckedChangedUpdate() {
470      GenerateCharts(false); // only checked-changes -> reuse cached values
471    }
472
473    #region Generate Charts
474    private void GenerateCharts(bool clearCache) {
475      if (Content == null || suppressCheckedChangedUpdate) return;
476
477      // Clear old layouts and cache
478      foreach (var tableLayoutPanel in new[] { columnHeaderTableLayoutPanel, rowHeaderTableLayoutPanel, bodyTableLayoutPanel }) {
479        tableLayoutPanel.Controls.Clear();
480        tableLayoutPanel.ColumnStyles.Clear();
481        tableLayoutPanel.RowStyles.Clear();
482      }
483
484      if (clearCache) {
485        foreach (var control in bodyCache.Values.Concat(columnHeaderCache.Values).Concat(rowHeaderCache.Values)) {
486          control.Dispose();
487        }
488        columnHeaderCache.Clear();
489        rowHeaderCache.Clear();
490        bodyCache.Clear();
491      }
492
493      var variables = Content.VariableItemList.Select(x => x.Value).ToList();
494
495      // Set row and column count
496      columnHeaderTableLayoutPanel.ColumnCount = variables.Count;
497      rowHeaderTableLayoutPanel.RowCount = variables.Count;
498      bodyTableLayoutPanel.ColumnCount = variables.Count;
499      bodyTableLayoutPanel.RowCount = variables.Count;
500
501      // Set column and row layout
502      for (int i = 0; i < variables.Count; i++) {
503        bool @checked = Content.VariableItemList.ItemChecked(Content.VariableItemList[i]);
504        columnHeaderTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, @checked ? GetColumnWidth() : 0));
505        rowHeaderTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, @checked ? GetRowHeight() : 0));
506        bodyTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, @checked ? GetColumnWidth() : 0));
507        bodyTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, @checked ? GetRowHeight() : 0));
508      }
509
510      frameTableLayoutPanel.SuspendLayout();
511      AddHeaderToTableLayoutPanels();
512      AddChartsToTableLayoutPanel();
513      UpdateHeaderMargin();
514      frameTableLayoutPanel.ResumeLayout(true);
515    }
516
517    private void AddHeaderToTableLayoutPanels() {
518      for (int i = 0; i < Content.VariableItemList.Count; i++) {
519        var variable = Content.VariableItemList[i];
520        if (Content.VariableItemList.ItemChecked(variable)) {
521          columnHeaderTableLayoutPanel.Controls.Add(GetColumnHeader(variable.Value), i, 0);
522          rowHeaderTableLayoutPanel.Controls.Add(GetRowHeader(variable.Value), 0, i);
523        }
524      }
525    }
526    private void AddChartsToTableLayoutPanel() {
527      for (int c = 0; c < Content.VariableItemList.Count; c++) {
528        var colVar = Content.VariableItemList[c].Value;
529        if (!IsVariableChecked(colVar)) continue;
530        for (int r = 0; r < Content.VariableItemList.Count; r++) {
531          var rowVar = Content.VariableItemList[r].Value;
532          if (!IsVariableChecked(rowVar)) continue;
533          bodyTableLayoutPanel.Controls.Add(GetBody(colVar, rowVar), c, r);
534        }
535      }
536      UpdateRegressionLine();
537    }
538
539    #endregion
540
541    #region DoubleClick Events
542    //Open scatter plot in new tab with new content when double clicked
543    private void ScatterPlotDoubleClick(object sender, EventArgs e) {
544      var scatterPlotView = (ScatterPlotView)sender;
545      var scatterContent = new SingleScatterPlotContent(Content.PreprocessingData);
546      ScatterPlot scatterPlot = scatterPlotView.Content;
547
548      //Extract variable names from scatter plot and set them in content
549      if (scatterPlot.Rows.Count == 1) {
550        string[] variables = scatterPlot.Rows.ElementAt(0).Name.Split(new string[] { " - " }, StringSplitOptions.None); // extract variable names from string
551        scatterContent.SelectedXVariable = variables[0];
552        scatterContent.SelectedYVariable = variables[1];
553      }
554
555      MainFormManager.MainForm.ShowContent(scatterContent, typeof(ScatterPlotSingleView)); // open in new tab
556    }
557
558    //open histogram in new tab with new content when double clicked
559    private void HistogramDoubleClick(object sender, EventArgs e) {
560      DataTableView pcv = (DataTableView)sender;
561      HistogramContent histoContent = new HistogramContent(Content.PreprocessingData);  // create new content     
562                                                                                        //ToDo: histoContent.VariableItemList = Content.CreateVariableItemList();
563      var dataTable = pcv.Content;
564
565      //Set variable item list from with variable from data table
566      if (dataTable.Rows.Count == 1) { // only one data row should be in data table
567        string variableName = dataTable.Rows.ElementAt(0).Name;
568
569        // set only variable name checked
570        foreach (var checkedItem in histoContent.VariableItemList) {
571          histoContent.VariableItemList.SetItemCheckedState(checkedItem, checkedItem.Value == variableName);
572        }
573      }
574      MainFormManager.MainForm.ShowContent(histoContent, typeof(HistogramView)); // open in new tab
575    }
576    #endregion
577
578    #region Scrolling
579    private void bodyScrollPanel_Scroll(object sender, ScrollEventArgs e) {
580      SyncScroll();
581
582      UpdateHeaderMargin();
583    }
584    private void bodyScrollPanel_MouseWheel(object sender, MouseEventArgs e) {
585      // Scrolling with the mouse wheel is not captured in the Scoll event
586      SyncScroll();
587    }
588    private void SyncScroll() {
589      frameTableLayoutPanel.SuspendRepaint();
590
591      columnHeaderScrollPanel.HorizontalScroll.Minimum = bodyScrollPanel.HorizontalScroll.Minimum;
592      columnHeaderScrollPanel.HorizontalScroll.Maximum = bodyScrollPanel.HorizontalScroll.Maximum;
593      rowHeaderScrollPanel.VerticalScroll.Minimum = bodyScrollPanel.VerticalScroll.Minimum;
594      rowHeaderScrollPanel.VerticalScroll.Maximum = bodyScrollPanel.VerticalScroll.Maximum;
595
596      columnHeaderScrollPanel.HorizontalScroll.Value = Math.Max(bodyScrollPanel.HorizontalScroll.Value, 1);
597      rowHeaderScrollPanel.VerticalScroll.Value = Math.Max(bodyScrollPanel.VerticalScroll.Value, 1);
598      // minimum 1 is nececary  because of two factors:
599      // - setting the Value-property of Horizontal/VerticalScroll updates the internal state but the Value-property stays 0
600      // - setting the same number of the Value-property has no effect
601      // since the Value-property is always 0, setting it to 0 would have no effect; so it is set to 1 instead
602
603      frameTableLayoutPanel.ResumeRepaint(true);
604    }
605    // add a margin to the header table layouts if the scollbar is visible to account for the width/height of the scrollbar
606    private void UpdateHeaderMargin() {
607      columnHeaderScrollPanel.Margin = new Padding(0, 0, bodyScrollPanel.VerticalScroll.Visible ? SystemInformation.VerticalScrollBarWidth : 0, 0);
608      rowHeaderScrollPanel.Margin = new Padding(0, 0, 0, bodyScrollPanel.HorizontalScroll.Visible ? SystemInformation.HorizontalScrollBarHeight : 0);
609    }
610    #endregion
611
612    #region Sizing of Charts
613    private int oldWidth;
614    private int oldHeight;
615    private float AspectRatio {
616      get {
617        if (oldWidth == 0 || oldHeight == 0) return 1;
618        return (float)oldWidth / oldHeight;
619      }
620    }
621    private bool lockChange = false;
622
623    private int GetColumnWidth() { return (int)widthNumericUpDown.Value; }
624    private int GetRowHeight() { return (int)heightNumericUpDown.Value; }
625
626    private void widthNumericUpDown_ValueChanged(object sender, EventArgs e) {
627      frameTableLayoutPanel.SuspendRepaint();
628      if (lockAspectCheckBox.Checked && !lockChange) {
629        lockChange = true;
630        heightNumericUpDown.Value = (int)((double)widthNumericUpDown.Value / AspectRatio);
631        lockChange = false;
632      }
633      for (int i = 0; i < columnHeaderTableLayoutPanel.ColumnCount; i++) {
634        if (Content.VariableItemList.ItemChecked(Content.VariableItemList[i])) {
635          columnHeaderTableLayoutPanel.ColumnStyles[i].Width = GetColumnWidth();
636          bodyTableLayoutPanel.ColumnStyles[i].Width = GetColumnWidth();
637        }
638      }
639      oldWidth = GetColumnWidth();
640      oldHeight = GetRowHeight();
641      frameTableLayoutPanel.ResumeRepaint(true);
642    }
643    private void heightNumericUpDown_ValueChanged(object sender, EventArgs e) {
644      frameTableLayoutPanel.SuspendRepaint();
645      if (lockAspectCheckBox.Checked && !lockChange) {
646        lockChange = true;
647        widthNumericUpDown.Value = (int)((double)heightNumericUpDown.Value * AspectRatio);
648        lockChange = false;
649      }
650      for (int i = 0; i < rowHeaderTableLayoutPanel.RowCount; i++) {
651        if (Content.VariableItemList.ItemChecked(Content.VariableItemList[i])) {
652          rowHeaderTableLayoutPanel.RowStyles[i].Height = GetRowHeight();
653          bodyTableLayoutPanel.RowStyles[i].Height = GetRowHeight();
654        }
655      }
656      oldWidth = GetColumnWidth();
657      oldHeight = GetRowHeight();
658      frameTableLayoutPanel.ResumeRepaint(true);
659    }
660    private void pointSizeNumericUpDown_ValueChanged(object sender, EventArgs e) {
661      int pointSize = (int)pointSizeNumericUpDown.Value;
662      foreach (var control in bodyCache.ToList()) {
663        var scatterPlotView = control.Value as ScatterPlotView;
664        if (scatterPlotView != null) {
665          foreach (var row in scatterPlotView.Content.Rows) {
666            row.VisualProperties.PointSize = pointSize;
667          }
668        }
669      }
670    }
671    private void pointOpacityNumericUpDown_ValueChanged(object sender, EventArgs e) {
672      float opacity = (float)pointOpacityNumericUpDown.Value;
673      foreach (var control in bodyCache.ToList()) {
674        var scatterPlotView = control.Value as ScatterPlotView;
675        if (scatterPlotView != null) {
676          foreach (var row in scatterPlotView.Content.Rows) {
677            var color = row.VisualProperties.Color;
678            if (color.IsEmpty)
679              color = PreprocessingChartView.Colors.First();
680            row.VisualProperties.Color = Color.FromArgb((int)(opacity * 255), color);
681          }
682        }
683      }
684    }
685    #endregion
686
687    #region Regression Line
688    private void regressionTypeComboBox_SelectedValueChanged(object sender, EventArgs e) {
689      var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
690      polynomialRegressionOrderNumericUpDown.Enabled = regressionType == RegressionType.Polynomial;
691      UpdateRegressionLine();
692    }
693
694    private void polynomialRegressionOrderNumericUpDown_ValueChanged(object sender, EventArgs e) {
695      UpdateRegressionLine();
696    }
697
698    private void UpdateRegressionLine() {
699      var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
700      int order = (int)polynomialRegressionOrderNumericUpDown.Value;
701
702      foreach (var control in bodyCache.ToList()) {
703        // hidden chart => reset cache
704        if (!bodyTableLayoutPanel.Controls.Contains(control.Value)) {
705          bodyCache.Remove(control.Key);
706        }
707
708        var scatterPlotView = control.Value as ScatterPlotView;
709        if (scatterPlotView != null) {
710          foreach (var row in scatterPlotView.Content.Rows) {
711            row.VisualProperties.IsRegressionVisibleInLegend = false;
712            row.VisualProperties.RegressionType = regressionType;
713            row.VisualProperties.PolynomialRegressionOrder = order;
714          }
715        }
716      }
717    }
718    #endregion
719
720    #region Grouping
721    private void groupingComboBox_SelectedIndexChanged(object sender, EventArgs e) {
722      aggregationLabel.Enabled = groupingComboBox.SelectedIndex > 0;
723      aggregationComboBox.Enabled = groupingComboBox.SelectedIndex > 0;
724      legendGroupBox.Enabled = groupingComboBox.SelectedIndex > 0;
725      GenerateCharts(true); // new series within charts -> clear cache
726    }
727
728    private void aggregationComboBox_SelectedIndexChanged(object sender, EventArgs e) {
729      var aggregation = (AggregationType)aggregationComboBox.SelectedValue;
730      foreach (var control in bodyCache.ToList()) {
731        // hidden chart => reset cache
732        if (!bodyTableLayoutPanel.Controls.Contains(control.Value)) {
733          bodyCache.Remove(control.Key);
734        }
735
736        var histogramControl = control.Value as DataTableView;
737        if (histogramControl != null) {
738          foreach (var row in histogramControl.Content.Rows) {
739            row.VisualProperties.Aggregation = aggregation;
740          }
741        }
742      }
743    }
744
745    private void legendCheckbox_CheckedChanged(object sender, EventArgs e) {
746      foreach (var control in bodyCache.ToList()) {
747        var histogramControl = control.Value as DataTableView;
748        if (histogramControl != null) {
749          foreach (var row in histogramControl.Content.Rows) {
750            row.VisualProperties.IsVisibleInLegend = legendCheckbox.Checked && groupingComboBox.SelectedIndex > 0;
751          }
752        }
753        var scatterplotControl = control.Value as ScatterPlotView;
754        if (scatterplotControl != null) {
755          foreach (var row in scatterplotControl.Content.Rows) {
756            row.VisualProperties.IsVisibleInLegend = legendCheckbox.Checked && groupingComboBox.SelectedIndex > 0;
757          }
758        }
759      }
760    }
761
762    private void legendOrderComboBox_SelectedIndexChanged(object sender, EventArgs e) {
763      GenerateCharts(true);
764    }
765    #endregion
766  }
767}
768
Note: See TracBrowser for help on using the repository browser.