Public
Edited
Nov 18, 2022
Insert cell
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").append("rect").attr("width", width).attr("height", height);

const data = d3.range(4).map((d) => ({
radius: 50 + d * 40,
color: ["pink", "orange", "yellow", "green"].at(d)
}));
// return data;

svg
.append("g")
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("r", (d) => d.radius)
.attr("fill", "none")
.attr("stroke", (d) => d.color)
.attr("stroke-width", 10);

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
Insert cell
viewof fruit = Inputs.select(["apple", "orange"], { label: "fruit" })
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
{
const width = 400;
const height = 400;
const div = d3.create("div");

let fruit = "apple";
const form = div.append("form");
const formDiv = form
.selectAll("div")
.data(["apple", "orange"])
.join("div")
.on("click", function () {
fruit = d3.select(this).datum();
updateSVG();
// console.log(fruit);
});

formDiv
.append("input")
.attr("type", "radio")
.attr("name", "fruit")
.attr("id", (d) => d)
.attr("checked", (d) => (d === fruit ? "checked" : null));
formDiv
.append("label")
.attr("for", (d) => d)
.text((d) => d);

const svg = div
.append("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width);

const updateSVG = function () {
svg.selectAll("*").remove();
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");
};

updateSVG();

return div.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