user5265160
user5265160

Reputation: 397

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.

Upvotes: 1

Views: 1277

Answers (2)

user19335768
user19335768

Reputation:

Unfortunately, this solution is know deprecated. All events except for PlotView where marked as such.

The answer is that the binding of new events should be declared in a PlotController. an exemple is provided here.

to go further, the event can be handled more specifically by this way:

_sectionPlotCtrl.BindMouseDown(OxyMouseButton.Left, new DelegateViewCommand<OxyMouseDownEventArgs>(myPlot_HandleMouseDown));

...

private void myPlot_HandleMouseDown(IView view, IController controller, OxyMouseDownEventArgs e)
 {
      //handle mouse down stuff
 }

the oxyMouseDownEventArgs has a hitTestResult, that would return the nearest point from the serie.

Upvotes: 0

mm8
mm8

Reputation: 169360

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;

Upvotes: 1

Related Questions