Free cookie consent management tool by TermsFeed Policy Generator

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

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

Created base view PreprocessingChartView for LineChartView and HistogramView. Replaced HistogramLogic and LineChartlogic with ChartLogic. Added base content ChartContent for LineChartContent and HistogramContent.

File size: 5.3 KB
Line 
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
22using System.Windows.Forms;
23using HeuristicLab.Analysis;
24using HeuristicLab.Collections;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.Data;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.DataPreprocessing.Views {
31
32  [View("Preprocessing Chart View")]
33  [Content(typeof(PreprocessingChartContent), false)]
34  public partial class PreprocessingChartView : ItemView {
35
36    private IChartLogic logic;
37    private DataTable dataTable;
38    private ICheckedItemList<StringValue> variableItemList;   
39    protected DataRowVisualProperties.DataRowChartType chartType;
40    protected string chartTitle;
41
42    private const string DEFAULT_CHART_TITLE = "Chart";
43
44
45    public PreprocessingChartView() {
46      InitializeComponent();
47      chartType = DataRowVisualProperties.DataRowChartType.Line;
48      chartTitle = DEFAULT_CHART_TITLE;
49    }
50
51    private void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
52      foreach(IndexedItem<StringValue> item in checkedItems.Items)
53      {
54        string variableName = item.Value.Value;
55        if (VariableIsDisplayed(variableName)) {
56          dataTable.Rows.Remove(variableName);
57        } else {
58          dataTable.Rows.Add(logic.CreateDataRow(variableName, chartType));
59        }
60      }
61    }
62
63    public bool VariableIsDisplayed(string name) {
64
65      foreach (var item in dataTable.Rows) {
66        if (item.Name == name)
67          return true;
68      }
69      return false;
70    }
71
72    private void InitDataTable() {
73      dataTable = logic.CreateDataTable(chartTitle, chartType);
74      dataTableView.Content = dataTable;
75    }
76
77    private void InitVariablesListBox() {
78      variableItemList = logic.CreateVariableItemList();
79      checkedItemList.Content = variableItemList;
80    }
81
82    protected override void RegisterContentEvents() {
83      base.RegisterContentEvents();
84      Content.Changed += Content_Changed;
85    }
86
87    protected override void DeregisterContentEvents() {
88      base.DeregisterContentEvents();
89      Content.Changed -= Content_Changed;
90    }
91
92    void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
93      dataTableView.Refresh();
94    }
95
96    public new PreprocessingChartContent Content {
97      get { return (PreprocessingChartContent)base.Content; }
98      set { base.Content = value; }
99    }
100
101    protected override void OnContentChanged() {
102      base.OnContentChanged();
103      if (Content != null) {
104        logic = Content.ChartLogic;
105        InitDataTable();
106        InitVariablesListBox();
107        variableItemList.CheckedItemsChanged += CheckedItemsChanged;
108        logic.Changed += PreprocessingData_Changed;
109      }
110    }
111
112    //TODO: refactor: possible code duplication with HistogramLogic
113    void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) {
114      switch (e.Type) {
115        case DataPreprocessingChangedEventType.DeleteColumn:
116          RemoveVariable(logic.GetVariableNameByIndex(e.Column));
117          break;
118        case DataPreprocessingChangedEventType.AddColumn:
119          AddVariable(logic.GetVariableNameByIndex(e.Column));
120          break;
121        case DataPreprocessingChangedEventType.ChangeColumn:
122        case DataPreprocessingChangedEventType.ChangeItem:
123          dataTable.Rows.Remove(logic.GetVariableNameByIndex(e.Column));
124          dataTable.Rows.Add(logic.CreateDataRow(logic.GetVariableNameByIndex(e.Column),chartType));
125          break;
126        case DataPreprocessingChangedEventType.DeleteRow:
127        case DataPreprocessingChangedEventType.AddRow:
128          InitDataTable();
129          break;
130      }
131    }
132
133    // add variable to data table and item list
134    private void AddVariable(string name) {
135      dataTable.Rows.Add(logic.CreateDataRow(name, chartType));
136      variableItemList.Add(new StringValue(name));
137    }
138
139    // remove variable from data table and item list
140    private void RemoveVariable(string name) {
141      dataTable.Rows.Remove(name);
142
143      StringValue stringValue = FindVariableItemList(name);
144      if (stringValue != null)
145        variableItemList.Remove(stringValue);
146
147    }
148
149    private StringValue FindVariableItemList(string name) {
150      foreach (StringValue stringValue in variableItemList) {
151        if (stringValue.Value == name)
152          return stringValue;
153      }
154      return null;
155    }
156
157
158  }
159}
160
161
Note: See TracBrowser for help on using the repository browser.