Public
Edited
Feb 14, 2024
Insert cell
Insert cell
countries = FileAttachment("countries.json").json()
Insert cell
countries
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
d3.map(countries, (country) => `${country.label} (${country.code})`)
Insert cell
Insert cell
d3.filter(countries, (country) => country.suggested)
Insert cell
Insert cell
countries.reduce((acc, country) => {
acc[country.code] = country.label;
return acc;
}, {});
Insert cell
d3.reduce(countries, (acc, country, index) => {
acc[country.code] = country.label
return acc
}, {})
Insert cell
Insert cell
d3.merge([[1], [2, 3]])
Insert cell
d3.merge([[1], [2, 3], [1, 2, 3]])
Insert cell
d3.merge(new Set([new Set([1]), new Set([2, 3])]))
Insert cell
d3.merge([[1, 2], [3, [4, 5]]])
Insert cell
d3.merge([[1, 2], [3], d3.merge([[4, 5], [6, 7]])]) // lol 😝
Insert cell
Insert cell
d3.cross([1, 2], ["x", "y"])
Insert cell
d3.cross([1, 2], ["x", "y"], (a, b) => a + b)
Insert cell
d3.cross([{ id: 1}, {id: 2 }], ["x", "y"], (a, b) => `${a.id}-${b}`)
Insert cell
Insert cell
d3
.select(html`<svg width=100 height=100>`)
.call((svg) =>
svg
.selectAll("circle")
.data(d3.cross(d3.range(10), d3.range(10)))
.join("circle")
.attr("transform", ([i, j]) => `translate(${i * 10},${j * 10})`)
.attr("cx", 5)
.attr("cy", 5)
.attr("r", 2.5)
.style("fill", ([i]) => (i <= 4 ? "green" : "red"))
)
.node()
Insert cell
Insert cell
d3.pairs([1, 2, 3, 4])
Insert cell
d3.pairs([1]) // less than two elements
Insert cell
d3.pairs([{ name: "Xavi" }, { name: "Messi" }, { name: "Iniesta" }])
Insert cell
d3.pairs(
[{ name: "Rice" }, { name: "Beans" }, { name: "Plantain" }],
(a, b) => `${a.name} & ${b.name}`
)
Insert cell
d3.pairs([{name: "John", yob: "1993"}, {name: "Jane", yob: "1995"}, {name: "Bob", yob: "1998"}], (a, b) => `${b.name} is older than ${a.name} by ${b.yob - a.yob} years.`)
Insert cell
Insert cell
d3.zip(["Messi", "Xavi", "Iniesta"], [10, 6, 8])
Insert cell
Insert cell
d3.transpose([
["Messi", "Neymar", "Suarez"],
[10, 11, 9]
])
Insert cell
d3.transpose([
["Messi", 10],
["Neymar", 11],
["Suarez", 9]
])
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