chart = {
const svg = d3.create("svg")
.attr("viewBox", "-" + adj + " -"+ adj + " " + (width + adj*2) + " " + (height + adj*2))
.style("background", mode === "dark" ? "#2e2e2e" : "#fff");
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)
.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()}`);
const path = svg.append("g")
.attr("fill", "none")
.selectAll("g")
.data(links)
.join("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", mode === "dark" ? single_color_dark_mode : single_color)
.attr("stroke-width", d => d.width)
.style("mix-blend-mode", mode === "dark" ? "lighten" : "multiply")
.on("mouseover", d => {
console.log(d);
path.attr("stroke", l => {
console.log(l);
if(l.names[0]===d.names[0]){return color(d.names[0]);}
else {
return mode === "dark" ? single_color_dark_mode : single_color;
}
});
})
.on("mouseout", d => {
path.attr("stroke", mode === "dark" ? single_color_dark_mode : single_color);
});
path.append("title")
.text(d => `${d.names.join(" → ")}\n${d.value.toLocaleString()}`);
const label = 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)
.attr("fill", mode==="dark" ? "#f2e8d2" : "#000")
.append("tspan")
.attr("fill-opacity", 0.7)
.text(d => ` ${d.value.toLocaleString()}`);
return svg.node();
}