Public
Edited
Nov 18, 2022
Insert cell
Insert cell
Insert cell
{
const width = 400;
const height = 400;
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height]);

svg
.append("rect")
.attr("width", width)
.attr("height", height)
.attr("fill", "black");

svg
.append("circle")
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("r", 170)
.attr("fill", "green");

svg
.append("circle")
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("r", 160)
.attr("fill", "black");

svg
.append("circle")
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("r", 130)
.attr("fill", "yellow");

svg
.append("circle")
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("r", 120)
.attr("fill", "black");

svg
.append("circle")
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("r", 90)
.attr("fill", "orange");

svg
.append("circle")
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("r", 80)
.attr("fill", "black");

svg
.append("circle")
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("r", 50)
.attr("fill", "pink");

svg
.append("circle")
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("r", 40)
.attr("fill", "black");

return svg.node();
}
Insert cell
Insert cell
dataset = d3.range(10).map((d) => ({
apple: d3.randomInt(100)(),
orange: d3.randomInt(100)()
}))
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

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.apple * 4)
.attr("width", width / dataset.length - 1)
.attr("height", (d) => d.apple * 4)
.attr("fill", "red");

svg
.append("g")
.selectAll("rect")
.data(dataset)
.join("text")
.text((d) => d.apple)
.attr("x", (d, i) => (i + 0.5) * (width / dataset.length))
.attr("y", (d) => height - d.apple * 4)
.attr("dy", -4)
.attr("text-anchor", "middle")
.attr("fill", "red");
return svg.node();
}
Insert cell
Insert cell
viewof fruit = Inputs.select(["apple", "orange"], { label: "fruit" })
Insert cell
{
const width = 400;
const height = 500;
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("rect")
.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
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