svg2 = {
const svg = d3.select(DOM.svg(width, height));
const view = svg.append("g")
.classed("view", true)
.attr("transform", `translate(${margin}, ${margin})`);
const nodes = view.selectAll("rect.node")
.data(graph.nodes)
.join("rect")
.attr("x", d => d.x0)
.attr("y", d => d.y0)
.attr("width", d => d.x1 - d.x0)
.attr("height", d => Math.max(1, d.y1 - d.y0))
.attr("fill", d => d.color)
.attr("opacity", 0.9);
nodes.append("title").text(d => `${d.name}\n${(d.value)}`);
view.selectAll("text.node")
.data(graph.nodes)
.join("text")
.attr("x", d => d.x1)
.attr("dx", 6)
.attr("y", d => (d.y1 + d.y0) / 2)
.attr("dy", "0.35em")
.attr("fill", "black")
.attr("text-anchor", "start")
.attr("font-size", 10)
.attr("font-family", "Arial, sans-serif")
.text(d => d.name)
.filter(d => d.x1 > width / 2)
.attr("x", d => d.x0)
.attr("dx", -6)
.attr("text-anchor", "end");
const links = view.selectAll("path.link")
.data(graph.links)
.join("path")
.attr("d", d3Sankey.sankeyLinkHorizontal())
.attr("stroke", "black")
.attr("stroke-opacity", 0.1)
.attr("stroke-width", d => Math.max(1, d.width))
.attr("fill", "none");
links.append("title").text(d => `${d.source.name} ${d.target.name}\n${(d.value)}`);
function branchAnimate(evt, node) {
if (node === undefined) node = evt;
let links = view.selectAll("path")
.filter((link) => {
return node.sourceLinks.indexOf(link) !== -1;
});
let nextNodes = [];
links.each((link) => {
nextNodes.push(link.target);
});
links
.transition()
.attr("stroke-opacity", 0.5)
.duration(duration)
.ease(d3.easeLinear)
.attr("stroke-dashoffset", 0)
.transition()
.duration(500)
.attr("stroke-opacity", 0.8);
}
function branchClear() {
links.attr("stroke", "black")
.attr("stroke-opacity", 0.1)
.attr("stroke-width", d => Math.max(1, d.width))
.attr("fill", "none");
}
nodes.on("mouseover", branchAnimate)
.on("mouseout", branchClear);
return svg.node();
}