CytoscapeJS - show node neighborhood?

[亡魂溺海] 提交于 2019-12-10 20:59:11

问题


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 ?


回答1:


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



来源:https://stackoverflow.com/questions/28970633/cytoscapejs-show-node-neighborhood

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!