简体   繁体   English

D3强制图渲染节点和链接

[英]D3 force graph rendering nodes and links

I am having a problem with my force graph which I have used d3 to create. 我的强制图表有问题,我用d3来创建。 I am dynamically adding to the nodes and links array with data I retrieve using d3.json. 我动态地添加节点和链接数组与我使用d3.json检索的数据。 My problem comes after the initial rendering and I add more nodes and links to their respective arrays. 我的问题出现在初始渲染之后,我添加了更多节点和链接到它们各自的数组。 The links that are added have been rendered "on-top" of the node. 添加的链接已呈现在节点的“顶部”。 Unfortunately my nodes consist of images so the link is covering part of that image. 不幸的是,我的节点由图像组成,因此链接覆盖了该图像的一部分。 The solution I have come up with is to select everything (nodes and links) inside my svg container and remove them on each update() call and re-enter each element again. 我提出的解决方案是选择我的svg容器中的所有内容(节点和链接),并在每次update()调用时删除它们,然后再次重新输入每个元素。 This seems like an inefficient way to go about it. 这似乎是一种低效的方式。 I have tampered with z-index but to no avail. 我篡改了z-index但无济于事。 Are there any suggestions? 有什么建议吗?

If you always PREpend any link (ie add the links to the container before any of the nodes), there will never be a link overlapping a node. 如果您总是预置任何链接(即在任何节点之前添加到容器的链接),则永远不会有与节点重叠的链接。 You do that with insert() , which will prepend as long as you specify a selector matching the nodes you want to keep in on top: 您可以使用insert()执行此操作,只要您指定与要保留在顶部的节点匹配的选择器,它就会前置:

forceContainer.selectAll('.node').data(force.nodes())
forceContainer.enter()
  .append('div')
  .attr('class', 'node')

forceContainer.selectAll('.link').data(force.links())
forceContainer.enter()
  .insert('div', '.node') // Inserts link before any of the existing nodes

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

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