Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2709 Adapted DataTable/ScatterPlotControl to the recent (re-) merge of the -View and -Control.

File size: 8.4 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
50    public PreprocessingChartView() {
51      InitializeComponent();
52      dataTables = new Dictionary<string, DataTable>();
53      dataTableViews = new Dictionary<string, DataTableView>();
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], ShowName = false });
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      foreach (IndexedItem<StringValue> item in checkedItems.Items) {
92        string variableName = item.Value.Value;
93
94        if (!IsVariableChecked(variableName)) {
95          // not checked -> remove
96          //dataTableView.SetRowEnabled(variableName, false);
97          //dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == variableName)));
98        } else {
99          // checked -> add
100          //DataRow row = GetDataRow(variableName);
101          //dataTableView.SetRowEnabled(variableName, true);
102
103          //var pdt = new DataTable(variableName);
104          //pdt.VisualProperties.Title = string.Empty;
105          //pdt.Rows.Add(row);
106          // dataTablePerVariable does not contain unchecked variables => reduce insert position by number of uncheckt variables to correct the index
107          //int uncheckedUntilVariable = checkedItemList.Content.TakeWhile(x => x.Value != variableName).Count(x => !checkedItemList.Content.ItemChecked(x));
108          //dataTables.Insert(item.Index - uncheckedUntilVariable, pdt);
109        }
110      }
111
112      // update chart if not in all in one mode
113      //if (Content != null && !Content.AllInOneMode)
114      //GenerateChart();??
115      GenerateLayout();
116    }
117
118
119    #region Add/Remove/Update Variable, Reset
120    protected override void AddVariable(string name) {
121      base.AddVariable(name);
122      dataTables.Add(name, CreateDataTable(name));
123
124      GenerateLayout();
125    }
126
127    // remove variable from data table and item list
128    protected override void RemoveVariable(string name) {
129      base.RemoveVariable(name);
130      dataTables.Remove(name);
131      dataTableViews.Remove(name);
132
133      GenerateLayout();
134    }
135
136    protected override void UpdateVariable(string name) {
137      base.UpdateVariable(name);
138      dataTables.Remove(name);
139      var newDataTable = CreateDataTable(name);
140      dataTables.Add(name, newDataTable);
141      dataTableViews[name].Content = newDataTable;
142      GenerateLayout();
143    }
144    protected override void ResetAllVariables() {
145      InitData();
146    }
147    #endregion
148
149    protected override void CheckedChangedUpdate() {
150      GenerateLayout();
151    }
152
153    #region Generate Layout
154    protected void GenerateLayout() {
155      if (suppressCheckedChangedUpdate)
156        return;
157
158      scrollPanel.SuspendRepaint();
159
160      ClearTableLayout();
161
162      int nrCharts = GetNumberOfVisibleDataTables();
163
164      // Set columns and rows based on number of items
165      int columns = Math.Min(nrCharts, (int)columnsNumericUpDown.Value);
166      int rows = (int)Math.Ceiling((float)nrCharts / columns);
167
168      tableLayoutPanel.ColumnCount = columns;
169      tableLayoutPanel.RowCount = rows;
170
171      var width = (splitContainer.Panel2.Width - SystemInformation.VerticalScrollBarWidth) / columns;
172      var height = width * 0.75f;
173
174      using (var enumerator = GetVisibleDataTables().GetEnumerator()) {
175        for (int row = 0; row < rows; row++) {
176          tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, height));
177          for (int col = 0; col < columns; col++) {
178            if (row == 0) { // Add a column-style only when creating the first row
179              tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, width));
180            }
181
182            if (enumerator.MoveNext())
183              AddDataTableToTableLayout(enumerator.Current, row, col);
184
185          }
186        }
187      }
188      tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 0));
189
190      scrollPanel.ResumeRepaint(true);
191    }
192
193    private void AddDataTableToTableLayout(DataTableView dataTable, int row, int col) {
194      //dataView.Classification = Classification;
195      //dataView.IsDetailedChartViewEnabled = IsDetailedChartViewEnabled;
196
197      if (dataTable == null) {
198        // dummy panel for empty field
199        Panel p = new Panel { Dock = DockStyle.Fill };
200        tableLayoutPanel.Controls.Add(p, col, row);
201      } else {
202        dataTable.Dock = DockStyle.Fill;
203        tableLayoutPanel.Controls.Add(dataTable, col, row);
204      }
205    }
206
207    protected void ClearTableLayout() {
208      //Clear out the existing controls
209      tableLayoutPanel.Controls.Clear();
210
211      //Clear out the existing row and column styles
212      tableLayoutPanel.ColumnStyles.Clear();
213      tableLayoutPanel.RowStyles.Clear();
214    }
215    //Remove horizontal scroll bar if visible
216    private void tableLayoutPanel_Layout(object sender, LayoutEventArgs e) {
217      if (tableLayoutPanel.HorizontalScroll.Visible) {
218        // Add padding on the right in order to accomodate the vertical scrollbar
219        tableLayoutPanel.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
220      } else {
221        // Reset padding
222        tableLayoutPanel.Padding = new Padding(0);
223      }
224    }
225    #endregion
226
227    private void columnsNumericUpDown_ValueChanged(object sender, System.EventArgs e) {
228      GenerateLayout();
229    }
230  }
231}
Note: See TracBrowser for help on using the repository browser.