Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2709

  • Fixed initial selection of the grouping text box (empty string instead of null to select the first entry).
  • General code fixes (removed unnessecary bank lines and code, class member order, ...)
File size: 7.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;
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    }
54
55    protected override void OnContentChanged() {
56      base.OnContentChanged();
57      if (Content != null) {
58        InitData();
59        GenerateLayout();
60      }
61    }
62
63    protected virtual int GetNumberOfVisibleDataTables() {
64      return Content.VariableItemList.CheckedItems.Count();
65    }
66
67    protected virtual IEnumerable<DataTableView> GetVisibleDataTables() {
68      foreach (var name in Content.VariableItemList.CheckedItems) {
69        if (!dataTableViews.ContainsKey(name.Value.Value))
70          dataTableViews.Add(name.Value.Value, new DataTableView() { Content = dataTables[name.Value.Value], ShowName = false });
71        yield return dataTableViews[name.Value.Value];
72      }
73    }
74
75    protected virtual DataTable CreateDataTable(string variableName) {
76      return null;
77    }
78
79    protected virtual void InitData() {
80      dataTables.Clear();
81      dataTableViews.Clear();
82      foreach (var variable in Content.VariableItemList.Select(v => v.Value)) {
83        dataTables.Add(variable, CreateDataTable(variable));
84      }
85    }
86
87    protected override void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
88      base.CheckedItemsChanged(sender, checkedItems);
89
90      GenerateLayout();
91    }
92
93
94    #region Add/Remove/Update Variable, Reset
95    protected override void AddVariable(string name) {
96      base.AddVariable(name);
97      dataTables.Add(name, CreateDataTable(name));
98
99      GenerateLayout();
100    }
101
102    // remove variable from data table and item list
103    protected override void RemoveVariable(string name) {
104      base.RemoveVariable(name);
105      dataTables.Remove(name);
106      dataTableViews.Remove(name);
107
108      GenerateLayout();
109    }
110
111    protected override void UpdateVariable(string name) {
112      base.UpdateVariable(name);
113      dataTables.Remove(name);
114      var newDataTable = CreateDataTable(name);
115      dataTables.Add(name, newDataTable);
116      dataTableViews[name].Content = newDataTable;
117      GenerateLayout();
118    }
119    protected override void ResetAllVariables() {
120      InitData();
121    }
122    #endregion
123
124    protected override void CheckedChangedUpdate() {
125      GenerateLayout();
126    }
127
128    #region Generate Layout
129    protected void GenerateLayout() {
130      if (SuppressCheckedChangedUpdate)
131        return;
132
133      scrollPanel.SuspendRepaint();
134
135      ClearTableLayout();
136
137      int nrCharts = GetNumberOfVisibleDataTables();
138
139      // Set columns and rows based on number of items
140      int columns = Math.Min(nrCharts, (int)columnsNumericUpDown.Value);
141      int rows = (int)Math.Ceiling((float)nrCharts / columns);
142
143      tableLayoutPanel.ColumnCount = columns;
144      tableLayoutPanel.RowCount = rows;
145
146      var width = (splitContainer.Panel2.Width - SystemInformation.VerticalScrollBarWidth) / columns;
147      var height = width * 0.75f;
148
149      using (var enumerator = GetVisibleDataTables().GetEnumerator()) {
150        for (int row = 0; row < rows; row++) {
151          tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, height));
152          for (int col = 0; col < columns; col++) {
153            if (row == 0) { // Add a column-style only when creating the first row
154              tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, width));
155            }
156
157            if (enumerator.MoveNext())
158              AddDataTableToTableLayout(enumerator.Current, row, col);
159
160          }
161        }
162      }
163      tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 0));
164
165      scrollPanel.ResumeRepaint(true);
166    }
167
168    private void AddDataTableToTableLayout(DataTableView dataTable, int row, int col) {
169      if (dataTable == null) {
170        // dummy panel for empty field
171        Panel p = new Panel { Dock = DockStyle.Fill };
172        tableLayoutPanel.Controls.Add(p, col, row);
173      } else {
174        dataTable.Dock = DockStyle.Fill;
175        tableLayoutPanel.Controls.Add(dataTable, col, row);
176      }
177    }
178
179    protected void ClearTableLayout() {
180      //Clear out the existing controls
181      tableLayoutPanel.Controls.Clear();
182
183      //Clear out the existing row and column styles
184      tableLayoutPanel.ColumnStyles.Clear();
185      tableLayoutPanel.RowStyles.Clear();
186    }
187    //Remove horizontal scroll bar if visible
188    private void tableLayoutPanel_Layout(object sender, LayoutEventArgs e) {
189      if (tableLayoutPanel.HorizontalScroll.Visible) {
190        // Add padding on the right in order to accomodate the vertical scrollbar
191        tableLayoutPanel.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
192      } else {
193        // Reset padding
194        tableLayoutPanel.Padding = new Padding(0);
195      }
196    }
197    #endregion
198
199    private void columnsNumericUpDown_ValueChanged(object sender, System.EventArgs e) {
200      GenerateLayout();
201    }
202  }
203}
Note: See TracBrowser for help on using the repository browser.