问题描述
我正在尝试使用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;
当然也可以在XAML中完成对Plot1的绑定。如果您的DataContext通过属性“ MyModel”提供了PlotModel,则它看起来像这样:
<oxyplot:PlotView Model="{Binding MyModel}"></oxyplot:PlotView>