d3.bisector()

d3.bisector(accessor)
d3.bisector(comparator)

Returns a new bisector using the specified accessor or comparator function. This method can be used to bisect arrays of objects instead of being limited to simple arrays of primitives. For example, given the following array of objects:

var data = [
  {date: new Date(2011, 1, 1), value: 0.5},
  {date: new Date(2011, 2, 1), value: 0.6},
  {date: new Date(2011, 3, 1), value: 0.7},
  {date: new Date(2011, 4, 1), value: 0.8}
];

A suitable bisect function could be constructed as:

var bisectDate = d3.bisector(function(d) { return d.date; }).right;

This is equivalent to specifying a comparator:

var bisectDate = d3.bisector(function(d, x) { return d.date - x; }).right;

And then applied as bisectDate(data, new Date(2011, 1, 2)), returning an index. Note that the comparator is always passed the search value x as the second argument. Use a comparator rather than an accessor if you want values to be sorted in an order different than natural order, such as in descending rather than ascending order.

doc_D3_Js
2016-11-24 10:25:47
Comments
Leave a Comment

Please login to continue.