Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/HistogramView.cs @ 14581

Last change on this file since 14581 was 14581, checked in by mkommend, 7 years ago

#2709: Refactored get variables for grouping (extracted method to another class).

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.MainForm;
26
27namespace HeuristicLab.DataPreprocessing.Views {
28  [View("Histogram View")]
29  [Content(typeof(HistogramContent), true)]
30  public partial class HistogramView : PreprocessingChartView {
31    public new HistogramContent Content {
32      get { return (HistogramContent)base.Content; }
33      set { base.Content = value; }
34    }
35
36    public HistogramView() {
37      InitializeComponent();
38    }
39
40    protected override void OnContentChanged() {
41      base.OnContentChanged();
42      classifierComboBox.Items.Clear();
43      classifierComboBox.Items.Add("");
44
45      if (Content != null) {
46        foreach (string var in GetVariableNamesForGrouping(Content.PreprocessingData)) {
47          classifierComboBox.Items.Add(var);
48        }
49
50        classifierComboBox.SelectedItem = Content.GroupingVariableName;
51      }
52    }
53
54    protected override DataTable CreateDataTable(string variableName) {
55      var dataTable = new DataTable();
56
57      if (string.IsNullOrEmpty(Content.GroupingVariableName)) {
58        var row = Content.CreateDataRow(variableName, DataRowVisualProperties.DataRowChartType.Histogram);
59        dataTable.Rows.Add(row);
60        return dataTable;
61      }
62
63      dataTable.VisualProperties.Title = variableName;
64
65      int variableIndex = Content.PreprocessingData.GetColumnIndex(variableName);
66      var variableValues = Content.PreprocessingData.GetValues<double>(variableIndex);
67      int groupVariableIndex = Content.PreprocessingData.GetColumnIndex(Content.GroupingVariableName);
68      var groupingValues = Enumerable.Empty<string>();
69
70      if (Content.PreprocessingData.VariableHasType<double>(groupVariableIndex)) {
71        groupingValues = Content.PreprocessingData.GetValues<double>(groupVariableIndex).Select(x => x.ToString());
72      } else if (Content.PreprocessingData.VariableHasType<string>(groupVariableIndex)) {
73        groupingValues = Content.PreprocessingData.GetValues<string>(groupVariableIndex);
74      } else if (Content.PreprocessingData.VariableHasType<DateTime>(groupVariableIndex)) {
75        groupingValues = Content.PreprocessingData.GetValues<DateTime>(groupVariableIndex).Select(x => x.ToString());
76      }
77
78      var groups = groupingValues.Zip(variableValues, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2);
79
80      foreach (var group in groups) {
81        var classRow = new DataRow();
82        classRow.Name = group.Key;
83        classRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
84        classRow.Values.AddRange(group);
85        dataTable.Rows.Add(classRow);
86      }
87      return dataTable;
88    }
89
90
91    private void classifierComboBox_SelectedIndexChanged(object sender, EventArgs e) {
92      Content.GroupingVariableName = classifierComboBox.SelectedItem.ToString();
93
94      // rebuild datatables
95      InitData();
96      GenerateLayout();
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.