dewastator
dewastator

Reputation: 213

D3js multiple parent nodes

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.

Upvotes: 5

Views: 6310

Answers (1)

JSBob
JSBob

Reputation: 1046

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();});

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.

Upvotes: 10

Related Questions