【问题标题】:Oxyplot- WPF: Adding data from a List of double to DataPointOxyplot- WPF:将数据从双重列表添加到数据点
【发布时间】:2018-06-16 22:26:31
【问题描述】:

我是 WPF 新手,我正在从事的项目要求我在 XY 图表上绘制一个双精度列表。我将 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; }
    }
}

【问题讨论】:

  • 已编辑:请问如何将 XValues 列表和 YValues 列表传入 DataPoint,它一次只接受 x 和 y 的单个值?
  • 您需要将两个双精度列表投影/合并到一个对象列表中(例如DataPoint)。

标签: wpf charts oxyplot


【解决方案1】:

我真的不明白为什么您不能直接存储 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 2017-10-06
    相关资源
    最近更新 更多