简体   繁体   中英

How to find number of data points in a given range in d3

I am trying to find the number of data points in my series that lie in a given range (xMin,yMin) to (xMax, yMax) . To give an example, if my series line plot looks like (1,1) (2,2) (4,4) (6,7) (9,10) and my range is from (3,3) to (8,8) , then 2 points satisfy the criteria I am looking for and they are => (4,4) (6,7)

(The actual problem I am trying to solve is if I have a rectangle to zoom on a series plot, then allow zooming if the rectangle of zoom at least captures N data points from the original series)

Is there an easy way to do this in d3?

Thank you

This is fairly straight-forward with javascript only:

var data = [[1,1],[2,2],[4,4],[6,7],[9,10]];

function filterData(minArr, maxArr) {
  return data.filter(function(d) {
    return d[0] >= minArr[0] &&
           d[0] <= maxArr[0] &&
           d[1] >= minArr[1] &&
           d[1] <= maxArr[1];
  });
}

console.log(filterData([3,3], [8,8]))  //[[4,4],[6,7]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM