0

I have a fully functioning d3.js force directed graph. There are various filters associated with the graph.

I am now trying to use the highlight feature i.e. if the node is double clicked then the node and all its neighbors are at a higher opacity as compared to the rest of the graph. And on double clicking again on any of the nodes, the entire graph will become visible. Below are the code that I've tried

 //Toggle stores whether the highlighting is on
    var toggle = 0;
    //Create an array logging what is connected to what
    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];
    }
    function connectedNodes() {
        if (toggle == 0) {
            //Reduce the opacity of all but the neighbouring nodes
            d = d3.select(this).node().__data__;
            node.style("opacity", function (o) {
                return neighboring(d, o) | neighboring(o, d) ? 1 : 0.1;
            });
            link.style("opacity", function (o) {
                return d.index==o.source.index | d.index==o.target.index ? 1 : 0.1;
            });
            //Reduce the op
            toggle = 1;
        } else {
            //Put them back to opacity=1
            node.style("opacity", 1);
            link.style("opacity", 1);
            toggle = 0;
        }
    }
peterh
  • 11,875
  • 18
  • 85
  • 108
  • Can we get a proper demo of this please? a jsfiddle would be nice – Deckerz Apr 15 '19 at 09:07
  • You mean something like this: http://bl.ocks.org/micahstubbs/d2362886844cff427e797ff992cc23ec? Also have a look at [*"How to highlight a source node and all its target nodes and corresponding links in d3 hierarchical edge bundling"*](/q/37506222) as well as [*"Fading network connections in directed network - d3js"*](/q/35655779). – altocumulus Apr 15 '19 at 09:11
  • I have already try it but it does not work – user123456789 Apr 16 '19 at 07:00

0 Answers0