Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.3/PreprocessingChartView.cs @ 10867

Last change on this file since 10867 was 10867, checked in by mleitner, 10 years ago

Add colors to histogram / remove all in one mode

File size: 12.9 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";
50
[10867]51    public List<double> classification { get; set; }
[10658]52
53    public PreprocessingChartView() {
[10303]54      InitializeComponent();
[10658]55      chartType = DataRowVisualProperties.DataRowChartType.Line;
56      chartTitle = DEFAULT_CHART_TITLE;
[10303]57    }
58
[10628]59    private void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
[10712]60      foreach (IndexedItem<StringValue> item in checkedItems.Items) {
[10628]61        string variableName = item.Value.Value;
[10847]62
63        //variable is displayed -> remove
[10628]64        if (VariableIsDisplayed(variableName)) {
65          dataTable.Rows.Remove(variableName);
[10847]66          dataTable.SelectedRows.Remove(variableName);
[10804]67          dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == variableName)));
[10847]68        //variable isnt't displayed -> add
[10628]69        } else {
[10717]70          DataRow row = GetDataRow(variableName);
[10847]71          DataRow selectedRow = GetSelectedDataRow(variableName);
[10717]72          dataTable.Rows.Add(row);
[10803]73          PreprocessingDataTable d = new PreprocessingDataTable(variableName);
[10717]74          d.Rows.Add(row);
75          dataTablePerVariable.Add(d);
[10847]76          if (selectedRow != null)
77          {
78            dataTable.SelectedRows.Add(selectedRow);
79            d.SelectedRows.Add(selectedRow);
[10628]80        }
81      }
[10847]82      }
[10717]83
[10818]84      //if (!Content.AllInOneMode)
85        if (Content != null && !Content.AllInOneMode)
[10717]86        GenerateChart();
87
[10628]88    }
89
[10717]90    private bool VariableIsDisplayed(string name) {
91
[10628]92      foreach (var item in dataTable.Rows) {
93        if (item.Name == name)
94          return true;
95      }
96      return false;
97    }
98
[10573]99    protected override void RegisterContentEvents() {
100      base.RegisterContentEvents();
[10736]101      Content.ChartLogic.Changed += PreprocessingData_Changed;
[10847]102      Content.ChartLogic.SelectionChanged += PreprocessingData_SelctionChanged;
[10804]103
[10573]104    }
105
106    protected override void DeregisterContentEvents() {
107      base.DeregisterContentEvents();
[10736]108      Content.ChartLogic.Changed -= PreprocessingData_Changed;
[10847]109      Content.ChartLogic.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() {
[10818]118      if (Content.VariableItemList == null)
119      {
120        Content.VariableItemList = logic.CreateVariableItemList();
121      }
122      checkedItemList.Content = Content.VariableItemList;
[10717]123      dataRows = logic.CreateAllDataRows(chartType);
124
[10803]125      dataTable = new PreprocessingDataTable(chartTitle);
[10847]126      dataTablePerVariable = new List<PreprocessingDataTable>();
[10717]127
[10847]128      //add data rows to data tables according to checked item list
[10818]129      foreach (var checkedItem in Content.VariableItemList.CheckedItems)
130      {
131        string variableName = Content.VariableItemList[checkedItem.Index].Value;
[10803]132        PreprocessingDataTable d = new PreprocessingDataTable(variableName);
[10717]133        DataRow row = GetDataRow(variableName);
[10804]134
[10847]135        //add row to data table
136        dataTable.Rows.Add(row);
137
138        //add row to data table per variable
[10717]139        d.Rows.Add(row);
140        dataTablePerVariable.Add(d);
[10847]141      }
[10803]142
[10847]143      UpdateSelection();
144    }
145
146    private void UpdateSelection() {
147
148      //update data table selection
149      selectedDataRows = logic.CreateAllSelectedDataRows(chartType);
150      dataTable.SelectedRows.Clear();
151      dataTable.SelectedRows.AddRange(selectedDataRows);
152
153      //update data table per variable selection
154      foreach (PreprocessingDataTable d in dataTablePerVariable) {
155        d.SelectedRows.Clear();
156        DataRow row = selectedDataRows.Find(x => x.Name == d.Name);
157        if (row != null)
158          d.SelectedRows.Add(row);
[10717]159      }
[10847]160
[10717]161    }
162
[10847]163    private DataRow GetSelectedDataRow(string variableName) {
164      foreach (DataRow row in selectedDataRows) {
165        if (row.Name == variableName)
166          return row;
167      }
168      return null;
169    }
170
[10717]171    private DataRow GetDataRow(string variableName) {
172      foreach (DataRow row in dataRows) {
173        if (row.Name == variableName)
174          return row;
175      }
176      return null;
177    }
178
[10303]179    protected override void OnContentChanged() {
180      base.OnContentChanged();
[10377]181      if (Content != null) {
[10658]182        logic = Content.ChartLogic;
[10804]183        InitData();
[10818]184        Content.VariableItemList.CheckedItemsChanged += CheckedItemsChanged;
[10804]185        GenerateChart();
[10377]186      }
[10303]187    }
[10377]188
[10717]189    // TODO : handle also other changed events
[10628]190    void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) {
191      switch (e.Type) {
192        case DataPreprocessingChangedEventType.DeleteColumn:
[10741]193          RemoveVariable(logic.GetVariableNameByIndex(e.Column));
[10628]194          break;
195        case DataPreprocessingChangedEventType.AddColumn:
[10741]196          AddVariable(logic.GetVariableNameByIndex(e.Column));
[10628]197          break;
198        case DataPreprocessingChangedEventType.ChangeColumn:
199        case DataPreprocessingChangedEventType.ChangeItem:
[10736]200          UpdateDataForVariable(logic.GetVariableNameByIndex(e.Column));
[10628]201          break;
202        case DataPreprocessingChangedEventType.DeleteRow:
203        case DataPreprocessingChangedEventType.AddRow:
[10736]204        case DataPreprocessingChangedEventType.Any:
[10817]205        default:
[10847]206           //TODO: test with transform
[10736]207          InitData();
[10804]208          GenerateChart();
[10628]209          break;
[10382]210      }
[10377]211    }
212
[10847]213    private void PreprocessingData_SelctionChanged(object sender, EventArgs e) {
214      UpdateSelection();
215    }
216
[10717]217    private void UpdateDataForVariable(string variableName) {
218      DataRow newRow = logic.CreateDataRow(variableName, chartType);
219      dataTable.Rows.Remove(variableName);
220      dataTable.Rows.Add(newRow);
[10736]221      DataTable dt = dataTablePerVariable.Find(x => x.Rows.Find(y => y.Name == variableName) != null);
222      if (dt != null) {
223        dt.Rows.Remove(variableName);
224        dt.Rows.Add(newRow);
225      }
[10717]226    }
227
[10628]228    // add variable to data table and item list
229    private void AddVariable(string name) {
[10741]230      DataRow row = logic.CreateDataRow(name, chartType);
231      dataTable.Rows.Add(row);
[10803]232      PreprocessingDataTable d = new PreprocessingDataTable(name);
[10741]233      d.Rows.Add(row);
234      dataTablePerVariable.Add(d);
[10818]235      Content.VariableItemList.Add(new StringValue(name));
236      if (!Content.AllInOneMode)
[10741]237        GenerateChart();
[10628]238    }
[10377]239
[10628]240    // remove variable from data table and item list
241    private void RemoveVariable(string name) {
242      dataTable.Rows.Remove(name);
[10741]243      dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == name)));
[10382]244
[10628]245      StringValue stringValue = FindVariableItemList(name);
246      if (stringValue != null)
[10818]247        Content.VariableItemList.Remove(stringValue);
248      if (!Content.AllInOneMode)
[10741]249        GenerateChart();
[10377]250    }
[10628]251
252    private StringValue FindVariableItemList(string name) {
[10818]253      foreach (StringValue stringValue in Content.VariableItemList)
254      {
[10628]255        if (stringValue.Value == name)
256          return stringValue;
257      }
258      return null;
259    }
260
[10717]261    protected void GenerateChart() {
262
[10804]263      ClearTableLayout();
[10736]264
[10818]265      if (Content.AllInOneMode)
266      {
[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
292    private void GenerateMultiChartLayout() {
293      int checkedItemsCnt = 0;
[10818]294      foreach (var item in Content.VariableItemList.CheckedItems)
[10717]295        checkedItemsCnt++;
296
297      int rows = 0;
298      int columns = 0;
299
300      // set columns and rows based on number of items
301      if (checkedItemsCnt <= 2)
302        columns = 1;
303      else if (checkedItemsCnt <= 6)
304        columns = 2;
305      else
306        columns = 3;
307
308      if (columns == 3)
[10804]309        rows = (checkedItemsCnt + 2) / columns;
310      else if (columns == 2)
[10717]311        rows = (checkedItemsCnt + 1) / columns;
312      else
313        rows = checkedItemsCnt / columns;
314
315      //Now we will generate the table, setting up the row and column counts first
316      tableLayoutPanel.ColumnCount = columns;
317      tableLayoutPanel.RowCount = rows;
318
[10803]319      List<PreprocessingDataTable>.Enumerator enumerator = dataTablePerVariable.GetEnumerator();
[10717]320      for (int x = 0; x < columns; x++) {
[10804]321
322        if (rows <= 3)
[10717]323          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100 / columns));
324        else
325          //scrollbar is shown if there are more than 3 rows -> remove scroll bar width from total width
326          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, (tableLayoutPanel.Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth) / columns));
327        for (int y = 0; y < rows; y++) {
328          //Add a row only when creating the first column
329          if (x == 0) {
330            // fixed chart size when there are more than 3 tables
331            if (rows > 3)
332              tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 300));
333            else
334              tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100 / rows));
335          }
336
[10771]337          PreprocessingDataTableView dataView = new PreprocessingDataTableView();
[10867]338          dataView.classification = classification;
[10717]339          enumerator.MoveNext();
[10803]340          PreprocessingDataTable d = enumerator.Current;
[10717]341          if (d == null) {
342            // dummy panel for empty field
343            Panel p = new Panel();
344            p.Dock = DockStyle.Fill;
345            tableLayoutPanel.Controls.Add(p, y, x);
[10804]346          } else {
[10717]347            dataView.Content = d;
348            dataView.Dock = DockStyle.Fill;
349            tableLayoutPanel.Controls.Add(dataView, y, x);
350          }
351        }
352      }
353    }
354
355    //Remove horizontal scroll bar if visible
356    private void tableLayoutPanel_Layout(object sender, LayoutEventArgs e) {
357      if (tableLayoutPanel.HorizontalScroll.Visible) {
358        // Add padding on the right in order to accomodate the vertical scrollbar
359        int vWidth = SystemInformation.VerticalScrollBarWidth;
360        tableLayoutPanel.Padding = new Padding(0, 0, vWidth, 0);
361      } else {
362        // Reset padding
363        tableLayoutPanel.Padding = new Padding(0);
364      }
365    }
366
367    private void radioButton1_CheckedChanged(object sender, EventArgs e) {
[10818]368      if (Content.AllInOneMode == false)
369        Content.AllInOneMode = true;
[10717]370      else
[10818]371        Content.AllInOneMode = false;
[10717]372      GenerateChart();
373    }
374
375
376
[10303]377  }
378}
379
380
Note: See TracBrowser for help on using the repository browser.