d3/d3-array

d3.bisectCenter?

mbostock opened this issue · 2 comments

It’d be nice to have something that returned the closest datum to the specified value. I’m often doing something like this:

bisect = {
  const bisectDate = d3.bisector(d => d.date).left;
  return (data, date) => {
    const i = bisectDate(data, date, 1);
    const a = data[i - 1], b = data[i];
    return date - a.date > b.date - date ? b : a;
  };
}

It’d be nicer to say

bisect = d3.bisector(d => d.date).center

(Obviously, this would only work for quantitative values; ordinal values aren’t sufficient.)

Fil commented

the (d) accessor / (d, x) comparator thing was a bit tricky, but I think #156 works as expected.