Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10884 was 10882, checked in by aesterer, 11 years ago

Added scatter plot view

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