source: trunk/sources/HeuristicLab.DataPreprocessing.Views/3.4/DataCompletenessView.cs @ 15110

Last change on this file since 15110 was 15110, checked in by pfleck, 2 years ago

#2709: merged branch to trunk

File size: 5.4 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.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms.DataVisualization.Charting;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.DataPreprocessing.Views {
31  [View("Histogram View")]
32  [Content(typeof(DataCompletenessChartContent), true)]
33  public partial class DataCompletenessView : ItemView {
34    //series colors
35    private static readonly Color colorNonMissingValue = Color.CornflowerBlue;
36    private static readonly Color colorMissingValue = Color.Orange;
37
38    public new DataCompletenessChartContent Content {
39      get { return (DataCompletenessChartContent)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public DataCompletenessView() {
44      InitializeComponent();
45    }
46
47    protected override void OnContentChanged() {
48      base.OnContentChanged();
49      if (Content == null) return;
50      InitData();
51    }
52
53    private void InitData() {
54      IDictionary<int, IList<int>> missingValueIndices = Content.SearchLogic.GetMissingValueIndices();
55
56      bool[,] valueMissing = new bool[Content.SearchLogic.Rows, Content.SearchLogic.Columns];
57      foreach (var columnMissingValues in missingValueIndices) {
58        var column = columnMissingValues.Key;
59        foreach (var missingValueIndex in columnMissingValues.Value)
60          valueMissing[missingValueIndex, column] = true;
61      }
62
63      var yValuesPerColumn = ProcessMatrixForCharting(valueMissing);
64      PrepareChart();
65      CreateSeries(yValuesPerColumn);
66    }
67
68    private void PrepareChart() {
69      chart.EnableDoubleClickResetsZoom = true;
70      chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
71      chart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
72      chart.ChartAreas[0].AxisX.IsMarginVisible = false;
73      chart.ChartAreas[0].AxisY.IsMarginVisible = false;
74      chart.ChartAreas[0].CursorX.IsUserEnabled = true;
75      chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
76      chart.ChartAreas[0].CursorY.IsUserEnabled = true;
77      chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
78      //custom x axis label
79      double from = 0.5;
80      foreach (String columnName in Content.SearchLogic.VariableNames) {
81        double to = from + 1;
82        chart.ChartAreas[0].AxisX.CustomLabels.Add(from, to, columnName);
83        from = to;
84      }
85      //custom y axis label
86      chart.ChartAreas[0].AxisY.IsReversed = true;
87    }
88
89    private void CreateSeries(List<List<int>> yValuesPerColumn) {
90      chart.Series.SuspendUpdates();
91      //prepare series
92      int seriesCount = DetermineSeriesCount(yValuesPerColumn);
93      for (int i = 0; i < seriesCount; i++) {
94        Series series = new Series(CreateSeriesName(i));
95        series.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
96        series.IsVisibleInLegend = false;
97        series["PointWidth"] = "1.0";
98        series.IsVisibleInLegend = i < 2; //only first two series are visible, non-missing and missing values
99        series.Color = i % 2 == 0 ? colorNonMissingValue : colorMissingValue;
100
101        var values = yValuesPerColumn.Select(y => i < y.Count ? y[i] : 0).ToArray();
102        series.Points.DataBindY(values);
103        chart.Series.Add(series);
104      }
105      chart.Series.ResumeUpdates();
106    }
107
108    private String CreateSeriesName(int index) {
109      if (index == 0)
110        return "non-missing value";
111      else if (index == 1)
112        return "missing value";
113      return "series" + index;
114    }
115
116    #region data_preparation_for_chartseries
117    private int DetermineSeriesCount(List<List<int>> yValuesPerColumn) {
118      return yValuesPerColumn.Max(values => values.Count);
119    }
120
121    private List<List<int>> ProcessMatrixForCharting(bool[,] matrix) {
122      var columnsYValues = new List<List<int>>();
123      for (int column = 0; column < matrix.GetLength(1); column++) {
124        var yValues = new List<int>();
125        bool missingState = false;
126        int valueCount = 0;
127
128        for (int row = 0; row < matrix.GetLength(0); row++) {
129          if (missingState == matrix[row, column]) {
130            valueCount++;
131          } else {
132            yValues.Add(valueCount);
133            valueCount = 1;
134            missingState = !missingState;
135          }
136        }
137        yValues.Add(valueCount);
138        if (missingState) //handle last missing
139        {
140          yValues.Add(0);
141        }
142        columnsYValues.Add(yValues);
143      }
144      return columnsYValues;
145    }
146    #endregion
147  }
148}
Note: See TracBrowser for help on using the repository browser.