I am using d3's force layout to create a custom collapse mechanism where the nodes collapse towards the parent node and are arranged in a cluster behind it. THis is using the normal FOrce layout with a JSON having nodes and links. I have managed to get the layout to collapse and cluster towards the main node, but as soon as the transition is done, it re-sets the nodes back to their original position. I have a feeling this has something to do with the tick() function.
Here is my code to collapse the nodes and links. (px, py) is the point they cluster towards.
currentNode.transition()
.duration(2000)
.attr("transform", "translate(" + px + "," + py + ")");
currentLink.transition()
.duration(2000)
.attr("x2", px)
.attr("y2", py);
In the tick function, I have the following:
force.on("tick", function () {
link.attr("x1", function (currLink) {
return currLink.source.x;
})
.attr("y1", function (currLink) {
return currLink.source.y;
})
.attr("x2", function (currLink) {
return currLink.target.x;
})
.attr("y2", function (currLink) {
return currLink.target.y;
});
node.attr("transform", function (currNode) {
return "translate(" + currNode.x + "," + currNode.y + ")";
});
});
Is there something terribly silly that I'm missing out here? Thanks.