Published
Edited
May 26, 2021
2 forks
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
root.each(d => (d.current = d));

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

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("d", d => arc(d.current));

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

path.append("title").text(d => `${getHierarchyPath(d)}\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("font-size", d => +labelVisible(d.current) * fontSize)
.attr("transform", d => labelTransform(d.current))
.text(d => d.data.name);

const goBackLabel = g
.append("text")
.text(getHierarchyPath(root))
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle")
.attr("transform", "scale(3)");

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

const tau = 2 * Math.PI;
function clicked(event, p) {
const pp = p.parent || root;
parent.datum(pp);
const t = g.transition().duration(mutable mutableAnimationDuration);
goBackLabel.transition(t).text(getHierarchyPath(p));

const pdx = p.x1 - p.x0;
root.each(d => {
const dt = {
x0: clamp((d.x0 - p.x0) / pdx, 0, 1) * tau,
x1: clamp((d.x1 - p.x0) / pdx, 0, 1) * tau,
y0: Math.max(0, d.y0 - p.depth),
y1: Math.max(0, d.y1 - p.depth)
};

dt.labelVisible = labelVisible(dt);
dt.arcVisible = arcVisible(dt);

d.target = dt;
});

// 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") || d.target.arcVisible;
})
.attr("fill-opacity", d =>
d.target.arcVisible ? (d.children ? 0.6 : 0.4) : 0
)
.attrTween("d", d => () => arc(d.current));

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

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

function labelVisible(d) {
return d.y1 <= yMax && 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 = ((clampY(d.y0) + clampY(d.y1)) / 2) * radius;
return `rotate(${x - 90}) translate(${y},0) rotate(${x < 180 ? 0 : 180})`;
}

return svg.node();
}
Insert cell
Insert cell
data = FileAttachment("flare-2.json").json()
Insert cell
root = partition(data)
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 / (2 * yMax)
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 => clampY(d.y0) * radius)
.outerRadius(d => Math.max(clampY(d.y0) * radius, clampY(d.y1) * radius - 1))
Insert cell
clampY = y => clamp(y, 1, yMax);
Insert cell
clamp = (x, a, b) => Math.max(a, Math.min(b, x))
Insert cell
Insert cell
mutable mutableAnimationDuration = animationDuration
Insert cell
d3 = require("d3@6")
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