Public
Edited
Nov 5, 2022
Insert cell
Insert cell
dataset = d3.range(10).map((d) => ({
apple: d3.randomInt(100)(),
orange: d3.randomInt(100)()
}))
Insert cell
Insert cell
{
const width = 400;
const height = 400;
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width);

svg
.append("g")
.selectAll("rect")
.data(dataset)
.join("rect")
.attr("x", (d, i) => i * (width / dataset.length))
.attr("y", (d) => height - d[fruit] * 4)
.attr("width", width / dataset.length - 1)
.attr("height", (d) => d[fruit] * 4)
.attr("fill", fruit === "apple" ? "red" : "orange");

svg
.append("g")
.selectAll("text")
.data(dataset)
.join("text")
.text((d) => d[fruit])
.attr("x", (d, i) => (i + 0.5) * (width / dataset.length))
.attr("y", (d) => height - d[fruit] * 4)
.attr("dy", -4)
.attr("text-anchor", "middle")
.attr("fill", fruit === "apple" ? "red" : "orange");

return svg.node();
}
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