Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2698 Only input and target variables are pre-checked for linechart, histrogram and scatterplot.

File size: 11.7 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 System.Windows.Forms;
26using HeuristicLab.Analysis;
27using HeuristicLab.Collections;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.DataPreprocessing.Views {
32  [View("Preprocessing Chart View")]
33  [Content(typeof(PreprocessingChartContent), false)]
34  public partial class PreprocessingChartView : PreprocessingCheckedVariablesView {
35
36    protected PreprocessingDataTable dataTable;
37    protected List<PreprocessingDataTable> dataTablePerVariable;
38    protected List<DataRow> dataRows;
39    protected List<DataRow> selectedDataRows;
40
41    protected DataRowVisualProperties.DataRowChartType chartType;
42    protected string chartTitle;
43
44    private const string DEFAULT_CHART_TITLE = "Chart";
45    private const int FIXED_CHART_SIZE = 300;
46    private const int MAX_TABLE_AUTO_SIZE_ROWS = 3;
47
48
49    public IEnumerable<double> Classification { get; set; }
50    public bool IsDetailedChartViewEnabled { get; set; }
51
52    public PreprocessingChartView() {
53      InitializeComponent();
54      chartType = DataRowVisualProperties.DataRowChartType.Line;
55      chartTitle = DEFAULT_CHART_TITLE;
56    }
57
58    protected override void OnContentChanged() {
59      base.OnContentChanged();
60      if (Content != null) {
61        InitData();
62        GenerateChart();
63
64        foreach (var row in dataRows) {
65          string variableName = row.Name;
66          if (!IsVariableChecked(variableName)) {
67            dataTableView.SetRowEnabled(variableName, false);
68            dataTable.SelectedRows.Remove(variableName);
69            dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == variableName)));
70          }
71        }
72      }
73    }
74
75    private void InitData() {
76      //Create data tables and data rows
77      dataRows = Content.CreateAllDataRows(chartType);
78      dataTable = new PreprocessingDataTable(chartTitle);
79      dataTablePerVariable = new List<PreprocessingDataTable>();
80
81      //add data rows to data tables according to checked item list
82      foreach (var row in dataRows) {
83        string variableName = row.Name;
84
85        //add row to data table
86        dataTable.Rows.Add(row);
87
88        //add row to data table per variable
89        PreprocessingDataTable d = new PreprocessingDataTable(variableName);
90        d.Rows.Add(row);
91        dataTablePerVariable.Add(d);
92      }
93
94      UpdateSelection();
95    }
96
97    protected override void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
98      base.CheckedItemsChanged(sender, checkedItems);
99
100      foreach (IndexedItem<StringValue> item in checkedItems.Items) {
101        string variableName = item.Value.Value;
102
103
104        if (!IsVariableChecked(variableName)) {
105          // not checked -> remove
106          dataTableView.SetRowEnabled(variableName, false);
107          dataTable.SelectedRows.Remove(variableName);
108          dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == variableName)));
109        } else {
110          // checked -> add
111          DataRow row = GetDataRow(variableName);
112          DataRow selectedRow = GetSelectedDataRow(variableName);
113          dataTableView.SetRowEnabled(variableName, true);
114
115          PreprocessingDataTable pdt = new PreprocessingDataTable(variableName);
116          pdt.Rows.Add(row);
117          // dataTablePerVariable does not contain unchecked variables => reduce insert position by number of uncheckt variables to correct the index
118          int uncheckedUntilVariable = checkedItemList.Content.TakeWhile(x => x.Value != variableName).Count(x => !checkedItemList.Content.ItemChecked(x));
119          dataTablePerVariable.Insert(item.Index - uncheckedUntilVariable, pdt);
120
121          //update selection
122          if (selectedRow != null) {
123            dataTable.SelectedRows.Add(selectedRow);
124            pdt.SelectedRows.Add(selectedRow);
125          }
126        }
127      }
128
129      // update chart if not in all in one mode
130      if (Content != null && !Content.AllInOneMode)
131        GenerateChart();
132    }
133
134    private DataRow GetSelectedDataRow(string variableName) {
135      foreach (DataRow row in selectedDataRows) {
136        if (row.Name == variableName)
137          return row;
138      }
139      return null;
140    }
141    private DataRow GetDataRow(string variableName) {
142      foreach (DataRow row in dataRows) {
143        if (row.Name == variableName)
144          return row;
145      }
146      return null;
147    }
148
149    #region Add/Remove/Update Variable, Reset
150    protected override void AddVariable(string name) {
151      base.AddVariable(name);
152      DataRow row = Content.CreateDataRow(name, chartType);
153      dataTable.Rows.Add(row);
154      PreprocessingDataTable d = new PreprocessingDataTable(name);
155      d.Rows.Add(row);
156      dataTablePerVariable.Add(d);
157
158      if (!Content.AllInOneMode)
159        GenerateChart();
160    }
161
162    // remove variable from data table and item list
163    protected override void RemoveVariable(string name) {
164      base.RemoveVariable(name);
165      dataTable.Rows.Remove(name);
166      dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == name)));
167
168      if (!Content.AllInOneMode)
169        GenerateChart();
170    }
171
172    protected override void UpdateVariable(string name) {
173      base.UpdateVariable(name);
174      DataRow newRow = Content.CreateDataRow(name, chartType);
175      dataTable.Rows.Remove(name);
176      dataTable.Rows.Add(newRow);
177      DataTable dt = dataTablePerVariable.Find(x => x.Rows.Find(y => y.Name == name) != null);
178      if (dt != null) {
179        dt.Rows.Remove(name);
180        dt.Rows.Add(newRow);
181      }
182    }
183    protected override void ResetAllVariables() {
184      InitData();
185    }
186    #endregion
187
188    #region Generate Charts
189    protected void GenerateChart() {
190      ClearTableLayout();
191      if (Content.AllInOneMode) {
192        GenerateSingleChartLayout();
193      } else
194        GenerateMultiChartLayout();
195    }
196
197    private void GenerateSingleChartLayout() {
198      tableLayoutPanel.ColumnCount = 1;
199      tableLayoutPanel.RowCount = 1;
200      tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
201      tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
202      tableLayoutPanel.Controls.Add(dataTableView, 0, 0);
203      dataTableView.Content = dataTable;
204    }
205
206    private void GenerateMultiChartLayout() {
207      int checkedItemsCnt = 0;
208      foreach (var item in Content.VariableItemList.CheckedItems)
209        checkedItemsCnt++;
210
211      // set columns and rows based on number of items
212      int columns = GetNrOfMultiChartColumns(checkedItemsCnt);
213      int rows = GetNrOfMultiChartRows(checkedItemsCnt, columns);
214
215      tableLayoutPanel.ColumnCount = columns;
216      tableLayoutPanel.RowCount = rows;
217
218      List<PreprocessingDataTable>.Enumerator enumerator = dataTablePerVariable.GetEnumerator();
219      for (int x = 0; x < columns; x++) {
220
221        if (rows <= MAX_TABLE_AUTO_SIZE_ROWS)
222          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100 / columns));
223        else
224          //scrollbar is shown if there are more than 3 rows -> remove scroll bar width from total width
225          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, (tableLayoutPanel.Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth) / columns));
226        for (int y = 0; y < rows; y++) {
227          //Add a row only when creating the first column
228          if (x == 0) {
229            // fixed chart size when there are more than 3 tables
230            if (rows > MAX_TABLE_AUTO_SIZE_ROWS)
231              tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, FIXED_CHART_SIZE));
232            else
233              tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100 / rows));
234          }
235
236          enumerator.MoveNext();
237          PreprocessingDataTable d = enumerator.Current;
238          AddDataTableToTableLayout(d, x, y);
239
240        }
241      }
242    }
243    private int GetNrOfMultiChartColumns(int itemCount) {
244      int columns = 0;
245      if (itemCount <= 2)
246        columns = 1;
247      else if (itemCount <= 6)
248        columns = 2;
249      else
250        columns = 3;
251      return columns;
252    }
253    private int GetNrOfMultiChartRows(int itemCount, int columns) {
254      int rows = 0;
255      if (columns == 3)
256        rows = (itemCount + 2) / columns;
257      else if (columns == 2)
258        rows = (itemCount + 1) / columns;
259      else
260        rows = itemCount / columns;
261      return rows;
262    }
263
264    private void AddDataTableToTableLayout(PreprocessingDataTable dataTable, int x, int y) {
265      PreprocessingDataTableView dataView = new PreprocessingDataTableView();
266      dataView.Classification = Classification;
267      dataView.IsDetailedChartViewEnabled = IsDetailedChartViewEnabled;
268
269      if (dataTable == null) {
270        // dummy panel for empty field
271        Panel p = new Panel();
272        p.Dock = DockStyle.Fill;
273        tableLayoutPanel.Controls.Add(p, y, x);
274      } else {
275        dataView.Content = dataTable;
276        dataView.Dock = DockStyle.Fill;
277        tableLayoutPanel.Controls.Add(dataView, y, x);
278      }
279    }
280
281    protected void ClearTableLayout() {
282      //Clear out the existing controls
283      tableLayoutPanel.Controls.Clear();
284
285      //Clear out the existing row and column styles
286      tableLayoutPanel.ColumnStyles.Clear();
287      tableLayoutPanel.RowStyles.Clear();
288      tableLayoutPanel.AutoScroll = false;
289      tableLayoutPanel.AutoScroll = true;
290    }
291    //Remove horizontal scroll bar if visible
292    private void tableLayoutPanel_Layout(object sender, LayoutEventArgs e) {
293      if (tableLayoutPanel.HorizontalScroll.Visible) {
294        // Add padding on the right in order to accomodate the vertical scrollbar
295        int vWidth = SystemInformation.VerticalScrollBarWidth;
296        tableLayoutPanel.Padding = new Padding(0, 0, vWidth, 0);
297      } else {
298        // Reset padding
299        tableLayoutPanel.Padding = new Padding(0);
300      }
301    }
302    #endregion
303
304    #region Update Selection
305    protected override void PreprocessingData_SelctionChanged(object sender, EventArgs e) {
306      base.PreprocessingData_SelctionChanged(sender, e);
307      UpdateSelection();
308    }
309
310    private void UpdateSelection() {
311      //update data table selection
312      selectedDataRows = Content.CreateAllSelectedDataRows(chartType);
313      dataTable.SelectedRows.Clear();
314      foreach (var selectedRow in selectedDataRows) {
315        if (IsVariableChecked(selectedRow.Name))
316          dataTable.SelectedRows.Add(selectedRow);
317      }
318
319      //update data table per variable selection
320      foreach (PreprocessingDataTable d in dataTablePerVariable) {
321        d.SelectedRows.Clear();
322        DataRow row = selectedDataRows.Find(x => x.Name == d.Name);
323        if (row != null)
324          d.SelectedRows.Add(row);
325      }
326    }
327    #endregion
328  }
329}
Note: See TracBrowser for help on using the repository browser.