最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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);

/

I think the changes should be made here

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/vuCAx/

I think the changes should be made here

Share Improve this question edited Dec 15, 2013 at 1:16 m59 43.8k14 gold badges121 silver badges139 bronze badges asked Dec 14, 2013 at 17:11 SubhoSubho 9235 gold badges25 silver badges49 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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/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)

  1. 暂无评论