Ber*_*ery 5 d3.js force-layout
我正在使用D3.js进行力导向布局的实验.我需要的是以root(或其他选定节点)为中心布局,并在tick函数完成后将此节点返回到svg(例如canvas)中心(图形alpha为低).可能吗?我找到了一个例子
但我无法使根(当使用aplha或其他自定义滴答函数计算时)返回中心(通过此特定节点居中布局).
任何帮助,将不胜感激.
实际上我这样解决了这个问题(类似于以前但更复杂):
force.on("tick", function(e) {
node.attr("transform", function(d) {
//TODO move these constants to the header section
//center the center (root) node when graph is cooling down
if(d.index==0){
damper = 0.1;
d.x = d.x + (w/2 - d.x) * (damper + 0.71) * e.alpha;
d.y = d.y + (h/2 - d.y) * (damper + 0.71) * e.alpha;
}
//start is initiated when importing nodes from XML
if(d.start === true){
d.x = w/2;
d.y = h/2;
d.start = false;
}
r = d.name.length;
//these setting are used for bounding box, see [http://blockses.appspot.com/1129492][1]
d.x = Math.max(r, Math.min(w - r, d.x));
d.y = Math.max(r, Math.min(h - r, d.y));
return "translate("+d.x+","+d.y+")";
}
);
});
Run Code Online (Sandbox Code Playgroud)