Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingChartView.cs @ 14820

Last change on this file since 14820 was 14796, checked in by mkommend, 8 years ago

#2663: Merged r14368, r14270, and r14371 to stable.

File size: 14.0 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
[14186]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10539]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
[10717]22using System;
23using System.Collections.Generic;
[12718]24using System.Linq;
[10303]25using System.Windows.Forms;
[10628]26using HeuristicLab.Analysis;
27using HeuristicLab.Collections;
[10303]28using HeuristicLab.Core.Views;
[10628]29using HeuristicLab.Data;
[10303]30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.DataPreprocessing.Views {
[10658]33  [View("Preprocessing Chart View")]
34  [Content(typeof(PreprocessingChartContent), false)]
35  public partial class PreprocessingChartView : ItemView {
[10377]36
[10803]37    private PreprocessingDataTable dataTable;
38    private List<PreprocessingDataTable> dataTablePerVariable;
[10717]39    private List<DataRow> dataRows;
[10847]40    private List<DataRow> selectedDataRows;
[10717]41
[10658]42    protected DataRowVisualProperties.DataRowChartType chartType;
43    protected string chartTitle;
[10628]44
[10658]45    private const string DEFAULT_CHART_TITLE = "Chart";
[10972]46    private const int FIXED_CHART_SIZE = 300;
[10999]47    private const int MAX_TABLE_AUTO_SIZE_ROWS = 3;
[10658]48
[10908]49    public IEnumerable<double> Classification { get; set; }
[12718]50    public bool IsDetailedChartViewEnabled { get; set; }
[10658]51
52    public PreprocessingChartView() {
[10303]53      InitializeComponent();
[10658]54      chartType = DataRowVisualProperties.DataRowChartType.Line;
55      chartTitle = DEFAULT_CHART_TITLE;
[10303]56    }
57
[10972]58    //Variable selection changed
59    //Add or remove data row
[10628]60    private void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
[14796]61      tableLayoutPanel.SuspendLayout();
[10712]62      foreach (IndexedItem<StringValue> item in checkedItems.Items) {
[10628]63        string variableName = item.Value.Value;
[10847]64
[14796]65        // not checked -> remove
66        if (!VariableIsChecked(variableName)) {
67          dataTableView.SetRowEnabled(variableName, false);
[10847]68          dataTable.SelectedRows.Remove(variableName);
[10804]69          dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == variableName)));
[10628]70        } else {
[10717]71          DataRow row = GetDataRow(variableName);
[10847]72          DataRow selectedRow = GetSelectedDataRow(variableName);
[14796]73          dataTableView.SetRowEnabled(variableName, true);
[10972]74
75          PreprocessingDataTable pdt = new PreprocessingDataTable(variableName);
76          pdt.Rows.Add(row);
[14796]77          // dataTablePerVariable does not contain unchecked variables => reduce insert position by number of unchecked variables to correct the index
78          int uncheckedUntilVariable = checkedItemList.Content.TakeWhile(x => x.Value != variableName).Count(x => !checkedItemList.Content.ItemChecked(x));
79          dataTablePerVariable.Insert(item.Index - uncheckedUntilVariable, pdt);
[10972]80
81          //update selection
[10992]82          if (selectedRow != null) {
[10847]83            dataTable.SelectedRows.Add(selectedRow);
[10972]84            pdt.SelectedRows.Add(selectedRow);
[10992]85          }
[10628]86        }
87      }
[10717]88
[10972]89      // update chart if not in all in one mode
[10908]90      if (Content != null && !Content.AllInOneMode)
[10717]91        GenerateChart();
[14796]92      tableLayoutPanel.ResumeLayout(true);
[10628]93    }
94
[14796]95    private bool VariableIsChecked(string name) {
96      return Content.VariableItemList.CheckedItems.Any(x => x.Value.Value == name);
[10628]97    }
98
[10573]99    protected override void RegisterContentEvents() {
100      base.RegisterContentEvents();
[10992]101      Content.PreprocessingData.Changed += PreprocessingData_Changed;
102      Content.PreprocessingData.SelectionChanged += PreprocessingData_SelctionChanged;
[10804]103
[10573]104    }
105
106    protected override void DeregisterContentEvents() {
107      base.DeregisterContentEvents();
[10992]108      Content.PreprocessingData.Changed -= PreprocessingData_Changed;
109      Content.PreprocessingData.SelectionChanged -= PreprocessingData_SelctionChanged;
[10573]110    }
111
[10658]112    public new PreprocessingChartContent Content {
113      get { return (PreprocessingChartContent)base.Content; }
[10303]114      set { base.Content = value; }
115    }
116
[10717]117    private void InitData() {
[10992]118      if (Content.VariableItemList == null) {
119        Content.VariableItemList = Content.CreateVariableItemList();
[12718]120      } else {
121        var checkedNames = Content.VariableItemList.CheckedItems.Select(x => x.Value.Value);
122        Content.VariableItemList = Content.CreateVariableItemList(checkedNames);
[10818]123      }
124      checkedItemList.Content = Content.VariableItemList;
[10972]125
126      //Create data tables and data rows
[10992]127      dataRows = Content.CreateAllDataRows(chartType);
[10803]128      dataTable = new PreprocessingDataTable(chartTitle);
[10847]129      dataTablePerVariable = new List<PreprocessingDataTable>();
[10717]130
[10847]131      //add data rows to data tables according to checked item list
[10992]132      foreach (var checkedItem in Content.VariableItemList.CheckedItems) {
[10818]133        string variableName = Content.VariableItemList[checkedItem.Index].Value;
[10803]134        PreprocessingDataTable d = new PreprocessingDataTable(variableName);
[10717]135        DataRow row = GetDataRow(variableName);
[10804]136
[12718]137        if (row != null) {
138          //add row to data table
139          dataTable.Rows.Add(row);
[10847]140
[12718]141          //add row to data table per variable
142          d.Rows.Add(row);
143          dataTablePerVariable.Add(d);
144        }
[10847]145      }
[10803]146
[10847]147      UpdateSelection();
148    }
149
150    private void UpdateSelection() {
151
152      //update data table selection
[10992]153      selectedDataRows = Content.CreateAllSelectedDataRows(chartType);
[10847]154      dataTable.SelectedRows.Clear();
[11039]155      foreach (var selectedRow in selectedDataRows) {
[14796]156        if (VariableIsChecked(selectedRow.Name))
[11039]157          dataTable.SelectedRows.Add(selectedRow);
158      }
[10847]159
160      //update data table per variable selection
161      foreach (PreprocessingDataTable d in dataTablePerVariable) {
162        d.SelectedRows.Clear();
163        DataRow row = selectedDataRows.Find(x => x.Name == d.Name);
164        if (row != null)
165          d.SelectedRows.Add(row);
[10717]166      }
[10847]167
[10717]168    }
169
[10847]170    private DataRow GetSelectedDataRow(string variableName) {
171      foreach (DataRow row in selectedDataRows) {
172        if (row.Name == variableName)
173          return row;
174      }
175      return null;
176    }
177
[10717]178    private DataRow GetDataRow(string variableName) {
179      foreach (DataRow row in dataRows) {
180        if (row.Name == variableName)
181          return row;
182      }
183      return null;
184    }
185
[10303]186    protected override void OnContentChanged() {
187      base.OnContentChanged();
[10377]188      if (Content != null) {
[10804]189        InitData();
[10818]190        Content.VariableItemList.CheckedItemsChanged += CheckedItemsChanged;
[10804]191        GenerateChart();
[10377]192      }
[10303]193    }
[10377]194
[10717]195    // TODO : handle also other changed events
[10628]196    void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) {
197      switch (e.Type) {
198        case DataPreprocessingChangedEventType.DeleteColumn:
[10992]199          RemoveVariable(Content.PreprocessingData.GetVariableName(e.Column));
[10628]200          break;
201        case DataPreprocessingChangedEventType.AddColumn:
[10992]202          AddVariable(Content.PreprocessingData.GetVariableName(e.Column));
[10628]203          break;
204        case DataPreprocessingChangedEventType.ChangeColumn:
205        case DataPreprocessingChangedEventType.ChangeItem:
[10992]206          UpdateDataForVariable(Content.PreprocessingData.GetVariableName(e.Column));
[10628]207          break;
208        case DataPreprocessingChangedEventType.DeleteRow:
209        case DataPreprocessingChangedEventType.AddRow:
[10736]210        case DataPreprocessingChangedEventType.Any:
[10817]211        default:
[10992]212          //TODO: test with transform
[10736]213          InitData();
[10804]214          GenerateChart();
[10628]215          break;
[10382]216      }
[10377]217    }
218
[10847]219    private void PreprocessingData_SelctionChanged(object sender, EventArgs e) {
220      UpdateSelection();
221    }
222
[10717]223    private void UpdateDataForVariable(string variableName) {
[10992]224      DataRow newRow = Content.CreateDataRow(variableName, chartType);
[10717]225      dataTable.Rows.Remove(variableName);
226      dataTable.Rows.Add(newRow);
[10736]227      DataTable dt = dataTablePerVariable.Find(x => x.Rows.Find(y => y.Name == variableName) != null);
228      if (dt != null) {
229        dt.Rows.Remove(variableName);
230        dt.Rows.Add(newRow);
231      }
[10717]232    }
233
[10628]234    // add variable to data table and item list
235    private void AddVariable(string name) {
[10992]236      DataRow row = Content.CreateDataRow(name, chartType);
[10741]237      dataTable.Rows.Add(row);
[10803]238      PreprocessingDataTable d = new PreprocessingDataTable(name);
[10741]239      d.Rows.Add(row);
240      dataTablePerVariable.Add(d);
[10818]241      Content.VariableItemList.Add(new StringValue(name));
242      if (!Content.AllInOneMode)
[10741]243        GenerateChart();
[10628]244    }
[10377]245
[10628]246    // remove variable from data table and item list
247    private void RemoveVariable(string name) {
248      dataTable.Rows.Remove(name);
[10741]249      dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == name)));
[10382]250
[10628]251      StringValue stringValue = FindVariableItemList(name);
252      if (stringValue != null)
[10818]253        Content.VariableItemList.Remove(stringValue);
254      if (!Content.AllInOneMode)
[10741]255        GenerateChart();
[10377]256    }
[10628]257
258    private StringValue FindVariableItemList(string name) {
[10992]259      foreach (StringValue stringValue in Content.VariableItemList) {
[10628]260        if (stringValue.Value == name)
261          return stringValue;
262      }
263      return null;
264    }
265
[10717]266    protected void GenerateChart() {
[10804]267      ClearTableLayout();
[10992]268      if (Content.AllInOneMode) {
[10736]269        GenerateSingleChartLayout();
270      } else
271        GenerateMultiChartLayout();
272    }
273
274    private void ClearTableLayout() {
[10717]275      //Clear out the existing controls
276      tableLayoutPanel.Controls.Clear();
277
278      //Clear out the existing row and column styles
279      tableLayoutPanel.ColumnStyles.Clear();
280      tableLayoutPanel.RowStyles.Clear();
281      tableLayoutPanel.AutoScroll = false;
[10736]282      tableLayoutPanel.AutoScroll = true;
[10804]283    }
[10717]284
285    private void GenerateSingleChartLayout() {
286      tableLayoutPanel.ColumnCount = 1;
287      tableLayoutPanel.RowCount = 1;
288      tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
289      tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
290      tableLayoutPanel.Controls.Add(dataTableView, 0, 0);
291      dataTableView.Content = dataTable;
292    }
293
[10972]294    private int GetNrOfMultiChartColumns(int itemCount) {
[10717]295      int columns = 0;
[10972]296      if (itemCount <= 2)
[10717]297        columns = 1;
[10972]298      else if (itemCount <= 6)
[10717]299        columns = 2;
300      else
301        columns = 3;
[10972]302      return columns;
303    }
[10717]304
[10972]305    private int GetNrOfMultiChartRows(int itemCount, int columns) {
306      int rows = 0;
[10717]307      if (columns == 3)
[10972]308        rows = (itemCount + 2) / columns;
[10804]309      else if (columns == 2)
[10972]310        rows = (itemCount + 1) / columns;
[10717]311      else
[10972]312        rows = itemCount / columns;
313      return rows;
314    }
[10717]315
[10972]316
317    private void GenerateMultiChartLayout() {
318      int checkedItemsCnt = 0;
319      foreach (var item in Content.VariableItemList.CheckedItems)
320        checkedItemsCnt++;
321
322      // set columns and rows based on number of items
323      int columns = GetNrOfMultiChartColumns(checkedItemsCnt);
324      int rows = GetNrOfMultiChartRows(checkedItemsCnt, columns);
[10992]325
[10717]326      tableLayoutPanel.ColumnCount = columns;
327      tableLayoutPanel.RowCount = rows;
328
[10803]329      List<PreprocessingDataTable>.Enumerator enumerator = dataTablePerVariable.GetEnumerator();
[10717]330      for (int x = 0; x < columns; x++) {
[10804]331
[10999]332        if (rows <= MAX_TABLE_AUTO_SIZE_ROWS)
[10717]333          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100 / columns));
334        else
335          //scrollbar is shown if there are more than 3 rows -> remove scroll bar width from total width
336          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, (tableLayoutPanel.Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth) / columns));
337        for (int y = 0; y < rows; y++) {
338          //Add a row only when creating the first column
339          if (x == 0) {
340            // fixed chart size when there are more than 3 tables
[10999]341            if (rows > MAX_TABLE_AUTO_SIZE_ROWS)
[10972]342              tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, FIXED_CHART_SIZE));
[10717]343            else
344              tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100 / rows));
345          }
346
347          enumerator.MoveNext();
[10803]348          PreprocessingDataTable d = enumerator.Current;
[10972]349          AddDataTableToTableLayout(d, x, y);
[10992]350
[10717]351        }
352      }
353    }
354
[10972]355    private void AddDataTableToTableLayout(PreprocessingDataTable dataTable, int x, int y) {
356      PreprocessingDataTableView dataView = new PreprocessingDataTableView();
357      dataView.Classification = Classification;
[12718]358      dataView.IsDetailedChartViewEnabled = IsDetailedChartViewEnabled;
[10972]359
360      if (dataTable == null) {
361        // dummy panel for empty field
362        Panel p = new Panel();
363        p.Dock = DockStyle.Fill;
364        tableLayoutPanel.Controls.Add(p, y, x);
365      } else {
366        dataView.Content = dataTable;
367        dataView.Dock = DockStyle.Fill;
368        tableLayoutPanel.Controls.Add(dataView, y, x);
369      }
370    }
371
[10717]372    //Remove horizontal scroll bar if visible
373    private void tableLayoutPanel_Layout(object sender, LayoutEventArgs e) {
374      if (tableLayoutPanel.HorizontalScroll.Visible) {
375        // Add padding on the right in order to accomodate the vertical scrollbar
376        int vWidth = SystemInformation.VerticalScrollBarWidth;
377        tableLayoutPanel.Padding = new Padding(0, 0, vWidth, 0);
378      } else {
379        // Reset padding
380        tableLayoutPanel.Padding = new Padding(0);
381      }
382    }
383
[10303]384  }
385}
386
387
Note: See TracBrowser for help on using the repository browser.