Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10952 was 10952, checked in by aesterer, 10 years ago

Completed scatter plot view

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