Retrieve DataPoints displayed using Oxyplot when Zooming or Panning

One of questions I recently saw in Stackoverflow involved Oxyplot. User wanted to obtain the points which are currently displayed on screen. Remember, like with most graph controls, User could zoom/pan with Oxyplot and the points which are currently visible could only be a subset of actual points in the series.

This could be achieved by retrieving the currently displayed part of graph(ScreenRectangle) with GetScreenRectangle and verifying if each of points would fall in it.

var series = plotModel.Series.OfType<OxyPlot.Series.LineSeries>().Single();
var pointCurrentlyInDisplay = new List<DataPoint>();
foreach (var point in series.ItemsSource.OfType<DataPoint>())
{
    if (series.GetScreenRectangle().Contains(series.Transform(point)))
    {
        pointCurrentlyInDisplay.Add(point);
    }
}

An important point to observe in above code is how we transform the existing DataPoint to ScreenPoint using the Transform()method.

Do note that if you had assigned the points via Series.Points.AddRange() instead of Series.ItemSource, you could have to retrieve the existing points using

series.Points.OfType<DataPoint>()

That would retrieve you the points which are currently on display in the graph owing to Zooming or Panning.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s