chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height + 20])
.on('mouseout', () => {
d3.selectAll(`path`)
.attr('stroke-opacity', 0.1);
});
const {nodes, links} = sankey(data);
svg.append("g")
.attr("stroke", "#000")
.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 => {
if (d.name === 'opex') {
return d3.schemeSet1[0];
}
if (d.name === 'capex') {
return d3.schemeSet1[4];
}
return color(d.name);
})
.on('mouseover', (d) => {
d.sourceLinks.forEach(e => {
d3.selectAll(`path.trajectory_${e.id}`)
.attr('stroke-opacity', 0.4);
});
d.targetLinks.forEach(e => {
d3.selectAll(`path.trajectory_${e.id}`)
.attr('stroke-opacity', 0.4);
});
})
.on('mouseout', d => {
d.sourceLinks.forEach(e => {
d3.selectAll(`path.trajectory_${e.id}`)
.attr('stroke-opacity', 0.1);
})
})
.append("title")
.text(d => `${d.name}\n${format(d.value)}`);
const link = svg.append("g")
.attr("fill", "none")
.attr("stroke-opacity", 0.4)
.selectAll("g")
.data(links)
.join("g")
.style("mix-blend-mode", "multiply");
link.append("path")
.attr('class', d => `trajectory_${d.id}`)
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", d => d.type === 'opex'
? d3.schemeSet1[0]
: d3.schemeSet1[4]
)
.attr('stroke-opacity', 0.2)
.attr("stroke-width", d => Math.max(1, d.width))
link.append("title")
.text(d => `${d.source.name} → ${d.target.name}\n${format(d.value)}`);
svg.append("g")
.style("font", "11px 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.4em")
.attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
.text(d => d.name)
.append('tspan')
.attr('dy', 12)
.attr('dx', -20)
.attr('font-size', '9px sans-serif')
.text(d => format(d.value));
return svg.node();
}