Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing/3.4/Content/ScatterPlotContent.cs @ 15529

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

#2709 merged to stable

File size: 5.0 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
[14186]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10539]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
[15242]22using System;
[10987]23using System.Collections.Generic;
[13502]24using System.Linq;
[10987]25using HeuristicLab.Analysis;
[10539]26using HeuristicLab.Common;
[15242]27using HeuristicLab.Visualization.ChartControlsExtensions;
[10242]28
[10539]29namespace HeuristicLab.DataPreprocessing {
[10709]30
[15242]31  public abstract class ScatterPlotContent : PreprocessingChartContent {
32    public string GroupingVariable { get; set; }
[10252]33
[15242]34    protected ScatterPlotContent(IFilteredPreprocessingData preprocessingData)
[10992]35      : base(preprocessingData) {
[10252]36    }
37
[15242]38    protected ScatterPlotContent(ScatterPlotContent content, Cloner cloner)
[10539]39      : base(content, cloner) {
[10245]40    }
[11001]41
[15242]42    public static ScatterPlot CreateScatterPlot(IFilteredPreprocessingData preprocessingData, string variableNameX, string variableNameY, string variableNameGroup = "-", LegendOrder legendOrder = LegendOrder.Alphabetically) {
43      ScatterPlot scatterPlot = new ScatterPlot();
[11001]44
[15242]45      IList<double> xValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameX));
46      IList<double> yValues = preprocessingData.GetValues<double>(preprocessingData.GetColumnIndex(variableNameY));
[10245]47
[15242]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      }
[10987]68
69
[15242]70      //No Grouping
71      if (string.IsNullOrEmpty(variableNameGroup) || variableNameGroup == "-") {
72        ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints);
73        scdr.VisualProperties.IsVisibleInLegend = false;
[13502]74        scatterPlot.Rows.Add(scdr);
[15242]75        return scatterPlot;
76      }
[13502]77
[15242]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());
[10987]88      }
[15242]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      }
[10987]105      return scatterPlot;
106    }
[10242]107  }
108}
Note: See TracBrowser for help on using the repository browser.