Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13397 was 12718, checked in by mkommend, 9 years ago

#2335: Merged all changes into stable.

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