Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10972 was 10972, checked in by aesterer, 10 years ago

Chart view refactoring

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