Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.3/PreprocessingChartView.cs @ 10817

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