Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3js multiple parent nodes

Tags:

d3.js

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.

like image 633
dewastator Avatar asked Jul 02 '15 13:07

dewastator


1 Answers

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.

like image 145
JSBob Avatar answered Nov 15 '22 13:11

JSBob