Published unlisted
Edited
Dec 17, 2021
Insert cell
Insert cell
chart = {
const root = partition(data);

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

const svg = d3
.select(DOM.svg(width, width))
.style("width", "100%")
.style("height", width)
.style("font-family", "NHaasGrotesk")
.call(
addWebFont,
"NHaasGrotesk",
"https://gist.githubusercontent.com/coindesk-research/2bf8eddda40303351c9f02f73b661cca/raw/4c22a4d26e33e04cfb56a99199e20e6b1a6e8854/NHaasGroteskTXStd-55Rg.otf",
"opentype"
);

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) =>
`${d
.ancestors()
.map((d) => d.data.name)
.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", "0em")
.style("font-size", width > 500 ? 16 : 7)
//.attr("class", "arctext")
.attr("fill-opacity", (d) => +labelVisible(d.current))
.attr("transform", (d) => labelTransform(d.current))
.text((d) => d.data.name);

setTimeout(() => {
label
.call(wrap, width > 500 ? 120 : 40)
.append("tspan")
.attr("font-weight", "bold")
.attr("x", 0)
.attr("dy", "1.1em")
.text((d) => format2(d.value));
}, 0);

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

g.append("text")
.style("font-size", width > 500 ? 16 : 8)
.transition()
.duration(750)
.ease(d3.easeLinear)
.attr("id", "intro")
.attr("text-anchor", "middle")
.attr("opacity", 1)
.text("Click the pies to see more details");

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

if (p.depth != 0 && g.selectAll("#backtext").empty()) {
g.append("text")
.style("font-size", width > 500 ? 16 : 8)
.datum(p)
.transition()
.duration(750)
.ease(d3.easeLinear)
.attr("id", "backtext")
.attr("text-anchor", "middle")
.attr("opacity", 1)
.text("Click here to go back");

g.selectAll("#intro")
.transition()
.duration(750)
.ease(d3.easeLinear)
.attr("opacity", 0);
} else if (p.depth == 0) {
g.selectAll("#backtext")
.transition()
.duration(750)
.ease(d3.easeLinear)
.attr("opacity", 0)
.remove();

g.selectAll("#intro")
.transition()
.duration(750)
.ease(d3.easeLinear)
.attr("opacity", 1);
}

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
)
.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.1;
}

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
Insert cell
Insert cell
wrap = (text, wrapWidth, yAxisAdjustment = 0) => {
text.each(function () {
let text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")) - yAxisAdjustment,
tspan = text
.text(null)
.append("tspan")
.attr("x", 0)
.attr("y", y)
.attr("dy", `${dy}em`);
while ((word = words.pop())) {
let previous_word = line.slice(-1)[0];
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > wrapWidth) {
console.log(++lineNumber)
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text
.append("tspan")
.attr("x", 0)
.attr("y", y)
.attr("dy", ++lineNumber == 0 ? "0em" : lineHeight + "em")
.text(word);
}
}
});
return 0;
}
Insert cell
color = d3.scaleOrdinal([
"#F8BF1E",
"#00D964",
"#FF0000",
"#939598",
"#E494FF",
"#24EDFF"
])
Insert cell
Insert cell
format2 = d3.format(".1%")
Insert cell
Insert cell
Insert cell
Insert cell
import { addWebFont } from "@leonelgalan/embedding-fonts-into-an-svg"
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