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

Last change on this file since 10736 was 10736, checked in by aesterer, 8 years ago

Update chart when data changed

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