/// <summary> /// Extracts the points from collection. /// </summary> /// <param name="dataSource">The data source (can be <see langword="null"/>).</param> /// <param name="xPath">The binding path for the x value.</param> /// <param name="yPath">The binding path for the y value.</param> /// <param name="xLabels"> /// The collection of text labels used for x values. Can be <see langword="null"/>. This /// parameter is only relevant when one chart axis shows text labels and the data source /// contains <see cref="String"/> values instead of <see cref="Double"/> values. /// </param> /// <param name="yLabels"> /// The collection of text labels used for y values. Can be <see langword="null"/>. This /// parameter is only relevant when one chart axis shows text labels and the data source /// contains <see cref="String"/> values instead of <see cref="Double"/> values. /// </param> /// <param name="culture"> /// The <see cref="CultureInfo"/> object that provides culture-specific formatting /// information. /// </param> /// <returns>The chart data as a list of data points.</returns> private static IList<DataPoint> ExtractPointsFromCollection( IEnumerable dataSource, PropertyPath xPath, PropertyPath yPath, CultureInfo culture, IList<TextLabel> xLabels, IList<TextLabel> yLabels) { var xValueExtractor = new DoubleValueExtractor { Collection = dataSource, Culture = culture, TextLabels = xLabels, ValuePath = xPath, }; var xValues = xValueExtractor.Extract(); var yValueExtractor = new DoubleValueExtractor { Collection = dataSource, Culture = culture, TextLabels = yLabels, ValuePath = yPath, }; var yValues = yValueExtractor.Extract(); Debug.Assert(xValues.Count == yValues.Count, "Number of x values in data source does not match the number of y values?!"); int index = 0; var chartDataSource = new DataPointCollection(); foreach (var data in dataSource) { chartDataSource.Add(new DataPoint(xValues[index], yValues[index], data)); index++; } return chartDataSource; }
public IList <DataPoint> ToChartDataSource() { if (XValues == null) { throw new InvalidOperationException("XValues is missing."); } if (YValues == null) { throw new InvalidOperationException("XValues is missing"); } // Extract x values from XValues collection. var xValueExtractor = new DoubleValueExtractor { Collection = XValues, Culture = Culture, ValuePath = XValuePath, }; var xValues = xValueExtractor.Extract(); // Extract y values from YValues collection. var yValueExtractor = new DoubleValueExtractor { Collection = YValues, Culture = Culture, ValuePath = YValuePath, }; var yValues = yValueExtractor.Extract(); Debug.Assert(xValues.Count == yValues.Count, "Number of x values in data source does not match the number of y values?!"); var chartDataSource = new DataPointCollection(); int index = 0; int numberOfDataPoints = Math.Min(xValues.Count, yValues.Count); var xValuesEnumerator = XValues.GetEnumerator(); var yValuesEnumerator = YValues.GetEnumerator(); while (index < numberOfDataPoints) { xValuesEnumerator.MoveNext(); yValuesEnumerator.MoveNext(); var dataPoint = new DataPoint { X = xValues[index], Y = yValues[index], DataContext = new CompositeData(xValuesEnumerator.Current, yValuesEnumerator.Current) }; chartDataSource.Add(dataPoint); index++; } return(chartDataSource); }