Published
Edited
Apr 23, 2020
Importers
Insert cell
Insert cell
chart = {
const root = tree(d3.hierarchy(data));

const map = new Map(root.leaves().map(d => [d.data.id, d]));

const svg = d3.select(DOM.svg(width, width))
.attr("viewBox", `${-width / 2} ${-width / 2} ${width} ${width - 40}`)
.style("max-width", "100%")
.style("height", "auto")
.style("display", "block")
.style("margin", "auto")
.style("font", "10px sans-serif");

svg.append("g")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-opacity", 0.5)
.selectAll("path")
.data(d3.merge(root.leaves().map(d => d.data.targetIds.map(i => d.path(map.get(i))))))
.join("path")
.style("mix-blend-mode", "multiply")
.attr("d", line);

svg.append("g")
.selectAll("text")
.data(root.leaves())
.join("text")
.attr("transform", d => `
rotate(${d.x * 180 / Math.PI - 90})
translate(${d.y},0)${d.x >= Math.PI ? `
rotate(180)` : ""}
`)
.attr("dy", "0.31em")
.attr("font-size", "1.2em")
.attr("x", d => d.x < Math.PI ? 3 : -3)
.attr("text-anchor", d => d.x < Math.PI ? "start" : "end")
.text(d => d.data.id);

return svg.node();
}
Insert cell
data = {
const {nodes, links} = await d3.json("https://gist.githubusercontent.com/mbostock/4062045/raw/5916d145c8c048a6e3086915a6be464467391c62/miserables.json");
const groupById = new Map;
const nodeById = new Map(nodes.map(node => [node.id, node]));

for (const node of nodes) {
let group = groupById.get(node.group);
if (!group) groupById.set(node.group, group = {name: node.group, children: []});
group.children.push(node);
node.targetIds = [];
}

for (const {source: sourceId, target: targetId} of links) {
const source = nodeById.get(sourceId);
source.targetIds.push(targetId);
}

return {name: "miserables", children: [...groupById.values()]};
}
Insert cell
width = 640
Insert cell
radius = width / 2
Insert cell
line = d3.radialLine()
.curve(d3.curveBundle.beta(0.85))
.radius(d => d.y)
.angle(d => d.x)
Insert cell
tree = d3.cluster().size([2 * Math.PI, radius - 100])
Insert cell
d3 = require("d3@5")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more