简体   繁体   English

d3强制定向布局-在不更改节点位置的情况下绘制链接

[英]d3 force directed layout - drawing links without changing the location of the nodes

I am trying to modify the directed graph editor in the following manner: 我试图以以下方式修改有向图编辑器

  • When a link is drawn, it won't re-position the source and target nodes. 绘制链接时,它不会重新放置源节点和目标节点。 Instead the link will be drawn between the current location of the nodes. 相反,将在节点的当前位置之间绘制链接。

  • When dragging an existing node around (using ctrl), it won't affect the position of any attached nodes attached to it. 拖动周围的现有节点(使用ctrl)时,它不会影响与其连接的任何附加节点的位置。 Instead all other nodes will remain in their position, and only the links attached to the dragged node will change their length according to the dragging-around. 取而代之的是,所有其他节点将保持在原位,只有附着到被拖动节点的链接会根据拖动的距离更改其长度。

I tried supplying this function to force's linkDistance: 我尝试提供此功能以强制使用linkDistance:

force.linkDistance(function(link) {
   var deltaX = d.target.x - d.source.x,
    deltaY = d.target.y - d.source.y,
    dist = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
   return dist;
})

Thinking that this would lead force to assume that there is no need for re-positioning the nodes at each side of the link (following the documentation ). 认为这将导致人们不得不假设无需在链接的每一侧重新定位节点(遵循文档 )。

However, this led to a run-time error, which I couldn't resolve. 但是,这导致了运行时错误,我无法解决。

Any ideas on how this behavior of the graph can be achieved? 关于该图形的行为如何实现的任何想法?

Sounds like you want to use the fixed options, by setting it as a property of each node node, eg, { id:123, fixed:true } . 通过将其设置为每个节点节点的属性,例如{ id:123, fixed:true }听起来您想使用fixed选项。

Here's a modified version 这是修改后的版本

Fixed nodes don't get moved around by the force layout at all, so unless you explicitly give them an initial position, they just get assigned a random one by the layout. 固定节点根本不会被强制布局移动,因此,除非您明确为其指定初始位置,否则固定布局只会为它们分配一个随机的位置。 Note too that with all the nodes being fixed, there's no real reason to use a force directed layout. 还要注意的是,在所有节点都固定的情况下,没有真正的理由使用力导向布局。

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

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