简体   繁体   English

d3强制布局节点之间的固定链接

[英]d3 force layout fixed links between nodes

I have a piece of code to display a force diagram which can be found in this link - https://jsfiddle.net/u49s9sth/3/ 我有一段代码来显示一个力图,可以在此链接中找到它-https: //jsfiddle.net/u49s9sth/3/

In the above case source data is represented as 在上述情况下,源数据表示为

var nodes = [{
    id: 0,
    reflexive: false,
    "x": 169,
    "y": 110,
    name: "event"
  }, {
    id: 1,
    reflexive: false,
    "x": 369,
    "y": 110,
    name: "email"
  }],
  lastNodeId = 1,
  links = [{
    source: {
    id: 0,
    reflexive: false,
    "x": 169,
    "y": 110,
    name: "event"
  },
    target: {
    id: 1,
    reflexive: false,
    "x": 369,
    "y": 110,
    name: "email"
  },
    left: false,
    right: true
  }];

In this case the link between the nodes shrinks after it is displayed to a smaller size. 在这种情况下,节点之间的链接在显示为较小尺寸后会缩小。 But with the same code , if i give the source data as 但是使用相同的代码,如果我给出源数据为

var nodes = [{
    id: 0,
    reflexive: false,
    "x": 169,
    "y": 110,
    name: "event"
  }, {
    id: 1,
    reflexive: false,
    "x": 369,
    "y": 110,
    name: "email"
  }],
  lastNodeId = 1,
  links = [{
    source: nodes[0],
    target: nodes[1],
    left: false,
    right: true
  }];

That is referring the data from nodes directly it will work fine - Here is the fiddle for the actual result- https://jsfiddle.net/u49s9sth/4/ 那就是直接从nodes引用数据,它将正常工作-这是实际结果的小提琴-https : //jsfiddle.net/u49s9sth/4/

Why does this happen ? 为什么会这样? How can i achieve the second result by giving data directly ? 如何通过直接提供数据来获得第二个结果?

In your first block of data the link is not really a link because with the way you specify the data d3 never actually has it connect the two nodes. 在您的第一个数据块中, 链接并不是真正的链接,因为使用您指定数据d3的方式时,它实际上并没有连接两个节点。 It then just ends up behaving like a node, subject to the forces in layout and it shrinks away from the nodes. 然后,它最终表现得像节点一样,受到布局中的力的作用,并且远离节点收缩。

So, how do we get it to behave like a true link? 那么,我们如何使其表现得像真正的链接? You are kind of limited here and you have two options on how to give d3 the data. 您在这里有一定的局限性,关于如何给d3数据有两种选择。 The first is how you have it in your second snippet, specify source and target as node objects. 第一个是在第二个片段中的使用方式,将源和目标指定为节点对象。 The second is specify source and target as indexes into the nodes array: 第二个是指定源和目标作为节点数组的索引:

var nodes = [{
    id: 0,
    reflexive: false,
    "x": 169,
    "y": 110,
    name: "event"
  }, {
    id: 1,
    reflexive: false,
    "x": 369,
    "y": 110,
    name: "email"
  }],
  lastNodeId = 1,
  links = [{
    source: 0, 
    target: 1,
    left: false,
    right: true
   }];

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

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