我要解决这个:
1)一旦显示图形,我希望能够拖动任意节点任何位置,部队不干预;链路的长度会自动调整
第一:
当你让你的矩形或圆形的节点,然后给它一个像这样的类:
var circlesOrRects = svg.append("g").selectAll(".foo")
.data(force.nodes())
.enter()
.append("path")
.attr("class", "foo")//give it a class
力向图后,下一步停止,修复如下所示的节点:
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(150)
.charge(-300)
.on("tick", tick)
.start()
.on("end", function(p) {
//using the class for selecting nodes.
d3.selectAll(".foo").each(function(d){
d.fixed=true;//thsi will fix the node.
});
});
现在在力停止后,您可以将节点拖到任何地方。
请注意,结束事件将被触发,一旦强制阿尔法变为0,只有这样才能修复节点。
工作代码here
可能有两个办法从什么是马克的回答表明不同。选择哪一个在很大程度上取决于1)当力量仍在运行时或刚刚停止时,你想要拖动行为吗? 2)力量布局是否会在初始停顿后重新加热? – altocumulus