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
RevLine 
[10539]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
[10544]22using System;
[10244]23using System.Collections.Generic;
[10847]24using System.Drawing;
[10377]25using HeuristicLab.Analysis;
[10628]26using HeuristicLab.Core;
27using HeuristicLab.Data;
[10244]28
[10539]29namespace HeuristicLab.DataPreprocessing {
[10658]30
31  public class ChartLogic : IChartLogic {
[10628]32 
[10586]33    private ITransactionalPreprocessingData preprocessingData;
[10377]34
[10658]35    public ChartLogic(ITransactionalPreprocessingData preprocessingData) {
[10377]36      this.preprocessingData = preprocessingData;
37    }
38
[10847]39    #region IChartLogic Members
40
[10658]41    public DataRow CreateDataRow(string variableName, DataRowVisualProperties.DataRowChartType chartType) {
[10811]42      IList<double> values = preprocessingData.GetValues<double>(variableName);
[10628]43      DataRow row = new DataRow(variableName, "", values);
[10658]44      row.VisualProperties.ChartType = chartType;
[10628]45      return row;
[10377]46    }
[10382]47
[10803]48    public DataRow CreateDataRowRange(string variableName,int start, int end, DataRowVisualProperties.DataRowChartType chartType) {
[10811]49      IList<double> values = preprocessingData.GetValues<double>(variableName);
[10803]50      IList<double> valuesRange = new List<double>();
[10736]51      for (int i = 0; i < values.Count; i++) {
[10803]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++) {
[10736]65        if (Double.IsNaN(values[i]))
66          values[i] = 0;
67      }
68    }
69
70
71
[10628]72    private IEnumerable<string> GetVariableNames() {
[10552]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;
[10382]82    }
83
[10628]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);
[10382]88      }
[10628]89      return new ReadOnlyCheckedItemList<StringValue>(itemList);
[10382]90    }
91
[10573]92    public event DataPreprocessingChangedEventHandler Changed {
93      add { preprocessingData.Changed += value; }
94      remove { preprocessingData.Changed -= value; }
95    }
[10628]96
[10847]97    public event EventHandler SelectionChanged {
98      add { preprocessingData.SelectionChanged += value; }
99      remove { preprocessingData.SelectionChanged -= value; }
100    }
101
[10628]102    public string GetVariableNameByIndex(int index) {
103      return preprocessingData.GetVariableName(index);
104    }
105
[10733]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
[10847]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
[10244]148  }
149}
Note: See TracBrowser for help on using the repository browser.