The bisector() Function in D3.js is used to 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.
Syntax:
d3.bisector(accessor) d3.bisector(comparator)
Parameters: This function accepts only one parameter which is mentioned above and described below:
- accessor/comparator: This parameter is the can be accessor or comparator function.
Return value: This function returns the new bisector.
Below given are a few examples of the above function.
Example 1: This program illustrates the use of d3.bisector() using the accessor parameters.
<!DOCTYPE html>Â <html>Â Â Â Â Â <head>Â Â Â Â Â <title>D3.js d3.bisector() Function</title>Â Â Â Â Â Â Â Â </script>Â </head>Â Â Â Â Â <body>Â Â Â Â Â <script>Â Â Â Â Â Â Â Â Â var data = [ Â Â Â Â Â Â Â Â Â Â {date: new Date(2011, 1, 1), value: 0.5}, Â Â Â Â Â Â Â Â Â Â {date: new Date(2012, 2, 1), value: 0.6}, Â Â Â Â Â Â Â Â Â Â {date: new Date(2013, 3, 1), value: 0.7}, Â Â Â Â Â Â Â Â Â Â {date: new Date(2014, 4, 1), value: 0.8} Â Â Â Â Â Â Â Â ];Â Â Â Â Â Â Â Â Â Â Â Â Â var bisectDate =Â d3.bisector(function(d) { return d.date; }).left;Â Â Â Â Â Â Â Â Â var dat = new Date(2014, 4, 1); Â Â Â Â Â Â Â Â document.write(bisectDate(data, dat));Â Â Â Â Â </script>Â </body>Â Â Â Â Â </html> |
Output:
3
Example 2: This program illustrates the use of d3.bisector() using the comparator function parameters.
<!DOCTYPE html>Â <html>Â Â Â Â Â <head>Â Â Â Â Â <title>D3.js d3.bisector() Function</title>Â Â Â Â Â Â Â Â Â </script>Â </head>Â Â Â Â Â <body>Â Â Â Â Â <script>Â Â Â Â Â Â Â Â Â var data = [ Â Â Â Â Â Â Â Â Â Â {date: new Date(2011, 1, 1), value: 0.5}, Â Â Â Â Â Â Â Â Â Â {date: new Date(2012, 2, 1), value: 0.6}, Â Â Â Â Â Â Â Â Â Â {date: new Date(2013, 3, 1), value: 0.7}, Â Â Â Â Â Â Â Â Â Â {date: new Date(2014, 4, 1), value: 0.8} Â Â Â Â Â Â Â Â ];Â Â Â Â Â Â Â Â Â Â Â Â Â var bisectDate =Â d3.bisector(function(d, x) { return d.date - x; }).right; Â Â Â Â Â Â Â Â var dat = new Date(2014, 4, 1); Â Â Â Â Â Â Â Â document.write(bisectDate(data, dat));Â Â Â Â Â </script>Â </body>Â Â Â Â Â </html> |
Output:
4
