Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10992 was 10992, checked in by rstoll, 10 years ago
  • removed ChartLogic and

moved logic accordingly to PreprocessingChartContent, ScatterPlotContent
modified views etc. to use IFilteredPreprocessingData instead of ChartLogic

  • reordered code
File size: 13.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Windows.Forms;
25using HeuristicLab.Analysis;
26using HeuristicLab.Collections;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.DataPreprocessing.Implementations;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.DataPreprocessing.Views {
33
34  [View("Preprocessing Chart View")]
35  [Content(typeof(PreprocessingChartContent), false)]
36  public partial class PreprocessingChartView : ItemView {
37
38    private PreprocessingDataTable dataTable;
39    private List<PreprocessingDataTable> dataTablePerVariable;
40    private List<DataRow> dataRows;
41    private List<DataRow> selectedDataRows;
42
43    protected DataRowVisualProperties.DataRowChartType chartType;
44    protected string chartTitle;
45
46    private const string DEFAULT_CHART_TITLE = "Chart";
47    private const int FIXED_CHART_SIZE = 300;
48    private const int MAX_TABLE_ROWS = 3;
49
50    public IEnumerable<double> Classification { get; set; }
51
52    public PreprocessingChartView() {
53      InitializeComponent();
54      chartType = DataRowVisualProperties.DataRowChartType.Line;
55      chartTitle = DEFAULT_CHART_TITLE;
56    }
57
58    //Variable selection changed
59    //Add or remove data row
60    private void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
61      foreach (IndexedItem<StringValue> item in checkedItems.Items) {
62        string variableName = item.Value.Value;
63
64        //variable is displayed -> remove
65        if (VariableIsDisplayed(variableName)) {
66          dataTable.Rows.Remove(variableName);
67          dataTable.SelectedRows.Remove(variableName);
68          dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == variableName)));
69          //variable isnt't displayed -> add
70        } else {
71          DataRow row = GetDataRow(variableName);
72          DataRow selectedRow = GetSelectedDataRow(variableName);
73          dataTable.Rows.Add(row);
74
75          PreprocessingDataTable pdt = new PreprocessingDataTable(variableName);
76          pdt.Rows.Add(row);
77          dataTablePerVariable.Add(pdt);
78
79          //update selection
80          if (selectedRow != null) {
81            dataTable.SelectedRows.Add(selectedRow);
82            pdt.SelectedRows.Add(selectedRow);
83          }
84        }
85      }
86
87      // update chart if not in all in one mode
88      if (Content != null && !Content.AllInOneMode)
89        GenerateChart();
90
91    }
92
93    private bool VariableIsDisplayed(string name) {
94
95      foreach (var item in dataTable.Rows) {
96        if (item.Name == name)
97          return true;
98      }
99      return false;
100    }
101
102    protected override void RegisterContentEvents() {
103      base.RegisterContentEvents();
104      Content.PreprocessingData.Changed += PreprocessingData_Changed;
105      Content.PreprocessingData.SelectionChanged += PreprocessingData_SelctionChanged;
106
107    }
108
109    protected override void DeregisterContentEvents() {
110      base.DeregisterContentEvents();
111      Content.PreprocessingData.Changed -= PreprocessingData_Changed;
112      Content.PreprocessingData.SelectionChanged -= PreprocessingData_SelctionChanged;
113    }
114
115    public new PreprocessingChartContent Content {
116      get { return (PreprocessingChartContent)base.Content; }
117      set { base.Content = value; }
118    }
119
120    private void InitData() {
121      if (Content.VariableItemList == null) {
122        Content.VariableItemList = Content.CreateVariableItemList();
123      }
124      checkedItemList.Content = Content.VariableItemList;
125
126      //Create data tables and data rows
127      dataRows = Content.CreateAllDataRows(chartType);
128      dataTable = new PreprocessingDataTable(chartTitle);
129      dataTablePerVariable = new List<PreprocessingDataTable>();
130
131      //add data rows to data tables according to checked item list
132      foreach (var checkedItem in Content.VariableItemList.CheckedItems) {
133        string variableName = Content.VariableItemList[checkedItem.Index].Value;
134        PreprocessingDataTable d = new PreprocessingDataTable(variableName);
135        DataRow row = GetDataRow(variableName);
136
137        //add row to data table
138        dataTable.Rows.Add(row);
139
140        //add row to data table per variable
141        d.Rows.Add(row);
142        dataTablePerVariable.Add(d);
143      }
144
145      UpdateSelection();
146    }
147
148    private void UpdateSelection() {
149
150      //update data table selection
151      selectedDataRows = Content.CreateAllSelectedDataRows(chartType);
152      dataTable.SelectedRows.Clear();
153      dataTable.SelectedRows.AddRange(selectedDataRows);
154
155      //update data table per variable selection
156      foreach (PreprocessingDataTable d in dataTablePerVariable) {
157        d.SelectedRows.Clear();
158        DataRow row = selectedDataRows.Find(x => x.Name == d.Name);
159        if (row != null)
160          d.SelectedRows.Add(row);
161      }
162
163    }
164
165    private DataRow GetSelectedDataRow(string variableName) {
166      foreach (DataRow row in selectedDataRows) {
167        if (row.Name == variableName)
168          return row;
169      }
170      return null;
171    }
172
173    private DataRow GetDataRow(string variableName) {
174      foreach (DataRow row in dataRows) {
175        if (row.Name == variableName)
176          return row;
177      }
178      return null;
179    }
180
181    protected override void OnContentChanged() {
182      base.OnContentChanged();
183      if (Content != null) {
184        InitData();
185        Content.VariableItemList.CheckedItemsChanged += CheckedItemsChanged;
186        GenerateChart();
187      }
188    }
189
190    // TODO : handle also other changed events
191    void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) {
192      switch (e.Type) {
193        case DataPreprocessingChangedEventType.DeleteColumn:
194          RemoveVariable(Content.PreprocessingData.GetVariableName(e.Column));
195          break;
196        case DataPreprocessingChangedEventType.AddColumn:
197          AddVariable(Content.PreprocessingData.GetVariableName(e.Column));
198          break;
199        case DataPreprocessingChangedEventType.ChangeColumn:
200        case DataPreprocessingChangedEventType.ChangeItem:
201          UpdateDataForVariable(Content.PreprocessingData.GetVariableName(e.Column));
202          break;
203        case DataPreprocessingChangedEventType.DeleteRow:
204        case DataPreprocessingChangedEventType.AddRow:
205        case DataPreprocessingChangedEventType.Any:
206        default:
207          //TODO: test with transform
208          InitData();
209          GenerateChart();
210          break;
211      }
212    }
213
214    private void PreprocessingData_SelctionChanged(object sender, EventArgs e) {
215      UpdateSelection();
216    }
217
218    private void UpdateDataForVariable(string variableName) {
219      DataRow newRow = Content.CreateDataRow(variableName, chartType);
220      dataTable.Rows.Remove(variableName);
221      dataTable.Rows.Add(newRow);
222      DataTable dt = dataTablePerVariable.Find(x => x.Rows.Find(y => y.Name == variableName) != null);
223      if (dt != null) {
224        dt.Rows.Remove(variableName);
225        dt.Rows.Add(newRow);
226      }
227    }
228
229    // add variable to data table and item list
230    private void AddVariable(string name) {
231      DataRow row = Content.CreateDataRow(name, chartType);
232      dataTable.Rows.Add(row);
233      PreprocessingDataTable d = new PreprocessingDataTable(name);
234      d.Rows.Add(row);
235      dataTablePerVariable.Add(d);
236      Content.VariableItemList.Add(new StringValue(name));
237      if (!Content.AllInOneMode)
238        GenerateChart();
239    }
240
241    // remove variable from data table and item list
242    private void RemoveVariable(string name) {
243      dataTable.Rows.Remove(name);
244      dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == name)));
245
246      StringValue stringValue = FindVariableItemList(name);
247      if (stringValue != null)
248        Content.VariableItemList.Remove(stringValue);
249      if (!Content.AllInOneMode)
250        GenerateChart();
251    }
252
253    private StringValue FindVariableItemList(string name) {
254      foreach (StringValue stringValue in Content.VariableItemList) {
255        if (stringValue.Value == name)
256          return stringValue;
257      }
258      return null;
259    }
260
261    protected void GenerateChart() {
262
263      ClearTableLayout();
264      if (Content.AllInOneMode) {
265        GenerateSingleChartLayout();
266      } else
267        GenerateMultiChartLayout();
268    }
269
270    private void ClearTableLayout() {
271      //Clear out the existing controls
272      tableLayoutPanel.Controls.Clear();
273
274      //Clear out the existing row and column styles
275      tableLayoutPanel.ColumnStyles.Clear();
276      tableLayoutPanel.RowStyles.Clear();
277      tableLayoutPanel.AutoScroll = false;
278      tableLayoutPanel.AutoScroll = true;
279    }
280
281    private void GenerateSingleChartLayout() {
282      tableLayoutPanel.ColumnCount = 1;
283      tableLayoutPanel.RowCount = 1;
284      tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
285      tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
286      tableLayoutPanel.Controls.Add(dataTableView, 0, 0);
287      dataTableView.Content = dataTable;
288    }
289
290    private int GetNrOfMultiChartColumns(int itemCount) {
291      int columns = 0;
292      if (itemCount <= 2)
293        columns = 1;
294      else if (itemCount <= 6)
295        columns = 2;
296      else
297        columns = 3;
298      return columns;
299    }
300
301    private int GetNrOfMultiChartRows(int itemCount, int columns) {
302      int rows = 0;
303      if (columns == 3)
304        rows = (itemCount + 2) / columns;
305      else if (columns == 2)
306        rows = (itemCount + 1) / columns;
307      else
308        rows = itemCount / columns;
309      return rows;
310    }
311
312
313    private void GenerateMultiChartLayout() {
314      int checkedItemsCnt = 0;
315      foreach (var item in Content.VariableItemList.CheckedItems)
316        checkedItemsCnt++;
317
318      // set columns and rows based on number of items
319      int columns = GetNrOfMultiChartColumns(checkedItemsCnt);
320      int rows = GetNrOfMultiChartRows(checkedItemsCnt, columns);
321
322      tableLayoutPanel.ColumnCount = columns;
323      tableLayoutPanel.RowCount = rows;
324
325      List<PreprocessingDataTable>.Enumerator enumerator = dataTablePerVariable.GetEnumerator();
326      for (int x = 0; x < columns; x++) {
327
328        if (rows <= MAX_TABLE_ROWS)
329          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100 / columns));
330        else
331          //scrollbar is shown if there are more than 3 rows -> remove scroll bar width from total width
332          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, (tableLayoutPanel.Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth) / columns));
333        for (int y = 0; y < rows; y++) {
334          //Add a row only when creating the first column
335          if (x == 0) {
336            // fixed chart size when there are more than 3 tables
337            if (rows > MAX_TABLE_ROWS)
338              tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, FIXED_CHART_SIZE));
339            else
340              tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100 / rows));
341          }
342
343          enumerator.MoveNext();
344          PreprocessingDataTable d = enumerator.Current;
345          AddDataTableToTableLayout(d, x, y);
346
347        }
348      }
349    }
350
351    private void AddDataTableToTableLayout(PreprocessingDataTable dataTable, int x, int y) {
352      PreprocessingDataTableView dataView = new PreprocessingDataTableView();
353      dataView.Classification = Classification;
354
355      if (dataTable == null) {
356        // dummy panel for empty field
357        Panel p = new Panel();
358        p.Dock = DockStyle.Fill;
359        tableLayoutPanel.Controls.Add(p, y, x);
360      } else {
361        dataView.Content = dataTable;
362        dataView.Dock = DockStyle.Fill;
363        tableLayoutPanel.Controls.Add(dataView, y, x);
364      }
365    }
366
367    //Remove horizontal scroll bar if visible
368    private void tableLayoutPanel_Layout(object sender, LayoutEventArgs e) {
369      if (tableLayoutPanel.HorizontalScroll.Visible) {
370        // Add padding on the right in order to accomodate the vertical scrollbar
371        int vWidth = SystemInformation.VerticalScrollBarWidth;
372        tableLayoutPanel.Padding = new Padding(0, 0, vWidth, 0);
373      } else {
374        // Reset padding
375        tableLayoutPanel.Padding = new Padding(0);
376      }
377    }
378
379  }
380}
381
382
Note: See TracBrowser for help on using the repository browser.