public static DataTable CreateHistogram(IFilteredPreprocessingData preprocessingData, string variableName, string groupingVariableName, DataTableVisualProperties.DataTableHistogramAggregation aggregation, LegendOrder legendOrder = LegendOrder.Alphabetically) { var dataTable = new DataTable { VisualProperties = { Title = variableName, HistogramAggregation = aggregation }, }; if (string.IsNullOrEmpty(groupingVariableName)) { var row = PreprocessingChartContent.CreateDataRow(preprocessingData, variableName, DataRowVisualProperties.DataRowChartType.Histogram); row.VisualProperties.IsVisibleInLegend = false; dataTable.Rows.Add(row); return(dataTable); } int variableIndex = preprocessingData.GetColumnIndex(variableName); var variableValues = preprocessingData.GetValues <double>(variableIndex); int groupVariableIndex = preprocessingData.GetColumnIndex(groupingVariableName); var groupingValues = Enumerable.Empty <string>(); if (preprocessingData.VariableHasType <double>(groupVariableIndex)) { groupingValues = preprocessingData.GetValues <double>(groupVariableIndex).Select(x => x.ToString()); } else if (preprocessingData.VariableHasType <string>(groupVariableIndex)) { groupingValues = preprocessingData.GetValues <string>(groupVariableIndex); } else if (preprocessingData.VariableHasType <DateTime>(groupVariableIndex)) { groupingValues = preprocessingData.GetValues <DateTime>(groupVariableIndex).Select(x => x.ToString()); } var groups = groupingValues.Zip(variableValues, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2); if (legendOrder == LegendOrder.Alphabetically) { groups = groups.OrderBy(x => x.Key, new NaturalStringComparer()); } foreach (var group in groups) { var classRow = new DataRow { Name = group.Key, VisualProperties = { ChartType = DataRowVisualProperties.DataRowChartType.Histogram, IsVisibleInLegend = !string.IsNullOrEmpty(groupingVariableName) } }; classRow.Values.AddRange(group); dataTable.Rows.Add(classRow); } return(dataTable); }
public static ScatterPlot CreateScatterPlot(IFilteredPreprocessingData preprocessingData, string variableNameX, string variableNameY, string variableNameGroup = "-", LegendOrder legendOrder = LegendOrder.Alphabetically) { ScatterPlot scatterPlot = new ScatterPlot(); IList <double> xValues = preprocessingData.GetValues <double>(preprocessingData.GetColumnIndex(variableNameX)); IList <double> yValues = preprocessingData.GetValues <double>(preprocessingData.GetColumnIndex(variableNameY)); var points = xValues.Zip(yValues, (x, y) => new Point2D <double>(x, y)).ToList(); var validPoints = points.Where(p => !double.IsNaN(p.X) && !double.IsNaN(p.Y) && !double.IsInfinity(p.X) && !double.IsInfinity(p.Y)).ToList(); if (validPoints.Any()) { try { double axisMin, axisMax, axisInterval; ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.X), validPoints.Max(p => p.X), out axisMin, out axisMax, out axisInterval); scatterPlot.VisualProperties.XAxisMinimumAuto = false; scatterPlot.VisualProperties.XAxisMaximumAuto = false; scatterPlot.VisualProperties.XAxisMinimumFixedValue = axisMin; scatterPlot.VisualProperties.XAxisMaximumFixedValue = axisMax; } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval try { double axisMin, axisMax, axisInterval; ChartUtil.CalculateOptimalAxisInterval(validPoints.Min(p => p.Y), validPoints.Max(p => p.Y), out axisMin, out axisMax, out axisInterval); scatterPlot.VisualProperties.YAxisMinimumAuto = false; scatterPlot.VisualProperties.YAxisMaximumAuto = false; scatterPlot.VisualProperties.YAxisMinimumFixedValue = axisMin; scatterPlot.VisualProperties.YAxisMaximumFixedValue = axisMax; } catch (ArgumentOutOfRangeException) { } // error during CalculateOptimalAxisInterval } //No Grouping if (string.IsNullOrEmpty(variableNameGroup) || variableNameGroup == "-") { ScatterPlotDataRow scdr = new ScatterPlotDataRow(variableNameX + " - " + variableNameY, "", validPoints); scdr.VisualProperties.IsVisibleInLegend = false; scatterPlot.Rows.Add(scdr); return(scatterPlot); } //Grouping int groupVariableIndex = preprocessingData.GetColumnIndex(variableNameGroup); var groupingValues = Enumerable.Empty <string>(); if (preprocessingData.VariableHasType <double>(groupVariableIndex)) { groupingValues = preprocessingData.GetValues <double>(groupVariableIndex).Select(x => x.ToString()); } else if (preprocessingData.VariableHasType <string>(groupVariableIndex)) { groupingValues = preprocessingData.GetValues <string>(groupVariableIndex); } else if (preprocessingData.VariableHasType <DateTime>(groupVariableIndex)) { groupingValues = preprocessingData.GetValues <DateTime>(groupVariableIndex).Select(x => x.ToString()); } var groups = groupingValues.Zip(validPoints, Tuple.Create).GroupBy(t => t.Item1, t => t.Item2); if (legendOrder == LegendOrder.Alphabetically) { groups = groups.OrderBy(x => x.Key, new NaturalStringComparer()); } foreach (var group in groups) { var scdr = new ScatterPlotDataRow { Name = group.Key, VisualProperties = { IsVisibleInLegend = true, PointSize = 6 } }; scdr.Points.AddRange(group); scatterPlot.Rows.Add(scdr); } return(scatterPlot); }