Maybe not the best of most efficient solution, but here is what I came up with. Suggestions are welcome...
function addBackground(graph) {
graph.canvas.canvases[1].clear();
graph.canvas.canvases[1].opt.levelDistance = graph.config.levelDistance;
graph.canvas.canvases[1].opt.numberOfCircles = graph.config.background.numberOfCircles;
graph.canvas.scale(1, 1);
return graph;
}
//Calculate a proper levelDistance so that the nodes don't overlap
var minSpan = Math.PI * 2;
var maxDepth = 0;
rgraph.graph.eachNode(function(n) {
if (n._depth == 1 && n.endData.$span < minSpan) {
minSpan = n.endData.$span;
}
if (n._depth > maxDepth) {
maxDepth = n._depth;
}
});
if (75 / minSpan > rgraph.config.levelDistance) {
rgraph.config.levelDistance = 75 / minSpan;
//TODO Calculate a proper initial zoomLevel to fit all nodes
rgraph.compute('end');
}
rgraph.config.background.levelDistance = rgraph.config.levelDistance;
rgraph.config.background.numberOfCircles = maxDepth;
rgaph = addBackground(rgraph);