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

二次信任 提交于 2019-12-30 07:20:14

问题


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 it will stay there.I have a working fiddle here,which is showing the relationship among the nodes.can anyone help me from here to do this event in this api? ..

this is the fiddle

var node = vis
  .selectAll("g.node")
  .data(data.nodes)
  .enter()
  .append("svg:g")
  .attr("class", "node")
  .call(force.drag);

http://jsfiddle.net/vuCAx/

I think the changes should be made here


回答1:


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.



来源:https://stackoverflow.com/questions/20585962/how-to-add-a-force-drag-event-in-d3-and-make-the-node-stay-where-i-leave-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!