How to add a force drag event in D3 and make the node stay where i leave it?

前端 未结 1 1214
Happy的楠姐
Happy的楠姐 2021-01-06 15:15

i have a D3 api which is showing some relationship between the nodes .I want to apply force.drag() event here where I will drag the node in a position and leave the node and

1条回答
  •  伪装坚强ぢ
    2021-01-06 15:26

    The solution involves setting the 'fixed' node property to true on dragstart.

    var drag = force.drag()
        .on("dragstart", dragstart);
    
    var node = vis.selectAll("g.node").data(data.nodes).enter().append(
        "svg:g").attr("class", "node").call(drag);
    
    function dragstart(d) {
      d.fixed = true;
    }
    

    See here: Sticky Force Layout

    Updated Fiddle: http://jsfiddle.net/vuCAx/1/

    Documentation: force.drag()

    If you want dragged nodes to remain fixed after dragging, set the fixed attribute to true on dragstart, as in the sticky force layout example.

    0 讨论(0)
提交回复
热议问题