William Nelson
William Nelson

Reputation: 75

Iterate connected nodes in cytoscape.js

I need to get the nodes connected to a given node, and highlight them. The "components" function looks good for this, however my traversal fails. The component collection shows a size of one and only the original node gets highlighted.

cynode = cy.getElementById(idstr);
comps = cynode.components();    
for (i = 0; i < comps.length; i++) /* really there's only one component */
{
    comp = comps[i];
    alert(comp.size());   /* this always returns 1!! */
    comp.nodes().addClass('nodehlt');  /* only the original node gets highlighted */
}

Upvotes: 0

Views: 2262

Answers (1)

maxkfranz
maxkfranz

Reputation: 12250

From the docs:

eles.components() : Get the connected components, considering only the elements in the calling collection. An array of collections is returned, with each collection representing a component.

If the set of elements you consider is only a single node, there can only ever be one component.

You need to get the components of the whole graph (cy.elements.components()) -- or of the subgraph you're interested in. Of those components, you then need to find the one that contains the node of interest.

Upvotes: 2

Related Questions