chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const {nodes, links} = sankey(data);
svg.append("g")
.attr("stroke", "#fff")
.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)
.attr("fill", d => d.color)
.append("title")
.text(d => `${d.name}\n${format(d.value)}`);
const link = svg.append("g")
.attr("fill", "none")
.attr("stroke-opacity", 0.5)
.selectAll("g")
.data(links)
.join("g")
.style("mix-blend-mode", "multiply");
if (edgeColor === "path") {
const gradient = link.append("linearGradient")
.attr("id", d => (d.uid = DOM.uid("link")).id)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", d => d.source.x1)
.attr("x2", d => d.target.x0);
gradient.append("stop")
.attr("offset", "0%")
.attr("stop-color", d => (d.source.color));
gradient.append("stop")
.attr("offset", "100%")
.attr("stop-color", d => (d.target.color));
}
link.append("path")
.attr("id", (d,i) => `linkpath-${i}`)
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", d => edgeColor === "none" ? "#aaa"
: edgeColor === "path" ? d.uid
: edgeColor === "input" ? color(d.source.name)
: color(d.target.name))
.attr("stroke-width", d => Math.max(1, d.width));
link.append("title")
.text(d => `${d.source.name} → ${d.target.name}\n${format(d.value)}`);
link.append("text")
.style("font", "10pt Inconsolata")
.style("fill", "#000")
.attr("dy", "3pt")
.append("textPath")
.attr("xlink:href", (d,i) => `#linkpath-${i}`)
.attr("startOffset", "30%")
.attr("text-anchor", "middle")
.text(d => `${format(d.value)}%`);
svg.append("g")
.style("font", "12pt Inconsolata")
.style("fill", "#000")
.selectAll("text")
.data(nodes)
.join("text")
.attr("x", d => d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6)
.attr("y", d => d.y0)
.attr("dy", "14pt")
.attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
.text(d => `${d.name}`);
return svg.node();
}