Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingChartView.cs @ 12417

Last change on this file since 12417 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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