Black Lives Matter. Please consider donating to Black Girls Code today.

Get data for current zoomed in view

I have created a scatter plot and i want to get the data of only the current zoomed in view(Not the entire view). How can i do that?

We don’t expose an easy way to do that.

Something like:

var gd = document.getElementById('graph')
var xRange = gd.layout.xaxis.range
var yRange = gd.layout.yaxis.range

gd.data.forEach(trace => {
  var len = Math.min(trace.x.length, trace.y.length)
  var xInside = []
  var yInside = []

  for (var i = 0; i < len; i++) {
    var x = trace.x[i]
    var y = trace.y[i]

    if(x > xRange[0] && x < xRange[1] && y > yRange[0] && y < yRange[1]) {
      xInside.push(x)
      yInside.push(y)
    }
})

should do the trick.

Thank you @etienne, it did the trick.

hi, there, i have a doubt, how can we do the same for bar chart like this in the following codepen https://codepen.io/plotly/pen/74a638752a41ac9672a05f628e4ddaff . is there a way to get the data from current zoom view when x or y contains string values?