Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2865-Tourplanning/HeuristicLab.DataPreprocessing/3.4/Content/ScatterPlotContent.cs @ 15529

Last change on this file since 15529 was 15518, checked in by pfleck, 7 years ago

#2809: merged branch to trunk

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