0

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.

  • Before collapsing/expanding, call `force.stop()` to stop the simulation. – Lars Kotthoff Jun 17 '14 at 14:17
  • Thanks, that fixed the immediate resetting after the collapse event. However, I am now finding that dragging any node (even non related ones) in the graph causes the collapsed nodes to be reset. Is there any reason for that? – user3748759 Jun 18 '14 at 00:59
  • Depends on how you're doing the dragging. Could you post a complete working example please? – Lars Kotthoff Jun 18 '14 at 10:50

0 Answers0