Calculate levelDistance to keep nodes from overlapping

172 views
Skip to first unread message

Daniel Schaefer

unread,
May 31, 2012, 9:52:52 PM5/31/12
to javascript-information...@googlegroups.com
I am working on a RGraph. I am having trouble trying to compute the right levelDistance for both the nodes and the background canvas so that my nodes and their labels do not overlap. If I knew the maximum arc length of a given node, how would I go about computing a good levelDistance to accomplish this? I would obviously need to loadJSON first, so that each node gets assigned a _treeAngularWidth. With that value, I may be able to find the levelDistance I'm looking for. Assuming I'm able to do that, how do I change the initial values of levelDistance for the nodes and the background canvas?

Daniel Schaefer

unread,
Jun 5, 2012, 10:36:50 PM6/5/12
to javascript-information...@googlegroups.com
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);

Reply all
Reply to author
Forward
0 new messages