选择节点并按D3.js中的功能ID突出显示其连接

时间:2017-04-14 18:15:31

标签: javascript d3.js

我创建了一个D3.js网络图。我想通过它的id选择一个节点并突出显示它和它的邻居。我已经有以下代码

var linkedByIndex = {};
for (i = 0; i < graph.nodes.length; i++) {
    linkedByIndex[i + "," + i] = 1;
};
graph.links.forEach(function (d) {
    linkedByIndex[d.source.index + "," + d.target.index] = 1;
});
//This function looks up whether a pair are neighbours
function neighboring(a, b) {
    return linkedByIndex[a.index + "," + b.index];
}

但是,如何仅将其用于由其功能ID选择的一个选定节点?

1 个答案:

答案 0 :(得分:0)

我找到了答案:

d3.select("#nodeID")
    .transition().duration(500)
    .each(function(d){ //#nodeID is now selected as this
            node.style("stroke-opacity", function(o) {
                thisOpacity = isConnected(d, o) ? 1 : opacity;
                this.setAttribute('fill-opacity', thisOpacity);
                return thisOpacity;
            });
            link.style("stroke-opacity", function(o) {
                return o.source === d || o.target === d ? 1 : opacity;
            });

        }); 

我想只显示要显示的节点的标签。有什么建议吗?