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.