Oxyplot- WPF:将双精度列表中的数据添加到 DataPoint

Oxyplot- WPF: Adding data from a List of double to DataPoint

我是 WPF 的新手,我正在从事的项目要求我在 XY 图表上绘制一个 double 列表。我将 Oxyplot 添加到我的图表项目中,但我在绘制图表时遇到了挑战。

我遵循了 Oxyplot 站点上的示例(参见下面的代码),但我发现 DataPoint 只能接受 x 和 y 的双精度值,而不是数组或双精度列表。

如何绘制 XValues 的 List<double> 和 YValues 的 List<double>

namespace WpfApplication2
{
    using System.Collections.Generic;

    using OxyPlot;

    public class MainViewModel
    {
        public MainViewModel()
        {
            this.Title = "Example 2";
            this.Points = new List<DataPoint>
                              {
                                  new DataPoint(0, 4),
                                  new DataPoint(10, 13),
                                  new DataPoint(20, 15),
                                  new DataPoint(30, 16),
                                  new DataPoint(40, 12),
                                  new DataPoint(50, 12)
                              };
        }

        public string Title { get; private set; }

        public IList<DataPoint> Points { get; private set; }
    }
}

我真的不明白为什么你不能直接存储 DataPoint 列表...但是假设你被 2 个列表困住了,我假设你的列表具有相同的长度(如果不是你有一个问题,因为要绘制的所有点都应具有 X 和 Y 值)。

所以我猜是这样的:

List<double> XValues = new List<double> { 0, 5, 10, 22, 30 };
List<double> YValues = new List<double> { 2, 11, 4, 15, 20 };
for (int i = 0; i < XValues.Count; ++i)
{
  Points.Add(new DataPoint(XValues[i], YValues[i]));
}

这不是很优雅,如果您是创建列表的人,您应该将它们合并到 DataPoint 列表中,就像@PaoloGo 所说的那样。如果你喜欢使用自定义对象以防你不使用 oxyplot,你可以创建一个简单的对象,例如:

public struct ChartPoint
{
    public double X;
    public double Y;
    public ChartPoint(double x, double y)
    {
        X = x;
        Y = y;
    }
}

然后你存储这个:

List<ChartPoint> points;