Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 启动/停止d3。动态强制_Javascript_Jquery_D3.js_Diagram - Fatal编程技术网

Javascript 启动/停止d3。动态强制

Javascript 启动/停止d3。动态强制,javascript,jquery,d3.js,diagram,Javascript,Jquery,D3.js,Diagram,假设我有一个简单的D3.js力图,如本例所示。我知道力的魔力主要发生在这个函数中: function tick() { path.attr("d", function(d) { var dx = d.target.x - d.source.x, dy = d.target.y - d.source.y, dr = Math.sqrt(dx * dx + dy * dy); return "M" + d.source.x + "

假设我有一个简单的D3.js力图,如本例所示。我知道力的魔力主要发生在这个函数中:

function tick() {
  path.attr("d", function(d) {
    var dx = d.target.x - d.source.x,
        dy = d.target.y - d.source.y,
        dr = Math.sqrt(dx * dx + dy * dy);
    return "M" + 
        d.source.x + "," + 
        d.source.y + "A" + 
        dr + "," + dr + " 0 0,1 " + 
        d.target.x + "," + 
        d.target.y;
  });

  node
    .attr("transform", function(d) { 
      return "translate(" + d.x + "," + d.y + ")"; 
    });
}
我想知道,是否可以动态停止/启动原力?所以我可以移动节点,然后出于某种原因,我会重新启用原力,然后禁用它,然后四处移动(我知道启用原力会扰乱我在禁用原力时创建的图表)


有人能给我一些建议吗?我可以看到如何创建一个静态图,但它没有点击我的noob头如何从中派生我的功能。。。小提琴总是一个最清晰的例子/建议/答案。

通过将节点的
fixed
属性设置为false/true,可以从节点启动/停止d3强制。下面是示例代码和

在这里找到了答案:


使用
.start()
.stop()
@larskothoff我将用一个例子来说明,当我移动一个节点时,一旦调用了stop,力就开始了。也许你可以指出我的问题在哪里。谢谢各位,这里()我使用开始和停止。单击以禁用力并在节点周围移动后,力将再次启动。我确信这与拖动功能有关,但请建议并编辑。使用强制布局拖动行为会触发mousemove上的重新启动。您需要使用正常拖动行为或禁止重新启动。能否详细说明“正常拖动行为”?此时是否仍激发勾号功能(当节点设置为固定时)?我假设禁用强制时,不需要勾号功能
d3.select(".toggleButton").on("click",function(){
    var val =  d3.select(this).attr("value");
    if(val=="Disable Force"){
        d3.select(this).attr("value","Enable Force");
        circle.each(function(d){ d.fixed= true; })                    
    }else{
        d3.select(this).attr("value","Disable Force");
        circle.each(function(d){ d.fixed= false; })           
    }
});
var node_drag = d3.behavior.drag()
    .on("dragstart", dragstart)
    .on("drag", dragmove)
    .on("dragend", dragend);

function dragstart(d, i) {
    force.stop() // stops the force auto positioning before you start dragging
}

function dragmove(d, i) {
    d.px += d3.event.dx;
    d.py += d3.event.dy;
    d.x += d3.event.dx;
    d.y += d3.event.dy; 
    tick(); // this is the key to make it work together with updating both px,py,x,y on d !
}

function dragend(d, i) {
    d.fixed = true; // of course set the node to fixed so the force doesn't include the node in its auto positioning stuff
    tick();
    force.resume();
}