Public
Edited
May 23, 2023
Insert cell
Insert cell
chart1 = {
const root = partition(data);

root.each((d) => (d.current = d));

const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, width])
.style("font", "10px sans-serif");

const g = svg
.append("g")
.attr("transform", `translate(${width / 2},${width / 2})`);

const path = g
.append("g")
.selectAll("path")
.data(root.descendants().slice(1))
.join("path")
.attr("fill", (d) => {
while (d.depth > 1) d = d.parent;
return color(d.data.name);
})
.attr("fill-opacity", (d) =>
arcVisible(d.current) ? (d.children ? 0.6 : 0.4) : 0
)
.attr("pointer-events", (d) => (arcVisible(d.current) ? "auto" : "none"))

.attr("d", (d) => arc(d.current));

path
.filter((d) => d.children)
.style("cursor", "pointer")
.on("click", clicked);

path.append("title").text(
(d) =>
`${d
.ancestors()
.map((d) => d.data.name + d.data.id)
.reverse()
.join("/")}\n${format(d.value)}`
);

const label = g
.append("g")
.attr("pointer-events", "none")
.attr("text-anchor", "middle")
.style("user-select", "none")
.selectAll("text")
.data(root.descendants().slice(1))
.join("text")
.attr("dy", "0.35em")
.attr("fill-opacity", (d) => +labelVisible(d.current))
.attr("transform", (d) => labelTransform(d.current))
.text((d) => d.data.name);

const parent = g
.append("circle")
.datum(root)
.attr("r", radius)
.attr("fill", "none")
.attr("pointer-events", "all")
.on("click", clicked);

function clicked(event, p) {
parent.datum(p.parent || root);

root.each(
(d) =>
(d.target = {
x0:
Math.max(0, Math.min(1, (d.x0 - p.x0) / (p.x1 - p.x0))) *
2 *
Math.PI,
x1:
Math.max(0, Math.min(1, (d.x1 - p.x0) / (p.x1 - p.x0))) *
2 *
Math.PI,
y0: Math.max(0, d.y0 - p.depth),
y1: Math.max(0, d.y1 - p.depth)
})
);

const t = g.transition().duration(750);

// Transition the data on all arcs, even the ones that aren’t visible,
// so that if this transition is interrupted, entering arcs will start
// the next transition from the desired position.
path
.transition(t)
.tween("data", (d) => {
const i = d3.interpolate(d.current, d.target);
return (t) => (d.current = i(t));
})
.filter(function (d) {
return +this.getAttribute("fill-opacity") || arcVisible(d.target);
})
.attr("fill-opacity", (d) =>
arcVisible(d.target) ? (d.children ? 0.6 : 0.4) : 0
)
.attr("pointer-events", (d) => (arcVisible(d.target) ? "auto" : "none"))

.attrTween("d", (d) => () => arc(d.current));

label
.filter(function (d) {
return +this.getAttribute("fill-opacity") || labelVisible(d.target);
})
.transition(t)
.attr("fill-opacity", (d) => +labelVisible(d.target))
.attrTween("transform", (d) => () => labelTransform(d.current));
}

function arcVisible(d) {
return d.y1 <= 3 && d.y0 >= 1 && d.x1 > d.x0;
}

function labelVisible(d) {
return d.y1 <= 3 && d.y0 >= 1 && (d.y1 - d.y0) * (d.x1 - d.x0) > 0.03;
}

function labelTransform(d) {
const x = (((d.x0 + d.x1) / 2) * 180) / Math.PI;
const y = ((d.y0 + d.y1) / 2) * radius;
return `rotate(${x - 90}) translate(${y},0) rotate(${x < 180 ? 0 : 180})`;
}

return svg.node();
}
Insert cell
chart = {
const root = pack(data);
let focus = root;
let view;
const height = width;
const color = color2;

const svg = d3
.create("svg")
.attr("viewBox", `-${width / 2} -${height / 2} ${width} ${height}`)
.style("display", "block")
.style("margin", "0 -14px")
.style("background", color(0))
.style("cursor", "pointer")
.on("click", (event) => zoom(event, root));

const node = svg
.append("g")
.selectAll("circle")
.data(root.descendants().slice(1))
.join("circle")
.attr("fill", (d) => (d.children ? color(d.depth) : "white"))
.attr("pointer-events", (d) => (!d.children ? "none" : null))
.on("mouseover", function () {
d3.select(this).attr("stroke", "#000");
})
.on("mouseout", function () {
d3.select(this).attr("stroke", null);
})
.on(
"click",
(event, d) => focus !== d && (zoom(event, d), event.stopPropagation())
);

const label = svg
.append("g")
.style("font", "20px sans-serif")
.style("font-weight", "bolder")
.attr("pointer-events", "none")
.attr("text-anchor", "middle")
.selectAll("text")
.data(root.descendants())
.join("text")
.style("font-size", "larger")
.style("fill", "black")
.style("fill-opacity", (d) => (d.parent === root ? 1 : 0))
.style("display", (d) => (d.parent === root ? "inline" : "none"))
.text((d) => d.data.name);

zoomTo([root.x, root.y, root.r * 2]);

function zoomTo(v) {
const k = width / v[2];

view = v;

label.attr(
"transform",
(d) => `translate(${(d.x - v[0]) * k},${(d.y - v[1]) * k})`
);
node.attr(
"transform",
(d) => `translate(${(d.x - v[0]) * k},${(d.y - v[1]) * k})`
);
node.attr("r", (d) => d.r * k);
label.style("font-size", (d) => (d.r * k) / 3 + "px");
}

function zoom(event, d) {
const focus0 = focus;

focus = d;

const transition = svg
.transition()
.duration(event.altKey ? 7500 : 750)
.tween("zoom", (d) => {
const i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2]);
return (t) => zoomTo(i(t));
});

label
// .style("font-size", (d) => d.r / 3 + "px")
.filter(function (d) {
return d.parent === focus || this.style.display === "inline";
})
.transition(transition)
.style("fill-opacity", (d) => (d.parent === focus ? 1 : 0))
.on("start", function (d) {
if (d.parent === focus) this.style.display = "inline";
})
.on("end", function (d) {
if (d.parent !== focus) this.style.display = "none";
});
}

return svg.node();
}
Insert cell
pack = (data) =>
d3.pack().size([width, width]).padding(3)(
d3
.hierarchy(data)
.sum((d) => d.value)
.sort((a, b) => b.value - a.value)
)
Insert cell
color2 = d3
.scaleLinear()
.domain([0, 5])
.range(["hsl(152,80%,80%)", "hsl(228,30%,40%)"])
.interpolate(d3.interpolateHcl)
Insert cell
data = FileAttachment("china_tree@8.json").json()
Insert cell
partition = data => {
const root = d3.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.value - a.value);
return d3.partition()
.size([2 * Math.PI, root.height + 1])
(root);
}
Insert cell
color = d3.scaleOrdinal(
d3.quantize(d3.interpolateRainbow, data.children.length + 1)
)

Insert cell
format = d3.format(",d")
Insert cell
width = 932
Insert cell
radius = width / 6
Insert cell
arc = d3.arc()
.startAngle(d => d.x0)
.endAngle(d => d.x1)
.padAngle(d => Math.min((d.x1 - d.x0) / 2, 0.005))
.padRadius(radius * 1.5)
.innerRadius(d => d.y0 * radius)
.outerRadius(d => Math.max(d.y0 * radius, d.y1 * radius - 1))
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