Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Dave Landry edited this page Oct 3, 2014 · 6 revisions

d3plus features Utilities that can be used to help with some common javascript processes.

Given an Array of 2 numbers and a desired integer of buckets, this method returns an Array of values in between (and including) the original Array. For example, if we wanted to define 5 buckets between 0 and 100, we would call:

var buckets = d3plus.util.buckets([0,100],5)

The array returned will look like this:

[0, 25, 50, 75, 100]

Returns a Boolean whether or not the child element is inside of the parent element. This function is recursive, meaning it will return true if the element is a "grandchild", and so forth.

Given an Array of numbers and a defined Number, .closest() will search the Array for the value closest to the defined Number. Take this example:

var closest = d3plus.util.closest([0,25,50,75,100],31)

The value returned would be 25, as it is the closest numeric value in the given Array to 31

This method clones an object and remove all symbolic links to the original object.

Returns a Boolean whether or not the selection passed is an actual d3.selection.

Given an image url and a callback Function, .dataurl() will convert the image URL into a Base 64 data URL and return it to the callback.

This is used internally in the Legend to make it easier for sites using d3plus to download an SVG with the icons embedded in the file.

Given an Array of objects and a String to be used as a key, this method will return an array of the unique values for the given key that exist in the data Array. In this example, we use .uniques() to find the unique years available in the data:

var obj = [
	{"year": 2005, "id": 1},
	{"year": 2006, "id": 1},
	{"year": 2006, "id": 2},
	{"year": 2010, "id": 1},
	{"year": 2010, "id": 2}
]

var uniques = d3plus.util.uniques(obj,"year")

The returned variable "uniques" looks like this:

[ 2005 , 2006 , 2010 ]

Clone this wiki locally