【问题标题】:OxyPlot WPF get selected data pointOxyPlot WPF 获取选定的数据点
【发布时间】:2020-09-24 14:18:20
【问题描述】:

我有一个显示线条的 Oxyplot 图表,如下所示

                    <oxy:Plot x:Name="MyChart"
                              Title="Real"
                              Grid.Row="1"
                              Grid.Column="0">
                        <oxy:Plot.Series>
                            <oxy:LineSeries Title="MySeries"/>
                        </oxy:Plot.Series>
                        <oxy:Plot.Axes>
                            <oxy:LinearAxis Position="Left" TicklineColor="White" Title= "MySeries"/>
                            <oxy:LinearAxis Position="Bottom" TicklineColor="White" />    
                        </oxy:Plot.Axes>
                    </oxy:Plot>

当用户左键单击该行时,将显示跟踪器,显示所选数据点。我想在我的代码中有一个处理程序来获取选定的数据点,但不确定执行此操作的正确方法。

我尝试添加如下处理程序

    this.MyChart.ActualModel.MouseDown += OxyMouseDown;

    private void OxyMouseDown(object sender, OxyMouseDownEventArgs e)
    {
        LineSeries lineSeries = sender as LineSeries;
        if (lineSeries != null)
        {
            double x = lineSeries.InverseTransform(e.Position).X;
        }
    }

但是,尽管调用了处理程序,但发送方绝不是 LineSeries 类型,因此我永远无法转换该点。

有人可以帮忙吗?

谢谢。

【问题讨论】:

    标签: wpf mouseevent oxyplot


    【解决方案1】:

    Plot 元素替换为PlotView 元素并创建一个PlotModel,您可以在其中添加您的系列和轴:

    PlotModel plotModel = new PlotModel() { Title = "Real" };
    LineSeries lineSeries = new LineSeries() { Title = "MySeries" };
    plotModel.Series.Add(lineSeries);
    //...and add the axes
    MyChart.Model = plotModel;
    

    XAML:

    <oxy:PlotModel x:Name="MyChart" Grid.Row="1" />
    

    那么你应该可以处理LineSeries的事件了:

    lineSeries.MouseDown += OxyMouseDown;
    

    【讨论】:

    • 谢谢。这不会为我编译,因为 lineSeries.MouseDown 需要一个类型的处理程序(对象发送者,MouseButtonEventArgs e)。
    • 另外,当我将处理程序更改为使用 MouseButtonEventArgs 时,当我单击图表时,处理程序永远不会被调用。
    • @user5265160:将Plot 元素替换为PlotView 元素并创建PlotModel。查看我的编辑。
    • 谢谢。如何绑定到 plotmodel 系列?这没有 ItemsSource。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    相关资源
    最近更新 更多