Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.DataPreprocessing.Views/3.4/DataCompletenessView.cs @ 17111

Last change on this file since 17111 was 11002, checked in by mleitner, 10 years ago

Refactoring

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