chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width + 60, height]);
const { nodes, links } = sankey({
nodes: graph.nodes.map(d => Object.assign({}, d)),
links: graph.links.map(d => Object.assign({}, d))
});
svg
.append("g")
.selectAll("rect")
.data(nodes)
.append("title")
.text(d => `${d.name}\n${d.value.toLocaleString()}`);
svg
.append("g")
.attr('transform', 'translate(30,0)')
.attr("fill", "none")
.selectAll("g")
.data(links)
.join("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", d => {
console.log(d.names[0], color(d.names[0]));
return color(d.names[0]);
})
.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 + 22 : d.x0 + 60))
.attr("y", d => (d.y1 + d.y0) / 2)
.attr("text-anchor", "end")
.text(d => d.name)
.append("tspan")
.attr("dx", "-20px")
.attr("dy", "1.2em")
.text(d => (d.x0 < width / 2 ? ` ${d.value.toLocaleString()}` : ''));
return svg.node();
}