VivaGraphJS icon indicating copy to clipboard operation
VivaGraphJS copied to clipboard

How to Implement Nested Nodes in Nodes

Open yukixiong opened this issue 8 years ago • 1 comments
trafficstars

How to Implement Nested Nodes in Nodes

yukixiong avatar Jan 05 '17 09:01 yukixiong

If you mean, having new nodes appear when clicking on one, here's my solution:

/*
   links: list of all the links
   nodeToNested: a dictionnary of node => nestedNode
*/
function expand_cluster(node_id, graph, links, nodeToNested) {
  // remove current node
  graph.removeNode(node_id);

  // add all nodes of the cluster
  links.forEach(function(line) {
      // if origin node in cluster, add it
      if (nodeToNested[line[0]] == node_id) {
        if (graph.getNode(line[1]) || nodeToNested[line[1]] == node_id) {
          graph.addLink(line[0], line[1]);
        } else {
          // if target node not yet expanded, link to the cluster
          graph.addLink(line[0], nodeToNested[line[1]]);
        }
      }
      // if target node in cluster, add it
      else if (nodeToNested[line[1]] == node_id) {
        if (graph.getNode(line[0]) || nodeToNested[line[0]] == node_id) {
          graph.addLink(line[0], line[1]);
        } else {
          // if origin node not yet expanded, link to the cluster
          graph.addLink(nodeToNested[line[0]], line[1]);
        }
      }
  });
}

mdamien avatar Feb 21 '17 13:02 mdamien