Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/ChartLogic.cs @ 10847

Last change on this file since 10847 was 10847, checked in by aesterer, 10 years ago

Added coloring feature for line chart

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.Analysis;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.DataPreprocessing {
30
31  public class ChartLogic : IChartLogic {
32 
33    private ITransactionalPreprocessingData preprocessingData;
34
35    public ChartLogic(ITransactionalPreprocessingData preprocessingData) {
36      this.preprocessingData = preprocessingData;
37    }
38
39    #region IChartLogic Members
40
41    public DataRow CreateDataRow(string variableName, DataRowVisualProperties.DataRowChartType chartType) {
42      IList<double> values = preprocessingData.GetValues<double>(variableName);
43      DataRow row = new DataRow(variableName, "", values);
44      row.VisualProperties.ChartType = chartType;
45      return row;
46    }
47
48    public DataRow CreateDataRowRange(string variableName,int start, int end, DataRowVisualProperties.DataRowChartType chartType) {
49      IList<double> values = preprocessingData.GetValues<double>(variableName);
50      IList<double> valuesRange = new List<double>();
51      for (int i = 0; i < values.Count; i++) {
52        if (i >= start && i <= end)
53          valuesRange.Add(values[i]);
54        else
55          valuesRange.Add(Double.NaN);
56      }
57
58      DataRow row = new DataRow(variableName, "", valuesRange);
59      row.VisualProperties.ChartType = chartType;
60      return row;
61    }
62
63    private void ReplaceNANwithZero(IList<double> values) {
64      for (int i = 0; i < values.Count; i++) {
65        if (Double.IsNaN(values[i]))
66          values[i] = 0;
67      }
68    }
69
70
71
72    private IEnumerable<string> GetVariableNames() {
73      List<string> doubleVariableNames = new List<string>();
74
75      //only return variable names from type double
76      foreach (string variableName in preprocessingData.VariableNames) {
77        if (preprocessingData.IsType<double>(preprocessingData.GetColumnIndex(variableName)))
78          doubleVariableNames.Add(variableName);
79      }
80
81      return doubleVariableNames;
82    }
83
84    public ICheckedItemList<StringValue> CreateVariableItemList() {
85      ICheckedItemList<StringValue> itemList = new CheckedItemList<StringValue>();
86      foreach (string name in GetVariableNames()) {
87        itemList.Add(new StringValue(name), true);
88      }
89      return new ReadOnlyCheckedItemList<StringValue>(itemList);
90    }
91
92    public event DataPreprocessingChangedEventHandler Changed {
93      add { preprocessingData.Changed += value; }
94      remove { preprocessingData.Changed -= value; }
95    }
96
97    public event EventHandler SelectionChanged {
98      add { preprocessingData.SelectionChanged += value; }
99      remove { preprocessingData.SelectionChanged -= value; }
100    }
101
102    public string GetVariableNameByIndex(int index) {
103      return preprocessingData.GetVariableName(index);
104    }
105
106
107    public List<DataRow> CreateAllDataRows(DataRowVisualProperties.DataRowChartType chartType) {
108      List<DataRow> dataRows = new List<DataRow>();
109      foreach (var name in GetVariableNames())
110        dataRows.Add(CreateDataRow(name, chartType));
111      return dataRows;
112    }
113
114    public List<DataRow> CreateAllSelectedDataRows(DataRowVisualProperties.DataRowChartType chartType) {
115      List<DataRow> dataRows = new List<DataRow>();
116      foreach (var name in GetVariableNames()) {
117        DataRow row = CreateSelectedDataRow(name, chartType);
118        if(row != null)
119          dataRows.Add(row);
120      }
121      return dataRows;
122    }
123
124    public DataRow CreateSelectedDataRow(string variableName, DataRowVisualProperties.DataRowChartType chartType) {
125     
126      IDictionary<int,IList<int>> selection = preprocessingData.GetSelection();
127      int variableIndex = preprocessingData.GetColumnIndex(variableName);
128
129      if (selection.Keys.Contains(variableIndex))
130      {
131        List<int> selectedIndices = new List<int>(selection[variableIndex]);
132        //need selection with more than 1 value
133        if(selectedIndices.Count < 2)
134          return null;
135
136        int end = selectedIndices[0];      // indices are provided in reverse order
137        int start = selectedIndices[selectedIndices.Count-1];
138
139        DataRow rowSelect = CreateDataRowRange(variableName, start, end, chartType);
140        rowSelect.VisualProperties.Color = Color.Green;
141        return rowSelect;
142      }
143      else
144       return null;
145    }
146
147    #endregion
148  }
149}
Note: See TracBrowser for help on using the repository browser.