【问题标题】:Add series programmatically to an OxyPlot defined in XAML以编程方式将系列添加到 XAML 中定义的 OxyPlot
【发布时间】:2020-02-05 22:33:26
【问题描述】:

我正在为一个用户可以在图表上放置数据的项目测试 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。 我错过了一些东西,但是当我用谷歌搜索这个问题时无法弄清楚是什么......

感谢您的帮助。

【问题讨论】:

  • 是否触发了drop事件?
  • 是的,它被触发了

标签: c# wpf oxyplot


【解决方案1】:

使用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);

【讨论】:

    猜你喜欢
    • 2016-05-16
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    相关资源
    最近更新 更多