Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing/3.4/Content/ScatterPlotContent.cs @ 15401

Last change on this file since 15401 was 15210, checked in by pfleck, 7 years ago

#2709 Fixed some small issues and some default behavior.

File size: 5.0 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 = "-", LegendOrder legendOrder = LegendOrder.Alphabetically) {
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        } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
59        try {
60          double axisMin, axisMax, axisInterval;
61          ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.Y), validPoints.Max(p => p.Y), out axisMin, out axisMax, out axisInterval);
62          scatterPlot.VisualProperties.YAxisMinimumAuto = false;
63          scatterPlot.VisualProperties.YAxisMaximumAuto = false;
64          scatterPlot.VisualProperties.YAxisMinimumFixedValue = axisMin;
65          scatterPlot.VisualProperties.YAxisMaximumFixedValue = axisMax;
66        } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval
67      }
68
69
70      //No Grouping
71      if (string.IsNullOrEmpty(variableNameGroup) || variableNameGroup == "-") {
72        ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints);
73        scdr.VisualProperties.IsVisibleInLegend = false;
74        scatterPlot.Rows.Add(scdr);
75        return scatterPlot;
76      }
77
78      //Grouping
79      int groupVariableIndex = preprocessingData.GetColumnIndex(variableNameGroup);
80      var groupingValues = Enumerable.Empty<string>();
81
82      if (preprocessingData.VariableHasType<double>(groupVariableIndex)) {
83        groupingValues = preprocessingData.GetValues<double>(groupVariableIndex).Select(x => x.ToString());
84      } else if (preprocessingData.VariableHasType<string>(groupVariableIndex)) {
85        groupingValues = preprocessingData.GetValues<string>(groupVariableIndex);
86      } else if (preprocessingData.VariableHasType<DateTime>(groupVariableIndex)) {
87        groupingValues = preprocessingData.GetValues<DateTime>(groupVariableIndex).Select(x => x.ToString());
88      }
89      var groups = groupingValues.Zip(validPoints, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2);
90
91      if (legendOrder == LegendOrder.Alphabetically)
92        groups = groups.OrderBy(x => x.Key, new NaturalStringComparer());
93
94      foreach (var group in groups) {
95        var scdr = new ScatterPlotDataRow {
96          Name = group.Key,
97          VisualProperties = {
98            IsVisibleInLegend = true,
99            PointSize = 6
100          }
101        };
102        scdr.Points.AddRange(group);
103        scatterPlot.Rows.Add(scdr);
104      }
105      return scatterPlot;
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.