The problem is that when I update my layout with the new data, the whole diagram behaves as its the first time of run, moving very fast until it "calms down" normally after a 0.5 sec or so. This isn't elegant, especially now that most of the times the data I receive are the same as before.
function update(newJson) {
console.log('entered update');
var nodes = flatten(newJson),
links = d3.layout.tree().links(nodes);
// Restart the force layout.
force
.nodes(nodes)
.links(links)
.start();
var link = svg.selectAll("line.link")
link.enter().insert("svg:line", ".node")
.attr("class", "link")
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
link.exit().remove();
var node = svg.selectAll(".node")
.data(nodes, function(d) { return
d.id; });
//remove previous text
var text = svg.selectAll("text");
text.remove();
node.enter().append("g")
.attr("class", "node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.on("click", click)
.call(force.drag);
node.append("svg:circle")
.attr("class", "node")
.attr("cx", 0)
.attr("cy", 0)
.attr("r", circleRadius)
.style("fill", color);
//on hover display user info
node.append("svg:title")
.text(hover);
var text = node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.attr("class", textClass)
.text(textNode);
node.exit().remove();
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
}
);
}