skayred
skayred

Reputation: 10713

D3 tree with predefined depths

I want to create tree with predefined depths with D3.

Is there a way to set depth before tree is generated?

Upvotes: 3

Views: 2201

Answers (1)

mbostock
mbostock

Reputation: 51839

It depends on your definition of “tree”. D3 has several hierarchy layouts, of which d3.layout.tree is one. The tree layout refers to Reingold–Tilford’s tidy tree layout algorithm. This particular algorithm is not conducive to customizing the depth of nodes because it assumes that all siblings are the same depth (so that it can place the nodes tidily).

The d3.layout.cluster, in contrast, can be easily modified to render nodes at custom depths. Simply ignore the generated d.y coordinate and substitute your own depth value (probably in conjunction with a linear scale to map from data to pixels). For an example of this technique, see Ken-ichi Ueda’s right-angle phylograms.

Upvotes: 5

Related Questions