如何在 WPF UI 框架上使用 OxyPlot 库绘制 LineSeries

How to plot LineSeries using the OxyPlot library on the WPF UI framework

我正在尝试使用 OxyPlot 绘制我的数据。我想在我的图表中绘制两条线,但我不确定如何为图表提供数据。 这是我的 XAML 文件的样子:

<oxy:Plot Name="Plot1">
  <oxy:Plot.Series>
    <oxy:LineSeries ItemsSource="{Binding Graph1}"/>
    <oxy:LineSeries ItemsSource="{Binding Graph2}"/>
  </oxy:Plot.Series>
</oxy:Plot>

我的问题是如何在使用 LineSeries 时在同一张图中绘制两条线?

通常您不会将 LineSeries 直接添加到 PlotView,而是添加到 PlotModel,然后将其绑定到 Plot View。

C# 代码可能如下所示:

        PlotModel pm = new PlotModel();

        var s1 = new LineSeries();

        for (int i = 0; i < 1000; i++)
        {
            double x = Math.PI * 10 * i / (1000 - 1);
            s1.Points.Add(new DataPoint(x, Math.Sin(x)));
        }

        pm.Series.Add(s1);

        var s2 = new LineSeries();

        for (int i = 0; i < 1000; i++)
        {
            double x = Math.PI * 10 * i / (1000 - 1);
            s2.Points.Add(new DataPoint(x, Math.Cos(x)));
        }

        pm.Series.Add(s2);

        Plot1.Model = pm; 

绑定Plot1当然也可以在XAML中完成。如果您的 DataContext 通过 属性 'MyModel' 提供 PlotModel,它将如下所示:

<oxyplot:PlotView Model="{Binding MyModel}"></oxyplot:PlotView>