我正在浏览 sankey 布局代码,并找到了用于遍历布局节点的广度优先搜索实现。这里有一些关于 BFS 的知识 - http://www.cse.ohio-state.edu/~gurari/course/cis680/cis680Ch14.html
纯粹基于此,这里是在两个方向上突出显示单击节点的所有路径的功能 - 向前(目标)和向后(源)
希望这对某人有帮助!
工作示例 -
http://bl.ocks.org/git-ashish/8959771
https://observablehq.com/@git-ashish/sankey-diagram
function highlight_node_links(node,i){
var remainingNodes=[],
nextNodes=[];
var stroke_opacity = 0;
if( d3.select(this).attr("data-clicked") == "1" ){
d3.select(this).attr("data-clicked","0");
stroke_opacity = 0.2;
}else{
d3.select(this).attr("data-clicked","1");
stroke_opacity = 0.5;
}
var traverse = [{
linkType : "sourceLinks",
nodeType : "target"
},{
linkType : "targetLinks",
nodeType : "source"
}];
traverse.forEach(function(step){
node[step.linkType].forEach(function(link) {
remainingNodes.push(link[step.nodeType]);
highlight_link(link.id, stroke_opacity);
});
while (remainingNodes.length) {
nextNodes = [];
remainingNodes.forEach(function(node) {
node[step.linkType].forEach(function(link) {
nextNodes.push(link[step.nodeType]);
highlight_link(link.id, stroke_opacity);
});
});
remainingNodes = nextNodes;
}
});
}
function highlight_link(id,opacity){
d3.select("#link-"+id).style("stroke-opacity", opacity);
}