by0*_*by0 3 javascript d3.js force-layout
我试图完全冻结d3中的强制定向网络!我已经尝试将摩擦值设置为0,但网络变得更加精简,节点仍然略微悬停.
var force = d3.layout.force()
.charge(-220)
.linkDistance(70)
.friction(0);
Run Code Online (Sandbox Code Playgroud)
我还希望我的节点可以拖动,即拖动时移动位置.
最终,我试图得到类似于Cytoscape js的东西,看起来像这样.
谢谢!
首先,如果要在特定时间"冻结"图形,可以使用布局的stop命令force:
force.stop()
Run Code Online (Sandbox Code Playgroud)
一个很好的用法是首先让图形自组织(使用tick)然后停止力:
// include in beginning of script
force.start();
for (var i = 0; i < n; ++i) force.tick();
force.stop();
Run Code Online (Sandbox Code Playgroud)
然后,如果你想拖放节点,一个好主意就是drag在d3示例页面上进行搜索,你会发现以下链接:拖放支持将节点设置为固定位置,当你删除了你想要的所有内容.顺便说一下,它也与stackoverflow问题有关,您可能会觉得有趣:D3强制定向图具有拖放支持,以便在删除时固定选定的节点位置
这是一个有趣的拖放代码,适用于力已经停止的图形(我只是注释了一些行,但不确定,所以通过取消注释验证它是否按预期工作)
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();
}
function tick() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4218 次 |
| 最近记录: |