VivaGraphJS
VivaGraphJS copied to clipboard
How to Implement Nested Nodes in Nodes
trafficstars
How to Implement Nested Nodes in Nodes
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]);
}
}
});
}