Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10867 was 10867, checked in by mleitner, 10 years ago

Add colors to histogram / remove all in one mode

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