Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10979 was 10979, checked in by psteiner, 10 years ago

datacompleteness view
formating filter percentage

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