Ajit Singh
Ajit Singh

Reputation: 115

CytoscapeJS - show node neighborhood?

I have a dataset in JSON format containing information about nodes and edges which I am using to generate a network graph in cytoscapeJS. The JSON data contains id, value, shape, colour and visibleDisplay ('element' or 'none') attributes for nodes and id, source, target and label for edges. My stylesheet uses this 'visibleDisplay' property to show/ hide nodes, as needed, when the cy container is first initialised.

I want to allow users to unhide nodes with an option to "Show neighbourhood". I have modified my old code to make use of collections but it still does not work:

function showNeighbourhood() {
   var eleID;
   var neighbourArray= new Array();

   var neighbours= cy.collection(); // collection

   cy.nodes().forEach(function( ele ) {
       if(ele.selected()) { // get the currently selected node.
          eleID= ele.id();
         }
      });

   // Find its connected neighbours.
   cy.edges().forEach(function( edg ) {
       if(edg.data('source') === eleID) {
          neighbourArray[neighbourArray.length]= edg.data('target');
         }
       else if(edg.data('target') === eleID) {
          neighbourArray[neighbourArray.length]= edg.data('source');
         }
      });

   // Add the array to the collection.
   neighbours.add(neighbourArray);

  // Show neighbourhood, using the collection.
   neighbours.show();
  }

Any suggestions on how to make this work ? Can't I use the show() method on the collection to make the required nodes visible ?

Upvotes: 3

Views: 2051

Answers (1)

maxkfranz
maxkfranz

Reputation: 12250

You need only use node.neighborhood(), e.g. cy.$(':selected').neighborhood().removeClass('hidden').

Upvotes: 4

Related Questions