D3js - Force directed graph - advanced highlighting of neigbour nodes and links, is it possible?

前端 未结 1 1066
执念已碎
执念已碎 2021-01-03 12:17

I\'ve managed to get highlight working on my force directed graph, with help of this tutorial from Mike Bostock. Now for further procedure in my idea and needs of my graph,

相关标签:
1条回答
  • 2021-01-03 12:46

    To identify your "special" nodes, you could add another attribute to the data that identifies them. Then you can check this in your highlighting function and get second-degree neighbours if necessary. The code would look something like this.

    function fade(opacity,color) {
        return function(d) {
            var connected = [d];
            if(d.isAuxiliary) {
                node.each(function(o) { if(isConnected(d, o)) { connected.push(o); } });
            }
            node.style("stroke-opacity", function(o) {
                thisOpacity = opacity;
                connected.forEach(function(e) { 
                    if(isConnected(e, o)) { thisOpacity = 1; }
                });
                this.setAttribute('fill-opacity', thisOpacity);
                return thisOpacity;
            });
            // similar for links
        }
    }
    

    You can adapt this code to do an arbitrary level of neighbours.

    0 讨论(0)
提交回复
热议问题