Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10981 was 10981, checked in by psteiner, 8 years ago

Zoom in DataCompletenessChart

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   
46    private void InitData()
47    {
48      IDictionary<int, IList<int>> missingValueIndices = Content.SearchLogic.GetMissingValueIndices();
49      for (int i = 0; i < Content.DataGridLogic.Columns; i++)
50      {
51        //append column
52        List<bool> column = new List<bool>();
53        for (int j=0; j < Content.DataGridLogic.Rows; j++) {
54          column.Add(missingValueIndices[i].Contains(j));
55        }
56        matrix.Add(column);
57      }
58      List<List<int>> yValuesPerColumn = ProcessMatrixForCharting(matrix, missingValueIndices);
59      PrepareChart();
60      CreateSeries(yValuesPerColumn);
61    }
62
63    private void PrepareChart()
64    {
65      chart.Titles.Add("DataCompletenessChart");
66      chart.EnableDoubleClickResetsZoom = true;
67      chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
68      chart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
69      chart.ChartAreas[0].AxisX.IsMarginVisible = false;
70      chart.ChartAreas[0].AxisY.IsMarginVisible = false;
71      chart.ChartAreas[0].CursorX.IsUserEnabled = true;
72      chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
73      chart.ChartAreas[0].CursorY.IsUserEnabled = true;
74      chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
75      //custom x axis label
76      double from = 0.5;
77      foreach (String columnName in Content.DataGridLogic.ColumnNames)
78      {
79        double to = from + 1;
80        chart.ChartAreas[0].AxisX.CustomLabels.Add(from, to, columnName);
81        from = to;
82      }
83      //custom y axis label
84      chart.ChartAreas[0].AxisY.IsReversed = true;
85    }
86
87
88
89    private void CreateSeries(List<List<int>> yValuesPerColumn)
90    {
91      //prepare series
92      int seriesCount = DetermineSeriesCount(yValuesPerColumn);
93      for (int i = 0; i < seriesCount; i++)
94      {
95        chart.Series.Add(CreateSeriesName(i));
96        Series series = chart.Series[CreateSeriesName(i)];
97        series.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
98        series.IsVisibleInLegend = false;
99        series["PointWidth"] = "1.0";
100        if (i % 2 == 0)
101        {
102          if (i == 0) //show legend for non missing values only once
103            series.IsVisibleInLegend = true;
104          series.Color = colorNonMissingVal;
105        }
106        else
107        {
108          if (i == 1) //show legend for missing values only once
109            series.IsVisibleInLegend = true;
110          series.Color = colorMissingVal;
111        }
112      }
113      //fill series
114      for (int i = 0; i < yValuesPerColumn.Count; i++)
115      {
116        List<int> column = yValuesPerColumn[i];
117        for (int j = 0; j < seriesCount; j++) {
118          if (column.Count - 1 < j) {
119            chart.Series[CreateSeriesName(j)].Points.AddY(0);
120          } else {
121            chart.Series[CreateSeriesName(j)].Points.AddY(column[j]);
122          }
123        }
124      }
125    }
126
127    private String CreateSeriesName(int index)
128    {
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    {
139      int highest = 0;
140      foreach (List<int> values in yValuesPerColumn) {
141        highest = Math.Max(values.Count, highest);
142      }
143      return highest;
144    }
145
146    private List<List<int>> ProcessMatrixForCharting(List<List<bool>> matrix, IDictionary<int, IList<int>> missingValueIndices)
147    {
148      List<List<int>> columnsYValues = new List<List<int>>();
149      for (int i=0; i < matrix.Count; i++) //column
150      {
151        List<int> yValues = new List<int>();
152        List<bool> column = matrix[i];
153        bool missingState = false;
154        int valueCount = 0;
155        for (int j = 0; j < column.Count; j++ ) {
156          if (missingState == missingValueIndices[i].Contains(j))
157          {
158            valueCount++;
159          }
160          else
161          {
162            yValues.Add(valueCount);
163            valueCount = 1;
164            missingState = !missingState;
165          }
166        }
167        yValues.Add(valueCount);
168        if (missingState) //handle last missing
169        {
170          yValues.Add(0);
171        }
172        //yValues.Reverse();
173        columnsYValues.Add(yValues);
174      }
175      return columnsYValues;
176    }
177    #endregion
178  }
179}
Note: See TracBrowser for help on using the repository browser.