Skip to content
Related Articles

Related Articles

Improve Article

D3.js bisector.center() Method

  • Last Updated : 23 Sep, 2020

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:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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
My Personal Notes arrow_drop_up
Recommended Articles
Page :