修复D3强制定向布局中的节点位置 [英] Fix Node Position in D3 Force Directed Layout

查看:116
本文介绍了修复D3强制定向布局中的节点位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望力导向布局中的某些节点忽略所有力,并基于该节点的属性停留在固定位置,同时仍然能够被拖动并在其他节点上施加排斥力并保持其链接线.

I want some of the nodes in my force-directed layout to ignore all forces and stay in fixed positions based on an attribute of the node, while still being able to be dragged and exert repulsion on other nodes and maintain their link lines.

我认为这将像这样简单:

I thought it would be as simple as this:

force.on("tick", function() {
    vis.selectAll("g.node")
        .attr("transform", function(d) {
            return (d.someAttribute == true) ?
               "translate(" + d.xcoordFromAttribute + "," + d.ycoordFromAttribute +")" :
               "translate(" + d.x + "," + d.y + ")"
        });
  });

我还尝试过手动设置每个刻度的节点的x和y属性,但是链接继续浮动到该节点受力影响的位置.

I have also tried to manually set the node's x and y attributes each tick, but then the links continue to float out to where the node would be if it was affected by the force.

很明显,我对它应该如何工作有一个基本的误解.如何在保持链接的同时仍允许它们可拖动的同时将节点固定在一个位置?

Obviously I have a basic misunderstanding of how this is supposed to work. How can I fix nodes in a position, while keeping links and still allowing for them to be draggable?

推荐答案

将所需节点上的d.fixed设置为true,然后将d.xd.y初始化为所需位置.这些节点仍将是模拟的一部分,您可以使用常规显示代码(例如,设置转换属性);但是,由于将它们标记为固定,因此只能通过拖动而不是通过模拟来移动它们.

Set d.fixed on the desired nodes to true, and initialize d.x and d.y to the desired position. These nodes will then still be part of the simulation, and you can use the normal display code (e.g., setting a transform attribute); however, because they are marked as fixed, they can only be moved by dragging and not by the simulation.

有关更多详细信息,请参见强制布局文档(当前文档),并了解如何根节点位于此示例中.

See the force layout documentation for more details (v3 docs, current docs), and also see how the root node is positioned in this example.

这篇关于修复D3强制定向布局中的节点位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆