Open In App

D3.js bisector.center() Method

Improve
Improve
Like Article
Like
Save
Share
Report

With the help of bisector.center() method, we can insert the element V in an array such that V is closest to the very ith element in an array by using this method.

Syntax:

bisector.center(array, x)

Parameter: This function has the following parameter as mentioned above and described below:

  • array: It is an array of values which is passed as parameter.
  • x: It is the value to be inserted.

Return Value: It will return index of an array after insertion of new element.

Note: To execute the below examples you have to install the d3 library by using this command prompt we have to execute the following command.

npm install d3

Example 1: In this example we can see that by using bisector.center() method, we are able to insert the new element in such a way that the new element is very closest to ith element in an array by using this method.

Filename: index.js




// Defining d3 contrib variable  
var d3 = require('d3');
  
var bisect = d3.bisector(i => i.int)
var gfg = bisect.center([1, 2, 3, 4, 5], 4)
  
console.log(gfg)


Output:

0

Example 2: Filename: index.js




// Defining d3 contrib variable  
var d3 = require('d3');
  
var arr = []
for(var i = 0; i < 5; i++) {
    arr.push(i);
}
  
var bisect = d3.bisector(i => i.float);
var gfg = bisect.center(arr, 4);
  
console.log(gfg);


Output:

0

Note: The above program will compile and run by using the following command:

node index.js

Last Updated : 23 Sep, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads