如何一次移动所有选定的节点?D3/JS.

How do I move all selected nodes at once ? D3/JS

本文关键字:节点 D3 JS 移动 何一次      更新时间:2023-09-26

我有一个用D3制作的力定向图。

我可以在节点上拖动一个框并将其属性更改为"已选择"。

我现在要做的是一次移动所有这些选定的节点。这是我的拖动功能'

function dragstart(d, i) 
{
    force.stop(); //-stop the force layout as soon as you move nodes
}
function dragmove(d, i) //-change coordinates of nodes and lines ???
{
    d.px += d3.event.dx;
    d.py += d3.event.dy;
    d.x += d3.event.dx;
    d.y += d3.event.dy; 
    tick();
}
function dragend(d, i) //-when you stop dragging the node
{
    d.fixed = true; //-D3 giving the node a fixed attribute
    d3.select(this).classed("fixed", true); //-changing nodes CSS class
    tick(); //-update positions
}

如何应用它以便一次移动所有选定的节点?

我假设你的意思是你把节点的类更改为"选择"而不是它们的属性。我是 D3js 的初学者,但我认为应该发生以下情况:

d3.behaviour.drag()
    .on("dragstart", function() {
        d3.selectAll('.selected').each(dragstart(d,i))
    }) 
    .on("drag", function() {
        d3.selectAll('.selected').each(dragmove(d,i))
    }) 
    .on("dragend", function() {
        d3.selectAll('.selected').each(dragend(d,i))
    })

根据您的函数tick()做什么,这可能适合您。