Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing/3.4/Content/HistogramContent.cs @ 14725

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

#2709: Added grouping for multi scatter plot view.

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.Drawing;
24using System.Linq;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28
29namespace HeuristicLab.DataPreprocessing {
30  [Item("Histogram", "Represents the histogram grid.")]
31  public class HistogramContent : PreprocessingChartContent {
32    public static new Image StaticItemImage {
33      get { return HeuristicLab.Common.Resources.VSImageLibrary.Statistics; }
34    }
35
36    public string GroupingVariableName { get; set; }
37
38    public int Bins { get; set; }
39    public bool ExactBins { get; set; }
40
41    public HistogramContent(IFilteredPreprocessingData preprocessingData)
42      : base(preprocessingData) {
43      Bins = 10;
44      ExactBins = false;
45    }
46
47    public HistogramContent(HistogramContent content, Cloner cloner)
48      : base(content, cloner) {
49    }
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new HistogramContent(this, cloner);
52    }
53
54    public static DataTable CreateHistogram(IFilteredPreprocessingData preprocessingData, string variableName, string groupingVariableName, DataRowVisualProperties.DataRowHistogramAggregation aggregation) {
55      {
56        var dataTable = new DataTable();
57
58        if (string.IsNullOrEmpty(groupingVariableName)) {
59          var row = PreprocessingChartContent.CreateDataRow(preprocessingData, variableName, DataRowVisualProperties.DataRowChartType.Histogram);
60          dataTable.Rows.Add(row);
61          return dataTable;
62        }
63
64        dataTable.VisualProperties.Title = variableName;
65
66        int variableIndex = preprocessingData.GetColumnIndex(variableName);
67        var variableValues = preprocessingData.GetValues<double>(variableIndex);
68        int groupVariableIndex = preprocessingData.GetColumnIndex(groupingVariableName);
69        var groupingValues = Enumerable.Empty<string>();
70
71        if (preprocessingData.VariableHasType<double>(groupVariableIndex)) {
72          groupingValues = preprocessingData.GetValues<double>(groupVariableIndex).Select(x => x.ToString());
73        } else if (preprocessingData.VariableHasType<string>(groupVariableIndex)) {
74          groupingValues = preprocessingData.GetValues<string>(groupVariableIndex);
75        } else if (preprocessingData.VariableHasType<DateTime>(groupVariableIndex)) {
76          groupingValues = preprocessingData.GetValues<DateTime>(groupVariableIndex).Select(x => x.ToString());
77        }
78
79        var groups = groupingValues.Zip(variableValues, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2);
80
81        foreach (var group in groups) {
82          var classRow = new DataRow();
83          classRow.Name = group.Key;
84          classRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
85          classRow.VisualProperties.Aggregation = aggregation;
86          classRow.Values.AddRange(group);
87          dataTable.Rows.Add(classRow);
88        }
89        return dataTable;
90      }
91    }
92
93
94  }
95}
Note: See TracBrowser for help on using the repository browser.