Published
Edited
May 9, 2021
1 star
Insert cell
Insert cell
Insert cell
d3 = require("d3@v6")
Insert cell
Insert cell
Insert cell
dummyData = [1, 2, 34, 52, 90, 128, 45, 3, 87, 19]
Insert cell
Insert cell
Insert cell
d3.max(dummyData)
Insert cell
d3.min(dummyData)
Insert cell
Insert cell
d3.least(dummyData)
Insert cell
d3.leastIndex(dummyData)
Insert cell
// return the index at which the new number (60) should be placed
d3.bisect(dummyData, 60)
Insert cell
dummyDataSorted = d3.sort(dummyData, d3.ascending)
Insert cell
d3.bisect(dummyDataSorted, 60)
Insert cell
Insert cell
Insert cell
dummyDataSorted2 = d3.sort(dummyData, (a, b) => a - b)
Insert cell
Insert cell
Insert cell
dummyArrayofObjects = [
{id: 0, name: "item0", color: "red", size: 2.5},
{id: 1, name: "item1", color: "blue", size: 3.2},
{id: 2, name: "item2", color: "green", size: 4.1},
{id: 3, name: "item3", color: "green", size: 3},
{id: 4, name: "item4", color: "blue", size: 2.1},
{id: 5, name: "item5", color: "green", size: 4.5},
{id: 6, name: "item6", color: "red", size: 1.2},
{id: 7, name: "item7", color: "red", size: 3.4}
]
Insert cell
group = d3.group(dummyArrayofObjects, d => d.color)
Insert cell
group.get("green")
Insert cell
// get the mean size in each group of color
d3.rollup(dummyArrayofObjects, v => d3.mean(v, d => d.size), d => d.color)
Insert cell
// count the items by color
d3.rollup(dummyArrayofObjects, v => v.length, d => d.color)
Insert cell
Insert cell
Insert cell
d3.cross([1, 2], ["a", "b", "c"])
Insert cell
d3.cross([1, 2], ["a", "b", "c"], (a, b) => a + b)
Insert cell
d3.cross([1, 2], ["a", "b", "c"], [33, 11])
Insert cell
d3.cross([1, 2], [33, 11])
Insert cell
d3.cross([1, 2], [33, 11], (a, b) => a + b)
Insert cell
Insert cell
Insert cell
d3.permute(["a", "b", "c", "d"], [1, 2, 3, 0]) // parameters: data, key
Insert cell
// can also be used to extract the values from an object into an array with a stable order
fields = ["id", "size"]
Insert cell
d3.permute(dummyArrayofObjects[0], fields)
Insert cell
Insert cell
d3.shuffle([1, 2, 3, 4, 5]) // randomly shuffle the order
Insert cell
d3.shuffle([1, 2, 3, 4, 5], 2) // randomly shuffle the order with the start specified, i.e. all numbers starting from index 2 (inclusive) will be shuffled
Insert cell
d3.shuffle([1, 2, 3, 4, 5], 0, 3) // specified the end index, all numbers before index 3 (exlusive) will be shuffled
Insert cell
Insert cell
d3.range(5)
Insert cell
d3.range(2, 5)
Insert cell
d3.range(2, 5, 0.5) // the last parameter is the step
Insert cell
Insert cell
d3.zip([1, 2, 3], ["a", "b", "c"])
Insert cell
d3.zip([1, 2, 3, 4], ["a", "b", "c"]) // extra values that doesnt find a match will be truncated; returns an array of arrays
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more