Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2709 Fixed an issue with empty charts.

File size: 7.2 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 = Math.Max(columns, 0);
144      tableLayoutPanel.RowCount = Math.Max(rows, 0);
145
146      if (columns > 0 && rows > 0) {
147        var width = (splitContainer.Panel2.Width - SystemInformation.VerticalScrollBarWidth) / columns;
148        var height = width * 0.75f;
149
150        using (var enumerator = GetVisibleDataTables().GetEnumerator()) {
151          for (int row = 0; row < rows; row++) {
152            tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, height));
153            for (int col = 0; col < columns; col++) {
154              if (row == 0) {
155                // Add a column-style only when creating the first row
156                tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, width));
157              }
158
159              if (enumerator.MoveNext())
160                AddDataTableToTableLayout(enumerator.Current, row, col);
161
162            }
163          }
164        }
165        tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 0));
166      }
167
168      scrollPanel.ResumeRepaint(true);
169    }
170
171    private void AddDataTableToTableLayout(DataTableView dataTable, int row, int col) {
172      if (dataTable == null) {
173        // dummy panel for empty field
174        Panel p = new Panel { Dock = DockStyle.Fill };
175        tableLayoutPanel.Controls.Add(p, col, row);
176      } else {
177        dataTable.Dock = DockStyle.Fill;
178        tableLayoutPanel.Controls.Add(dataTable, col, row);
179      }
180    }
181
182    protected void ClearTableLayout() {
183      //Clear out the existing controls
184      tableLayoutPanel.Controls.Clear();
185
186      //Clear out the existing row and column styles
187      tableLayoutPanel.ColumnStyles.Clear();
188      tableLayoutPanel.RowStyles.Clear();
189    }
190    //Remove horizontal scroll bar if visible
191    private void tableLayoutPanel_Layout(object sender, LayoutEventArgs e) {
192      if (tableLayoutPanel.HorizontalScroll.Visible) {
193        // Add padding on the right in order to accomodate the vertical scrollbar
194        tableLayoutPanel.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
195      } else {
196        // Reset padding
197        tableLayoutPanel.Padding = new Padding(0);
198      }
199    }
200    #endregion
201
202    private void columnsNumericUpDown_ValueChanged(object sender, System.EventArgs e) {
203      GenerateLayout();
204    }
205  }
206}
Note: See TracBrowser for help on using the repository browser.