Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing/3.4/Content/ScatterPlotContent.cs @ 14725

Last change on this file since 14725 was 14725, checked in by mkommend, 7 years ago

#2709: Added grouping for multi scatter plot view.

File size: 4.8 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.Linq;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Visualization.ChartControlsExtensions;
28
29namespace HeuristicLab.DataPreprocessing {
30
31  public abstract class ScatterPlotContent : PreprocessingChartContent {
32    public string GroupingVariable { get; set; }
33
34    protected ScatterPlotContent(IFilteredPreprocessingData preprocessingData)
35      : base(preprocessingData) {
36    }
37
38    protected ScatterPlotContent(ScatterPlotContent content, Cloner cloner)
39      : base(content, cloner) {
40    }
41
42    public static ScatterPlot CreateScatterPlot(IFilteredPreprocessingData preprocessingData, string variableNameX, string variableNameY, string variableNameGroup = "-") {
43      ScatterPlot scatterPlot = new ScatterPlot();
44
45      IList<double> xValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameX));
46      IList<double> yValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameY));
47
48      var points = xValues.Zip(yValues, (x, y) => new Point2D<double>(x, y)).ToList();
49      var validPoints = points.Where(p => !double.IsNaN(p.X) && !double.IsNaN(p.Y) && !double.IsInfinity(p.X) && !double.IsInfinity(p.Y)).ToList();
50      if (validPoints.Any()) {
51        try {
52          double axisMin, axisMax, axisInterval;
53          ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.X), validPoints.Max(p => p.X), out axisMin, out axisMax, out axisInterval);
54          scatterPlot.VisualProperties.XAxisMinimumAuto = false;
55          scatterPlot.VisualProperties.XAxisMaximumAuto = false;
56          scatterPlot.VisualProperties.XAxisMinimumFixedValue = axisMin;
57          scatterPlot.VisualProperties.XAxisMaximumFixedValue = axisMax;
58        }
59        catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
60        try {
61          double axisMin, axisMax, axisInterval;
62          ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.Y), validPoints.Max(p => p.Y), out axisMin, out axisMax, out axisInterval);
63          scatterPlot.VisualProperties.YAxisMinimumAuto = false;
64          scatterPlot.VisualProperties.YAxisMaximumAuto = false;
65          scatterPlot.VisualProperties.YAxisMinimumFixedValue = axisMin;
66          scatterPlot.VisualProperties.YAxisMaximumFixedValue = axisMax;
67        }
68        catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
69      }
70
71
72      //No Grouping
73      if (string.IsNullOrEmpty(variableNameGroup) || variableNameGroup == "-") {
74        ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints);
75        scdr.VisualProperties.IsVisibleInLegend = false;
76        scatterPlot.Rows.Add(scdr);
77        return scatterPlot;
78      }
79
80      //Grouping
81      int groupVariableIndex = preprocessingData.GetColumnIndex(variableNameGroup);
82      var groupingValues = Enumerable.Empty<string>();
83
84      if (preprocessingData.VariableHasType<double>(groupVariableIndex)) {
85        groupingValues = preprocessingData.GetValues<double>(groupVariableIndex).Select(x => x.ToString());
86      } else if (preprocessingData.VariableHasType<string>(groupVariableIndex)) {
87        groupingValues = preprocessingData.GetValues<string>(groupVariableIndex);
88      } else if (preprocessingData.VariableHasType<DateTime>(groupVariableIndex)) {
89        groupingValues = preprocessingData.GetValues<DateTime>(groupVariableIndex).Select(x => x.ToString());
90      }
91      var groups = groupingValues.Zip(validPoints, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2);
92
93      foreach (var group in groups) {
94        var scdr = new ScatterPlotDataRow();
95        scdr.Name = group.Key;
96        scdr.VisualProperties.IsVisibleInLegend = true;
97        scdr.VisualProperties.PointSize = 6;
98        scdr.Points.AddRange(group);
99        scatterPlot.Rows.Add(scdr);
100      }
101      return scatterPlot;
102    }
103
104  }
105}
Note: See TracBrowser for help on using the repository browser.