Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10818 was 10818, checked in by psteiner, 10 years ago

Persistence of charting congfiguration

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