source: stable/HeuristicLab.DataPreprocessing.Views/3.4/DataCompletenessView.cs @ 14075

Last change on this file since 14075 was 14075, checked in by mkommend, 5 years ago

#2559: Merged r13502, r13504, r13507, r13508, r13512, r13514, r13517 into stable.

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Windows.Forms;
26using System.Windows.Forms.DataVisualization.Charting;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.DataPreprocessing.Views {
31
32  [View("Histogram View")]
33  [Content(typeof(DataCompletenessChartContent), true)]
34  public partial class DataCompletenessView : ItemView {
35
36    //list of columns, bool indicates wether the cell is a missing value or not
37    private List<List<bool>> matrix = new List<List<bool>>();
38    //series colors
39    private static Color colorNonMissingVal = Color.CornflowerBlue;
40    private static Color colorMissingVal = Color.Orange;
41
42    public new DataCompletenessChartContent Content {
43      get { return (DataCompletenessChartContent)base.Content; }
44      set { base.Content = value; }
45    }
46
47
48    public DataCompletenessView() {
49      InitializeComponent();
50    }
51
52    protected override void OnContentChanged() {
53      base.OnContentChanged();
54      if (Content != null) {
55        InitData();
56      }
57    }
58
59    private void InitData() {
60      IDictionary<int, IList<int>> missingValueIndices = Content.SearchLogic.GetMissingValueIndices();
61      for (int i = 0; i < Content.SearchLogic.Columns; i++) {
62        //append column
63        List<bool> column = new List<bool>();
64        for (int j = 0; j < Content.SearchLogic.Rows; j++) {
65          column.Add(missingValueIndices[i].Contains(j));
66        }
67        matrix.Add(column);
68      }
69      List<List<int>> yValuesPerColumn = ProcessMatrixForCharting(matrix, missingValueIndices);
70      PrepareChart();
71      CreateSeries(yValuesPerColumn);
72    }
73
74    private void PrepareChart() {
75      chart.Titles.Add("DataCompletenessChart");
76      chart.EnableDoubleClickResetsZoom = true;
77      chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
78      chart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
79      chart.ChartAreas[0].AxisX.IsMarginVisible = false;
80      chart.ChartAreas[0].AxisY.IsMarginVisible = false;
81      chart.ChartAreas[0].CursorX.IsUserEnabled = true;
82      chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
83      chart.ChartAreas[0].CursorY.IsUserEnabled = true;
84      chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
85      //custom x axis label
86      double from = 0.5;
87      foreach (String columnName in Content.SearchLogic.VariableNames) {
88        double to = from + 1;
89        chart.ChartAreas[0].AxisX.CustomLabels.Add(from, to, columnName);
90        from = to;
91      }
92      //custom y axis label
93      chart.ChartAreas[0].AxisY.IsReversed = true;
94    }
95
96    private void CreateSeries(List<List<int>> yValuesPerColumn) {
97      //prepare series
98      int seriesCount = DetermineSeriesCount(yValuesPerColumn);
99      for (int i = 0; i < seriesCount; i++) {
100        chart.Series.Add(CreateSeriesName(i));
101        Series series = chart.Series[CreateSeriesName(i)];
102        series.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
103        series.IsVisibleInLegend = false;
104        series["PointWidth"] = "1.0";
105        if (i % 2 == 0) {
106          if (i == 0) //show legend for non missing values only once
107            series.IsVisibleInLegend = true;
108          series.Color = colorNonMissingVal;
109        } else {
110          if (i == 1) //show legend for missing values only once
111            series.IsVisibleInLegend = true;
112          series.Color = colorMissingVal;
113        }
114      }
115      //fill series
116      for (int i = 0; i < yValuesPerColumn.Count; i++) {
117        List<int> column = yValuesPerColumn[i];
118        for (int j = 0; j < seriesCount; j++) {
119          if (column.Count - 1 < j) {
120            chart.Series[CreateSeriesName(j)].Points.AddY(0);
121          } else {
122            chart.Series[CreateSeriesName(j)].Points.AddY(column[j]);
123          }
124        }
125      }
126    }
127
128    private String CreateSeriesName(int index) {
129      if (index == 0)
130        return "non-missing value";
131      else if (index == 1)
132        return "missing value";
133      return "series" + index;
134    }
135
136    #region data_preparation_for_chartseries
137    private int DetermineSeriesCount(List<List<int>> yValuesPerColumn) {
138      int highest = 0;
139      foreach (List<int> values in yValuesPerColumn) {
140        highest = Math.Max(values.Count, highest);
141      }
142      return highest;
143    }
144
145    private List<List<int>> ProcessMatrixForCharting(List<List<bool>> matrix, IDictionary<int, IList<int>> missingValueIndices) {
146      List<List<int>> columnsYValues = new List<List<int>>();
147      for (int i = 0; i < matrix.Count; i++) //column
148      {
149        List<int> yValues = new List<int>();
150        List<bool> column = matrix[i];
151        bool missingState = false;
152        int valueCount = 0;
153        for (int j = 0; j < column.Count; j++) {
154          if (missingState == missingValueIndices[i].Contains(j)) {
155            valueCount++;
156          } else {
157            yValues.Add(valueCount);
158            valueCount = 1;
159            missingState = !missingState;
160          }
161        }
162        yValues.Add(valueCount);
163        if (missingState) //handle last missing
164        {
165          yValues.Add(0);
166        }
167        //yValues.Reverse();
168        columnsYValues.Add(yValues);
169      }
170      return columnsYValues;
171    }
172    #endregion
173  }
174}
Note: See TracBrowser for help on using the repository browser.