Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingChartView.cs @ 14725

Last change on this file since 14725 was 14725, checked in by mkommend, 7 years ago

#2709: Added grouping for multi scatter plot view.

File size: 9.3 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;
33
34namespace HeuristicLab.DataPreprocessing.Views {
35  [View("Preprocessing Chart View")]
36  [Content(typeof(PreprocessingChartContent), false)]
37  public partial class PreprocessingChartView : PreprocessingCheckedVariablesView { 
38    protected Dictionary<string, DataTable> dataTables;
39    protected Dictionary<string, DataTableControl> dataTableControls;
40
41    private const int FIXED_CHART_SIZE = 300;
42    private const int MAX_TABLE_AUTO_SIZE_ROWS = 3;
43
44    protected static readonly Color[] Colors = {
45      Color.FromArgb(59, 136, 239), Color.FromArgb(252, 177, 59), Color.FromArgb(226, 64, 10),
46      Color.FromArgb(5, 100, 146), Color.FromArgb(191, 191, 191), Color.FromArgb(26, 59, 105),
47      Color.FromArgb(255, 226, 126), Color.FromArgb(18, 156, 221), Color.FromArgb(202, 107, 75),
48      Color.FromArgb(0, 92, 219), Color.FromArgb(243, 210, 136), Color.FromArgb(80, 99, 129),
49      Color.FromArgb(241, 185, 168), Color.FromArgb(224, 131, 10), Color.FromArgb(120, 147, 190)
50    };
51
52
53    public PreprocessingChartView() {
54      InitializeComponent();
55      dataTables = new Dictionary<string, DataTable>();
56      dataTableControls = new Dictionary<string, DataTableControl>();
57    }
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      if (Content != null) {
62        InitData();
63        GenerateLayout();
64      }
65    }
66
67    protected virtual int GetNumberOfVisibleDataTables() {
68      return Content.VariableItemList.CheckedItems.Count();
69    }
70
71    protected virtual IEnumerable<DataTableControl> GetVisibleDataTables() {
72      foreach (var name in Content.VariableItemList.CheckedItems) {
73        if (!dataTableControls.ContainsKey(name.Value.Value))
74          dataTableControls.Add(name.Value.Value, new DataTableControl() { Content = dataTables[name.Value.Value] });
75        yield return dataTableControls[name.Value.Value];
76      }
77    }
78
79    protected virtual DataTable CreateDataTable(string variableName) {
80      return null;
81    }
82
83    protected virtual void InitData() {
84      dataTables.Clear();
85      dataTableControls.Clear();
86      foreach (var variable in Content.VariableItemList.Select(v => v.Value)) {
87        dataTables.Add(variable, CreateDataTable(variable));
88      }
89    }
90
91    protected override void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
92      base.CheckedItemsChanged(sender, checkedItems);
93
94      foreach (IndexedItem<StringValue> item in checkedItems.Items) {
95        string variableName = item.Value.Value;
96
97        if (!IsVariableChecked(variableName)) {
98          // not checked -> remove
99          //dataTableView.SetRowEnabled(variableName, false);
100          //dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == variableName)));
101        } else {
102          // checked -> add
103          //DataRow row = GetDataRow(variableName);
104          //dataTableView.SetRowEnabled(variableName, true);
105
106          //var pdt = new DataTable(variableName);
107          //pdt.VisualProperties.Title = string.Empty;
108          //pdt.Rows.Add(row);
109          // dataTablePerVariable does not contain unchecked variables => reduce insert position by number of uncheckt variables to correct the index
110          //int uncheckedUntilVariable = checkedItemList.Content.TakeWhile(x => x.Value != variableName).Count(x => !checkedItemList.Content.ItemChecked(x));
111          //dataTables.Insert(item.Index - uncheckedUntilVariable, pdt);
112        }
113      }
114
115      // update chart if not in all in one mode
116      //if (Content != null && !Content.AllInOneMode)
117      //GenerateChart();??
118      GenerateLayout();
119    }
120
121
122    #region Add/Remove/Update Variable, Reset
123    protected override void AddVariable(string name) {
124      base.AddVariable(name);
125      dataTables.Add(name, CreateDataTable(name));
126
127      GenerateLayout();
128    }
129
130    // remove variable from data table and item list
131    protected override void RemoveVariable(string name) {
132      base.RemoveVariable(name);
133      dataTables.Remove(name);
134      dataTableControls.Remove(name);
135
136      GenerateLayout();
137    }
138
139    protected override void UpdateVariable(string name) {
140      base.UpdateVariable(name);
141      dataTables.Remove(name);
142      var newDataTable = CreateDataTable(name);
143      dataTables.Add(name, newDataTable);
144      dataTableControls[name].Content = newDataTable;
145      GenerateLayout();
146    }
147    protected override void ResetAllVariables() {
148      InitData();
149    }
150    #endregion
151
152    protected override void CheckedChangedUpdate() {
153      GenerateLayout();
154    }
155
156    #region Generate Layout
157    protected void GenerateLayout() {
158      if (suppressCheckedChangedUpdate)
159        return;
160
161      tableLayoutPanel.SuspendRepaint();
162
163      ClearTableLayout();
164
165      int nrCharts = GetNumberOfVisibleDataTables();
166
167      // Set columns and rows based on number of items
168      int columns = GetNrOfMultiChartColumns(nrCharts);
169      int rows = GetNrOfMultiChartRows(nrCharts, columns);
170
171      tableLayoutPanel.ColumnCount = columns;
172      tableLayoutPanel.RowCount = rows;
173
174      using (var enumerator = GetVisibleDataTables().GetEnumerator()) {
175        for (int x = 0; x < columns; x++) {
176          var columnStyle = rows <= MAX_TABLE_AUTO_SIZE_ROWS
177            ? new ColumnStyle(SizeType.Percent, 100 / columns)
178            : new ColumnStyle(SizeType.Absolute, (tableLayoutPanel.Width - SystemInformation.VerticalScrollBarWidth) / columns);
179          tableLayoutPanel.ColumnStyles.Add(columnStyle);
180
181          for (int y = 0; y < rows; y++) {
182            // Add a row only when creating the first column
183            if (x == 0) {
184              var rowStyle = rows > MAX_TABLE_AUTO_SIZE_ROWS
185                ? new RowStyle(SizeType.Absolute, FIXED_CHART_SIZE)
186                : new RowStyle(SizeType.Percent, 100 / rows);
187              tableLayoutPanel.RowStyles.Add(rowStyle);
188            }
189
190            if (enumerator.MoveNext())
191              AddDataTableToTableLayout(enumerator.Current, x, y);
192          }
193        }
194      }
195
196      tableLayoutPanel.ResumeRepaint(true);
197    }
198
199    private int GetNrOfMultiChartColumns(int itemCount) {
200      int columns = 0;
201      if (itemCount <= 2)
202        columns = 1;
203      else if (itemCount <= 6)
204        columns = 2;
205      else
206        columns = 3;
207      return columns;
208    }
209    private int GetNrOfMultiChartRows(int itemCount, int columns) {
210      int rows = 0;
211      if (columns == 3)
212        rows = (itemCount + 2) / columns;
213      else if (columns == 2)
214        rows = (itemCount + 1) / columns;
215      else
216        rows = itemCount / columns;
217      return rows;
218    }
219
220    private void AddDataTableToTableLayout(DataTableControl dataTable, int x, int y) {
221      //dataView.Classification = Classification;
222      //dataView.IsDetailedChartViewEnabled = IsDetailedChartViewEnabled;
223
224      if (dataTable == null) {
225        // dummy panel for empty field
226        Panel p = new Panel { Dock = DockStyle.Fill };
227        tableLayoutPanel.Controls.Add(p, y, x);
228      } else {
229        dataTable.Dock = DockStyle.Fill;
230        tableLayoutPanel.Controls.Add(dataTable, y, x);
231      }
232    }
233
234    protected void ClearTableLayout() {
235      //Clear out the existing controls
236      tableLayoutPanel.Controls.Clear();
237
238      //Clear out the existing row and column styles
239      tableLayoutPanel.ColumnStyles.Clear();
240      tableLayoutPanel.RowStyles.Clear();
241
242      tableLayoutPanel.Width = 0;
243      tableLayoutPanel.Height = 0;
244
245      tableLayoutPanel.AutoScroll = false;
246      tableLayoutPanel.AutoScroll = true;
247    }
248    //Remove horizontal scroll bar if visible
249    private void tableLayoutPanel_Layout(object sender, LayoutEventArgs e) {
250      if (tableLayoutPanel.HorizontalScroll.Visible) {
251        // Add padding on the right in order to accomodate the vertical scrollbar
252        int vWidth = SystemInformation.VerticalScrollBarWidth;
253        tableLayoutPanel.Padding = new Padding(0, 0, vWidth, 0);
254      } else {
255        // Reset padding
256        tableLayoutPanel.Padding = new Padding(0);
257      }
258    }
259    #endregion
260  }
261}
Note: See TracBrowser for help on using the repository browser.