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

Last change on this file since 14467 was 14462, checked in by pfleck, 4 years ago

#2709

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