问题
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