Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingChartView.cs @ 17234

Last change on this file since 17234 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Analysis;
28using HeuristicLab.Analysis.Views;
29using HeuristicLab.Collections;
30using HeuristicLab.Data;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33
34namespace HeuristicLab.DataPreprocessing.Views {
35  [View("Preprocessing Chart View")]
36  [Content(typeof(PreprocessingChartContent), false)]
37  public partial class PreprocessingChartView : PreprocessingCheckedVariablesView {
38    protected Dictionary<string, DataTable> dataTables;
39    protected Dictionary<string, DataTableView> dataTableViews;
40
41    public static readonly Color[] Colors = {
42      Color.FromArgb(59, 136, 239), Color.FromArgb(252, 177, 59), Color.FromArgb(226, 64, 10),
43      Color.FromArgb(5, 100, 146), Color.FromArgb(191, 191, 191), Color.FromArgb(26, 59, 105),
44      Color.FromArgb(255, 226, 126), Color.FromArgb(18, 156, 221), Color.FromArgb(202, 107, 75),
45      Color.FromArgb(0, 92, 219), Color.FromArgb(243, 210, 136), Color.FromArgb(80, 99, 129),
46      Color.FromArgb(241, 185, 168), Color.FromArgb(224, 131, 10), Color.FromArgb(120, 147, 190)
47    };
48
49    public PreprocessingChartView() {
50      InitializeComponent();
51      dataTables = new Dictionary<string, DataTable>();
52      dataTableViews = new Dictionary<string, DataTableView>();
53      scrollPanel.HorizontalScroll.Visible = false;
54    }
55
56    protected override void OnContentChanged() {
57      base.OnContentChanged();
58      if (Content != null) {
59        InitData();
60        GenerateLayout();
61      }
62    }
63
64    protected virtual int GetNumberOfVisibleDataTables() {
65      return Content.VariableItemList.CheckedItems.Count();
66    }
67
68    protected virtual IEnumerable<DataTableView> GetVisibleDataTables() {
69      foreach (var name in Content.VariableItemList.CheckedItems) {
70        if (!dataTableViews.ContainsKey(name.Value.Value))
71          dataTableViews.Add(name.Value.Value, new DataTableView() { Content = dataTables[name.Value.Value], ShowChartOnly = true });
72        yield return dataTableViews[name.Value.Value];
73      }
74    }
75
76    protected virtual DataTable CreateDataTable(string variableName) {
77      return null;
78    }
79
80    protected virtual void InitData() {
81      dataTables.Clear();
82      dataTableViews.Clear();
83      foreach (var variable in Content.VariableItemList.Select(v => v.Value)) {
84        dataTables.Add(variable, CreateDataTable(variable));
85      }
86    }
87
88    protected override void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
89      base.CheckedItemsChanged(sender, checkedItems);
90
91      GenerateLayout();
92    }
93
94
95    #region Add/Remove/Update Variable, Reset
96    protected override void AddVariable(string name) {
97      base.AddVariable(name);
98      dataTables.Add(name, CreateDataTable(name));
99
100      GenerateLayout();
101    }
102
103    // remove variable from data table and item list
104    protected override void RemoveVariable(string name) {
105      base.RemoveVariable(name);
106      dataTables.Remove(name);
107      dataTableViews.Remove(name);
108
109      GenerateLayout();
110    }
111
112    protected override void UpdateVariable(string name) {
113      base.UpdateVariable(name);
114      dataTables.Remove(name);
115      var newDataTable = CreateDataTable(name);
116      dataTables.Add(name, newDataTable);
117      dataTableViews[name].Content = newDataTable;
118      GenerateLayout();
119    }
120    protected override void ResetAllVariables() {
121      InitData();
122    }
123    #endregion
124
125    protected override void CheckedChangedUpdate() {
126      GenerateLayout();
127    }
128
129    #region Generate Layout
130    protected void GenerateLayout() {
131      if (SuppressCheckedChangedUpdate)
132        return;
133
134      scrollPanel.SuspendRepaint();
135
136      ClearTableLayout();
137
138      int nrCharts = GetNumberOfVisibleDataTables();
139
140      // Set columns and rows based on number of items
141      int columns = Math.Min(nrCharts, (int)columnsNumericUpDown.Value);
142      int rows = (int)Math.Ceiling((float)nrCharts / columns);
143
144      tableLayoutPanel.ColumnCount = Math.Max(columns, 0);
145      tableLayoutPanel.RowCount = Math.Max(rows, 0);
146
147      if (columns > 0 && rows > 0) {
148        var width = (splitContainer.Panel2.Width - SystemInformation.VerticalScrollBarWidth) / columns;
149        var height = width * 0.75f;
150
151        using (var enumerator = GetVisibleDataTables().GetEnumerator()) {
152          for (int row = 0; row < rows; row++) {
153            tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, height));
154            for (int col = 0; col < columns; col++) {
155              if (row == 0) {
156                // Add a column-style only when creating the first row
157                tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, width));
158              }
159
160              if (enumerator.MoveNext())
161                AddDataTableToTableLayout(enumerator.Current, row, col);
162
163            }
164          }
165        }
166        tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 0));
167      }
168
169      scrollPanel.ResumeRepaint(true);
170    }
171
172    private void AddDataTableToTableLayout(DataTableView dataTable, int row, int col) {
173      if (dataTable == null) {
174        // dummy panel for empty field
175        Panel p = new Panel { Dock = DockStyle.Fill };
176        tableLayoutPanel.Controls.Add(p, col, row);
177      } else {
178        dataTable.Dock = DockStyle.Fill;
179        tableLayoutPanel.Controls.Add(dataTable, col, row);
180      }
181    }
182
183    protected void ClearTableLayout() {
184      //Clear out the existing controls
185      tableLayoutPanel.Controls.Clear();
186
187      //Clear out the existing row and column styles
188      tableLayoutPanel.ColumnStyles.Clear();
189      tableLayoutPanel.RowStyles.Clear();
190    }
191    //Remove horizontal scroll bar if visible
192    private void tableLayoutPanel_Layout(object sender, LayoutEventArgs e) {
193      if (tableLayoutPanel.HorizontalScroll.Visible) {
194        // Add padding on the right in order to accomodate the vertical scrollbar
195        tableLayoutPanel.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
196      } else {
197        // Reset padding
198        tableLayoutPanel.Padding = new Padding(0);
199      }
200    }
201    #endregion
202
203    private void columnsNumericUpDown_ValueChanged(object sender, System.EventArgs e) {
204      GenerateLayout();
205    }
206
207    private void splitContainer_Panel2_Resize(object sender, EventArgs e) {
208      if (SuppressCheckedChangedUpdate)
209        return;
210
211      scrollPanel.SuspendRepaint();
212
213      if (tableLayoutPanel.ColumnCount > 0 && tableLayoutPanel.RowCount > 0) {
214        var width = (splitContainer.Panel2.Width - SystemInformation.VerticalScrollBarWidth) / tableLayoutPanel.ColumnCount;
215        var height = width * 0.75f;
216
217        for (int i = 0; i < tableLayoutPanel.RowStyles.Count - 1; i++) {
218          tableLayoutPanel.RowStyles[i].Height = height;
219        }
220        for (int i = 0; i < tableLayoutPanel.ColumnStyles.Count; i++) {
221          tableLayoutPanel.ColumnStyles[i].Width = width;
222        }
223      }
224
225      scrollPanel.ResumeRepaint(true);
226    }
227  }
228}
Note: See TracBrowser for help on using the repository browser.