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