以编程方式将系列添加到 XAML 中定义的 OxyPlot

Add series programmatically to an OxyPlot defined in XAML

我正在为用户可以在图表上放置数据的项目测试 OxyPlot。

我尝试像这样以编程方式将系列添加到 OxyPlot,但它不起作用:

<Grid>
    <oxy:Plot x:Name="TestPlot" Title="" AllowDrop="True">

    </oxy:Plot>
</Grid>

On Drop 事件函数:

        OxyPlot.Series.LineSeries ls = new OxyPlot.Series.LineSeries();
        ls.StrokeThickness = 1;

        OxyPlot.Wpf.Axis Xaxis = new OxyPlot.Wpf.LinearAxis();
        Xaxis.Maximum = 0;
        Xaxis.Minimum = 50;
        Xaxis.Position = OxyPlot.Axes.AxisPosition.Bottom;
        Xaxis.Title = "time";
        TestPlot.Axes.Add(Xaxis);

        OxyPlot.Wpf.Axis Yaxis = new OxyPlot.Wpf.LinearAxis();
        Xaxis.Maximum = 0;
        Xaxis.Minimum = 500;
        Xaxis.Position = OxyPlot.Axes.AxisPosition.Left;
        Xaxis.Title = "Value";
        TestPlot.Axes.Add(Yaxis);

        ls.Points.Add(new DataPoint(0, 1));
        ls.Points.Add(new DataPoint(10, 100));
        ls.Points.Add(new DataPoint(20, 200));
        ls.Points.Add(new DataPoint(30, 300));

        TestPlot.ActualModel.Series.Add(ls);

        TestPlot.InvalidatePlot(true);

但是没有显示LineSerie。 我遗漏了一些东西,但无法弄清楚当我用谷歌搜索这个问题时是什么...

感谢您的帮助。

使用 Plot 元素,您应该创建一个 OxyPlot.Wpf.LineSeries 并设置其 ItemsSource:

OxyPlot.Wpf.LineSeries ls = new OxyPlot.Wpf.LineSeries();
ls.StrokeThickness = 1;
...
List<DataPoint> dataPoints = new List<DataPoint>();
dataPoints.Add(new DataPoint(0, 1));
dataPoints.Add(new DataPoint(10, 100));
dataPoints.Add(new DataPoint(20, 200));
dataPoints.Add(new DataPoint(30, 300));
ls.ItemsSource = dataPoints;

TestPlot.Series.Add(ls);