一旦你 d3.layout.force()....start() 在 D3 中进行强制布局模拟,它也会继续进行活动事件。
我想设置 5 秒的超时时间,让图形力布局模拟成形并停止(调用 .stop() 或 alpha(0)。这可以停止,但只要我拖动一个节点,模拟就会再次开始。
以下代码也可从jsfiddle轻松测试,在最后一行显示将发出stop()并且模拟立即停止,但只要拖动任何节点,模拟就会再次开始。我认为这是在强制拖动中硬编码的。有没有办法禁用/注销它?
var svgContainer = d3.select("#svgContainer");
var element0a = svgContainer.append("g").attr("class","node").attr("transform","translate(100,100)");
var element0b = element0a.append("rect").attr("x",0).attr("y",0).attr("width",20).attr("height",10).attr("fill","red");
var element1a = svgContainer.append("g").attr("class","node").attr("transform","translate(100,200)");
var element1b = element1a.append("rect").attr("x",0).attr("y",0).attr("width",20).attr("height",10).attr("fill","green");
var element2a = svgContainer.append("g").attr("class","node").attr("transform","translate(100,300)");
var element2b = element2a.append("rect").attr("x",0).attr("y",0).attr("width",20).attr("height",10).attr("fill","blue");
var nodeArray = new Array();
nodeArray[0] = { id : "000", label : "label 000", ui : element0a };
nodeArray[1] = { id : "001", label : "label 001", ui : element1a };
nodeArray[2] = { id : "002", label : "label 002", ui : element2a };
var linkArray = new Array();
var force = self.force = d3.layout.force()
.nodes(nodeArray)
.links(linkArray)
.gravity(.05)
.distance(80)
.charge(-100)
.size([600, 600])
.start();
var node = svgContainer.selectAll("g.node")
.data(nodeArray)
.call(force.drag);
force.on("tick", function() {
node.attr("transform", function(d) {return "translate(" + d.x + "," + d.y + ")";});
});
// HERE !!!! Without the stop() the simulation goes fine. With the stop() the simulation will immediately stop, but will continue as soon as you drap something. I want the simulation to never start again.
force.stop();
这是示例的屏幕截图: