Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/18/17 11:17:19 (8 years ago)
Author:
mkommend
Message:

#2709: Refactored histogram view and content to support grouping by string and datetime variables.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/HistogramView.cs

    r14474 r14579  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    2524using HeuristicLab.Analysis;
     
    2726
    2827namespace HeuristicLab.DataPreprocessing.Views {
    29 
    3028  [View("Histogram View")]
    3129  [Content(typeof(HistogramContent), true)]
    3230  public partial class HistogramView : PreprocessingChartView {
    33 
    34     public List<double> Classification { get; set; }
    35     public bool IsDetailedChartViewEnabled { get; set; }
     31    public new HistogramContent Content {
     32      get { return (HistogramContent)base.Content; }
     33      set { base.Content = value; }
     34    }
    3635
    3736    public HistogramView() {
     
    4140    protected override void OnContentChanged() {
    4241      base.OnContentChanged();
     42      classifierComboBox.Items.Clear();
     43      classifierComboBox.Items.Add("");
     44
    4345      if (Content != null) {
    44         classifierComboBox.Items.Clear();
    45         classifierComboBox.Items.Add("None");
    46 
    47         foreach (string var in Content.GetVariableNamesForHistogramClassification()) {
     46        foreach (string var in Content.GetVariableNamesForGrouping()) {
    4847          classifierComboBox.Items.Add(var);
    4948        }
    5049
    51         if (classifierComboBox.SelectedItem == null && Content.ClassifierVariableIndex < classifierComboBox.Items.Count) {
    52           classifierComboBox.SelectedIndex = Content.ClassifierVariableIndex;
    53         }
     50        classifierComboBox.SelectedItem = Content.GroupingVariableName;
    5451      }
    5552    }
    5653
    5754    protected override DataTable CreateDataTable(string variableName) {
    58       var dt = new DataTable();
    59       var row = Content.CreateDataRow(variableName, DataRowVisualProperties.DataRowChartType.Histogram);
    60       if (Classification == null) {
    61         dt.Rows.Add(row);
    62       } else {
    63         dt.VisualProperties.Title = variableName;
    64         var valuesPerClass = row.Values.Zip(Classification, (value, @class) => new { value, @class })
    65                                 .GroupBy(x => x.@class)
    66                                 .ToDictionary(x => x.Key, x => x.Select(v => v.value));
    67         foreach (var entry in valuesPerClass) {
    68           var classRow = new DataRow(string.Format("{0} ({1})", classifierComboBox.SelectedItem, entry.Key));
    69           classRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
    70           classRow.Values.AddRange(entry.Value);
    71           dt.Rows.Add(classRow);
    72         }
     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;
    7361      }
    74       return dt;
     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;
    7588    }
    7689
    77     public new HistogramContent Content {
    78       get { return (HistogramContent)base.Content; }
    79       set { base.Content = value; }
    80     }
    8190
    8291    private void classifierComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    83       if (classifierComboBox.SelectedItem == null)
    84         return;
    85 
    86       if (classifierComboBox.SelectedIndex != 0) {
    87         int columndIndex = Content.PreprocessingData.GetColumnIndex(classifierComboBox.SelectedItem.ToString());
    88         Classification = Content.PreprocessingData.GetValues<double>(columndIndex).ToList();
    89       } else {
    90         Classification = null;
    91       }
    92 
    93       Content.ClassifierVariableIndex = classifierComboBox.SelectedIndex;
     92      Content.GroupingVariableName = classifierComboBox.SelectedItem.ToString();
    9493
    9594      // rebuild datatables
Note: See TracChangeset for help on using the changeset viewer.