chart = {
const width = 928;
const height = 720;
const sankey = d3
.sankey()
.nodeSort(null)
.linkSort(null)
.nodeWidth(4)
.nodePadding(20)
.extent([
[0, 5],
[width, height - 5]
]);
const color = d3
.scaleOrdinal(["First Court Appearance"], ["#da4f81"])
.unknown("#ccc");
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("height", height)
.attr("style", "max-width: 100%; height: auto;");
const { nodes, links } = sankey({
nodes: graph.nodes.map((d) => Object.create(d)),
links: graph.links.map((d) => Object.create(d))
});
svg
.append("g")
.selectAll("rect")
.data(nodes)
.join("rect")
.attr("x", (d) => d.x0)
.attr("y", (d) => d.y0)
.attr("height", (d) => d.y1 - d.y0)
.attr("width", (d) => d.x1 - d.x0)
.append("title")
.text((d) => `${d.name}\n${d.value.toLocaleString()}`);
svg
.append("g")
.attr("fill", "none")
.selectAll("g")
.data(links)
.join("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", (d) => color(d.names[2]))
.attr("stroke-width", (d) => d.width)
.style("mix-blend-mode", "multiply")
.append("title")
.text((d) => `${d.names.join(" → ")}\n${d.value.toLocaleString()}`);
svg
.append("g")
.style("font", "10px sans-serif")
.selectAll("text")
.data(nodes)
.join("text")
.attr("x", (d) => (d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6))
.attr("y", (d) => (d.y1 + d.y0) / 2)
.attr("dy", "0.35em")
.attr("text-anchor", (d) => (d.x0 < width / 2 ? "start" : "end"))
.text((d) => d.name)
.append("tspan")
.attr("fill-opacity", 0.7)
.text((d) => ` ${d.value.toLocaleString()}`);
return svg.node();
}