vis.js Level sorting in Hierarchical Layout

5.3k views Asked by At

I have a fairly simple hierarchical structure of nodes, but when vis.js draws them, the order of nodes on each level doesn't make much sense - there are a lot of crossed edges (screenshot: Default Layout )

I am hoping to get a layout similar to that given here: Expected Layout

My vis.js options are as follows;

{
    "layout": {
        "hierarchical": {
            "direction": "LR",
            "sortMethod": "directed",
            "nodeSpacing": 200,
            "treeSpacing": 400
        }
    },
    "edges": {
        "smooth": {
            "type": "continuous"
        }
    },
    "nodes": {
        "physics": false
    }
};

What is the best method to produce this sorted layout?

2

There are 2 answers

1
Asaf On

you should remove the quotation marks. these are object's properties, not strings. it should look like this:

layout: {
    hierarchical: {
        direction: "LR",
        sortMethod: "directed",
        nodeSpacing: 200,
        treeSpacing: 400
    }
},
edges: {
    smooth: {
        type: "continuous"
    }
},
nodes: {
    physics: false
}
0
immelman On

I suggest your try enabling physics, which will sort out the edges crossing, etc.

However, in hierarchical layout, it's a good idea to disable the engine once it's done the first iterations by catching the 'stabilizationIterationsDone' event as follows:

network.on("stabilizationIterationsDone", function(){
  network.setOptions( { physics: false } );
});