Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing/3.4/Content/HistogramContent.cs @ 15344

Last change on this file since 15344 was 15210, checked in by pfleck, 7 years ago

#2709 Fixed some small issues and some default behavior.

File size: 4.1 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 LegendOrder Order { get; set; }
42
43    public HistogramContent(IFilteredPreprocessingData preprocessingData)
44      : base(preprocessingData) {
45      Bins = 10;
46      ExactBins = false;
47    }
48
49    public HistogramContent(HistogramContent content, Cloner cloner)
50      : base(content, cloner) {
51    }
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new HistogramContent(this, cloner);
54    }
55
56    public static DataTable CreateHistogram(IFilteredPreprocessingData preprocessingData, string variableName, string groupingVariableName, DataTableVisualProperties.DataTableHistogramAggregation aggregation, LegendOrder legendOrder = LegendOrder.Alphabetically) {
57      var dataTable = new DataTable {
58        VisualProperties = { Title = variableName, HistogramAggregation = aggregation },
59      };
60
61      if (string.IsNullOrEmpty(groupingVariableName)) {
62        var row = PreprocessingChartContent.CreateDataRow(preprocessingData, variableName, DataRowVisualProperties.DataRowChartType.Histogram);
63        row.VisualProperties.IsVisibleInLegend = false;
64        dataTable.Rows.Add(row);
65        return dataTable;
66      }
67
68      int variableIndex = preprocessingData.GetColumnIndex(variableName);
69      var variableValues = preprocessingData.GetValues<double>(variableIndex);
70      int groupVariableIndex = preprocessingData.GetColumnIndex(groupingVariableName);
71      var groupingValues = Enumerable.Empty<string>();
72
73      if (preprocessingData.VariableHasType<double>(groupVariableIndex)) {
74        groupingValues = preprocessingData.GetValues<double>(groupVariableIndex).Select(x => x.ToString());
75      } else if (preprocessingData.VariableHasType<string>(groupVariableIndex)) {
76        groupingValues = preprocessingData.GetValues<string>(groupVariableIndex);
77      } else if (preprocessingData.VariableHasType<DateTime>(groupVariableIndex)) {
78        groupingValues = preprocessingData.GetValues<DateTime>(groupVariableIndex).Select(x => x.ToString());
79      }
80
81      var groups = groupingValues.Zip(variableValues, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2);
82
83      if (legendOrder == LegendOrder.Alphabetically)
84        groups = groups.OrderBy(x => x.Key, new NaturalStringComparer());
85
86      foreach (var group in groups) {
87        var classRow = new DataRow {
88          Name = group.Key,
89          VisualProperties = {
90              ChartType = DataRowVisualProperties.DataRowChartType.Histogram,
91              IsVisibleInLegend = !string.IsNullOrEmpty(groupingVariableName)
92            }
93        };
94        classRow.Values.AddRange(group);
95        dataTable.Rows.Add(classRow);
96      }
97      return dataTable;
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.