Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added chart per variable feature to preprocessing chart view

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