wpfmouseeventoxyplot

OxyPlot WPF get selected data point


I have an Oxyplot chart displaying a lineseries, shown as follows

                    <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>

When the user left clicks on the line, the tracker is displayed, showing the selected data point. I would like to have a handler in my code to get the selected data point but am not sure the correct way to do this.

I have tried adding a handler as follows

    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;
        }
    }

However, although the handler gets called, the sender is never of type LineSeries and therefore I can never transform the point.

Can someone help please?

Thanks.


Solution

  • Replace the Plot element with a PlotView element and create a PlotModel to which you add your series and axes:

    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" />
    

    Then you should be able to handle the event of the LineSeries:

    lineSeries.MouseDown += OxyMouseDown;