D3js多个父节点 [英] D3js multiple parent nodes
问题描述
我正在尝试d3和树形布局.我认为是否有可能创建具有两个根的树.我知道这是规则,树只有一个根,但也许有人会举一些例子.
I am experimenting with d3 and tree layout. I think if it is possible to create tree with for example two roots. I know that it is rule that tree has only one root but maybe someone has some example.
推荐答案
这是一个小提琴,显示了什么我认为您正在寻找.重要代码就在底部附近.
Here's a Fiddle showing what I think you're looking for. The important code is right near the bottom.
node.each(function(d){
if (d.name == "flare")
d3.select(this).remove();});
link.each(function(d){
if (d.source.name == "flare")
d3.select(this).remove();});
这只是使用来自d3树示例之一的样本数据,其中根节点的字段name
为flare
.根据您自己的数据集以及node
和link
变量的名称(分别包含g
和path
对象)进行相应的调整.本质上,这是如何工作的,方法是创建一个具有单个根节点的树,然后删除该节点及其留下子级的链接,并根据需要提供任意数量的伪根.
This is just using the sample data from one of the d3 tree examples, where the root node has the field name
as flare
. Adjust accordingly for your own data set, as well as the name of the node
and link
variables (containing the g
and path
objects respectively). Essentially how this is works is by creating a tree with a single root node and then removing that node and its links which leaves the children, allowing for as many pseudo-roots as you want.
这篇关于D3js多个父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!