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

svg
.append("circle")
.attr("cx", 25)
.attr("cy", 25)
.attr("r", 20)
.attr("fill", "yellow")
.attr("stroke", "orange")
.attr("stroke-width", 2);

return svg.node();
}
Insert cell
Insert cell
Insert cell
dataset = [5, 25, 45, 65, 85]
Insert cell
chart_with_color = {
const width = 400;
const height = 100;
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height]);

const rects = svg.selectAll("rect").data(dataset).join("rect");

rects
.attr("x", (d, i) => i * 50)
.attr("y", 0)
.attr("width", 40)
.attr("height", (d) => d)
.attr("fill", "yellow")
.attr("stroke", "orange")
.attr("stroke-width", (d) => d / 4)
.attr("stroke-opacity", 0.5);

return svg.node();
}
Insert cell
{
const dataset = d3.shuffle(d3.range(40, 200, 10));
const width = 400;
const height = 100;
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height]);

const barPadding = 1;

// 背景
svg
.append("rect")
.attr("width", width)
.attr("height", height)
.attr("fill", "#004");

const rects = svg.append("g").selectAll("rect").data(dataset).join("rect");

rects
.attr("x", (d, i) => i * (width / dataset.length))
.attr("y", (d) => height - (d / d3.max(dataset)) * height)
.attr("width", width / dataset.length - barPadding)
.attr("height", (d) => (d / d3.max(dataset)) * height)
.attr("fill", "#800");

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