简体   繁体   English

在 d3 强制定向布局中拖动多个节点

[英]drag multiple nodes in d3 force directed layout

Precondition : d3 force directed layout;前提条件:d3 力定向布局; some nodes are selected by sequential clicking one by one (visually they become bigger and in the code pushed to array)一些节点是通过一个一个依次点击来选择的(视觉上它们变大并在代码中推送到数组)

Is there a way to drag them all by picking one with the mouse the same way as files in the Windows explorer?有没有办法像在 Windows 资源管理器中的文件一样用鼠标选择一个来拖动它们?

PS I'm getting very much answers here on stackoverflow without asking for a long time. PS我在stackoverflow上得到了很多答案而没有问很长时间。 This is my first question.这是我的第一个问题。 Thanks in advance for any help!在此先感谢您的帮助!

The way that I impemented the dragging of multiple nodes (based off of children) was by recording the displacement of the dragged node inside of my tick function with a variable whose scope allows the value to still exist the next time tick runs.我阻止拖拽多个节点(基于子节点)的方法是使用一个变量记录拖拽节点在我的滴答函数内的位移,该变量的范围允许值在下一次滴答运行时仍然存在。

You will need an object where the key is a unique indentifier of the node being dragged and the value is a d3 selection of the nodes that you would like to translate/drag when the key node is dragged.您将需要一个对象,其中键是被拖动节点的唯一标识符,值是在拖动关键节点时要平移/拖动的节点的 d3 选择。

dragObject is the above-mentioned object. dragObject就是上面提到的对象。

nodeData is the d3 data of the principle node that you are dragging - ( d3.select(node uid).datum() ). nodeData是您拖动的主节点的 d3 数据 - ( d3.select(node uid).datum() )。

offset.x and offset.y make up the above-mentioned variable that was defined the last time tick was run. offset.xoffset.y构成了上次运行 tick 时定义的上述变量。

var translateAllChildren = function (nodeData) {
    if (dragObject[nodeData.uid]) {
        dragObject[nodeData.uid]
            .attr("transform", function(d) {

                d.x = (d.x + offset.x);;
                d.y = (d.y + offset.y);

                return "translate(" + d.x + "," + d.y + ")";
            });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM