Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingChartView.cs @ 14467

Last change on this file since 14467 was 14459, checked in by pfleck, 7 years ago

#2709

  • Removed the PreprocessingDataTable and PreprocessingDataTableView and use dhe HL DatatTableControl instead.
  • Moved and refactored some code of PreprocessingChart and moved unnecessary code from base classes to actual derivative classes.
File size: 9.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Analysis;
27using HeuristicLab.Analysis.Views;
28using HeuristicLab.Collections;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32
33namespace HeuristicLab.DataPreprocessing.Views {
34  [View("Preprocessing Chart View")]
35  [Content(typeof(PreprocessingChartContent), false)]
36  public partial class PreprocessingChartView : PreprocessingCheckedVariablesView {
37
38    protected Dictionary<string, DataTable> dataTables;
39    protected Dictionary<string, DataTableControl> dataTableControls;
40
41    private const int FIXED_CHART_SIZE = 300;
42    private const int MAX_TABLE_AUTO_SIZE_ROWS = 3;
43
44    protected static readonly Color[] Colors = {
45      Color.FromArgb(59, 136, 239), Color.FromArgb(252, 177, 59), Color.FromArgb(226, 64, 10),
46      Color.FromArgb(5, 100, 146), Color.FromArgb(191, 191, 191), Color.FromArgb(26, 59, 105),
47      Color.FromArgb(255, 226, 126), Color.FromArgb(18, 156, 221), Color.FromArgb(202, 107, 75),
48      Color.FromArgb(0, 92, 219), Color.FromArgb(243, 210, 136), Color.FromArgb(80, 99, 129),
49      Color.FromArgb(241, 185, 168), Color.FromArgb(224, 131, 10), Color.FromArgb(120, 147, 190)
50    };
51
52
53    public PreprocessingChartView() {
54      InitializeComponent();
55      dataTables = new Dictionary<string, DataTable>();
56      dataTableControls= new Dictionary<string, DataTableControl>();
57    }
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      if (Content != null) {
62        InitData();
63        GenerateLayout();
64      }
65    }
66
67    protected virtual int GetNumberOfVisibleDataTables() {
68      return checkedItemList.Content.CheckedItems.Count();
69    }
70
71    protected virtual IEnumerable<DataTableControl> GetVisibleDataTables() {
72      foreach (var name in Content.VariableItemList.CheckedItems) {
73        if (!dataTableControls.ContainsKey(name.Value.Value))
74          dataTableControls.Add(name.Value.Value, new DataTableControl() { Content = dataTables[name.Value.Value] });
75        yield return dataTableControls[name.Value.Value];
76      }
77    }
78
79    protected virtual DataTable CreateDataTable(string variableName) {
80      return null;
81    }
82
83    protected virtual void InitData() {
84      dataTables.Clear();
85      dataTableControls.Clear();
86      foreach (var variable in Content.VariableItemList.Select(v => v.Value)) {
87        dataTables.Add(variable, CreateDataTable(variable));
88      }
89    }
90
91    protected override void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
92      base.CheckedItemsChanged(sender, checkedItems);
93
94      foreach (IndexedItem<StringValue> item in checkedItems.Items) {
95        string variableName = item.Value.Value;
96
97        if (!IsVariableChecked(variableName)) {
98          // not checked -> remove
99          //dataTableView.SetRowEnabled(variableName, false);
100          //dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == variableName)));
101        } else {
102          // checked -> add
103          //DataRow row = GetDataRow(variableName);
104          //dataTableView.SetRowEnabled(variableName, true);
105
106          //var pdt = new DataTable(variableName);
107          //pdt.VisualProperties.Title = string.Empty;
108          //pdt.Rows.Add(row);
109          // dataTablePerVariable does not contain unchecked variables => reduce insert position by number of uncheckt variables to correct the index
110          //int uncheckedUntilVariable = checkedItemList.Content.TakeWhile(x => x.Value != variableName).Count(x => !checkedItemList.Content.ItemChecked(x));
111          //dataTables.Insert(item.Index - uncheckedUntilVariable, pdt);
112        }
113      }
114
115      // update chart if not in all in one mode
116      //if (Content != null && !Content.AllInOneMode)
117      //GenerateChart();??
118      GenerateLayout();
119    }
120
121
122    #region Add/Remove/Update Variable, Reset
123    protected override void AddVariable(string name) {
124      base.AddVariable(name);
125      dataTables.Add(name, CreateDataTable(name));
126
127      GenerateLayout();
128    }
129
130    // remove variable from data table and item list
131    protected override void RemoveVariable(string name) {
132      base.RemoveVariable(name);
133      dataTables.Remove(name);
134      dataTableControls.Remove(name);
135
136      GenerateLayout();
137    }
138
139    protected override void UpdateVariable(string name) {
140      base.UpdateVariable(name);
141      dataTables.Remove(name);
142      var newDataTable = CreateDataTable(name);
143      dataTables.Add(name, newDataTable);
144      dataTableControls[name].Content = newDataTable;
145      GenerateLayout();
146    }
147    protected override void ResetAllVariables() {
148      InitData();
149    }
150    #endregion
151
152    #region Generate Layout
153    protected void GenerateLayout() {
154      tableLayoutPanel.SuspendRepaint();
155
156      ClearTableLayout();
157
158      int nrCharts = GetNumberOfVisibleDataTables();
159
160      // Set columns and rows based on number of items
161      int columns = GetNrOfMultiChartColumns(nrCharts);
162      int rows = GetNrOfMultiChartRows(nrCharts, columns);
163
164      tableLayoutPanel.ColumnCount = columns;
165      tableLayoutPanel.RowCount = rows;
166
167      using (var enumerator = GetVisibleDataTables().GetEnumerator()) {
168        for (int x = 0; x < columns; x++) {
169          var columnStyle = rows <= MAX_TABLE_AUTO_SIZE_ROWS
170            ? new ColumnStyle(SizeType.Percent, 100 / columns)
171            : new ColumnStyle(SizeType.Absolute, (tableLayoutPanel.Width - SystemInformation.VerticalScrollBarWidth) / columns);
172          tableLayoutPanel.ColumnStyles.Add(columnStyle);
173
174          for (int y = 0; y < rows; y++) {
175            // Add a row only when creating the first column
176            if (x == 0) {
177              var rowStyle = rows > MAX_TABLE_AUTO_SIZE_ROWS
178                ? new RowStyle(SizeType.Absolute, FIXED_CHART_SIZE)
179                : new RowStyle(SizeType.Percent, 100 / rows);
180              tableLayoutPanel.RowStyles.Add(rowStyle);
181            }
182
183            if (enumerator.MoveNext())
184              AddDataTableToTableLayout(enumerator.Current, x, y);
185          }
186        }
187      }
188
189      tableLayoutPanel.ResumeRepaint(true);
190    }
191
192    private int GetNrOfMultiChartColumns(int itemCount) {
193      int columns = 0;
194      if (itemCount <= 2)
195        columns = 1;
196      else if (itemCount <= 6)
197        columns = 2;
198      else
199        columns = 3;
200      return columns;
201    }
202    private int GetNrOfMultiChartRows(int itemCount, int columns) {
203      int rows = 0;
204      if (columns == 3)
205        rows = (itemCount + 2) / columns;
206      else if (columns == 2)
207        rows = (itemCount + 1) / columns;
208      else
209        rows = itemCount / columns;
210      return rows;
211    }
212
213    private void AddDataTableToTableLayout(DataTableControl dataTable, int x, int y) {
214      //dataView.Classification = Classification;
215      //dataView.IsDetailedChartViewEnabled = IsDetailedChartViewEnabled;
216
217      if (dataTable == null) {
218        // dummy panel for empty field
219        Panel p = new Panel { Dock = DockStyle.Fill };
220        tableLayoutPanel.Controls.Add(p, y, x);
221      } else {
222        dataTable.Dock = DockStyle.Fill;
223        tableLayoutPanel.Controls.Add(dataTable, y, x);
224      }
225    }
226
227    protected void ClearTableLayout() {
228      //Clear out the existing controls
229      tableLayoutPanel.Controls.Clear();
230
231      //Clear out the existing row and column styles
232      tableLayoutPanel.ColumnStyles.Clear();
233      tableLayoutPanel.RowStyles.Clear();
234
235      tableLayoutPanel.Width = 0;
236      tableLayoutPanel.Height = 0;
237
238      tableLayoutPanel.AutoScroll = false;
239      tableLayoutPanel.AutoScroll = true;
240    }
241    //Remove horizontal scroll bar if visible
242    private void tableLayoutPanel_Layout(object sender, LayoutEventArgs e) {
243      if (tableLayoutPanel.HorizontalScroll.Visible) {
244        // Add padding on the right in order to accomodate the vertical scrollbar
245        int vWidth = SystemInformation.VerticalScrollBarWidth;
246        tableLayoutPanel.Padding = new Padding(0, 0, vWidth, 0);
247      } else {
248        // Reset padding
249        tableLayoutPanel.Padding = new Padding(0);
250      }
251    }
252    #endregion
253  }
254}
Note: See TracBrowser for help on using the repository browser.