I want to generate this:
With this data structure (ids are random, btw not sequential):
var tree = [
{ \"id\": 1, \"name\": \"Me\", \"dob\":
As you show it, your tree data will not allow you to draw the diagram. You are in fact missing some information there:
children
for instance, back to the child's data.In my attempt (https://jsfiddle.net/61q2ym7q/), I am therefore converting your tree to a graph, and then doing various stages of computation to achieve a layout.
This is inspired by the Sugiyama algorithm, but simplified, since that algorithm is very tricky to implement. Still, the various stages are:
organize nodes into layers, using depth-first search. We do this in two steps by making sure that parents are always on a layer above their parent, and then trying to shorten the links when there is more than one layer between child and parent. This is the part where I am not using the exact Sugiyama algorithm, which uses a complex notion of cut points.
then sort the nodes into each layer to minimize crossing of edges. I use the barycenter method for this
finally, while preserving the order above, assign a specific x coordinate for each node, again using the barycenter method
There are lots of things that can be improved in this code (efficiency, by merging some of the loops for instance) and also in the final layout. But I tried to keep it simpler to make it easier to follow...