Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bumbu/5772040 to your computer and use it in GitHub Desktop.
Save bumbu/5772040 to your computer and use it in GitHub Desktop.
Cytoscape.js - Adding compound element on the fly
$("#button").click(function(){
var list = cy2.filter(function (i, ele) {
return (parseInt(ele.id().slice(1), 10) % 2 === 0);
});
var nodes = [];
// Create new parent
nodes.push({group: "nodes", data: {id: "n0"}, position: {x: 0, y: 0}});
// Create copies of old nodes
for (var i=list.size()-1; i >= 0; i--) {
nodes.push({
group: "nodes",
data: {id: "c1" + i, parent: "n0"},
position: {x: list[i].position('x'), y: list[i].position('y')}
});
}
// Remove old nodes
list.remove();
// Add new nodes
cy2.add(nodes);
});
@YoannBuch
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment