Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10867 was 10867, checked in by mleitner, 8 years ago

Add colors to histogram / remove all in one mode

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