vis.js Level sorting in Hierarchical Layout

nnx*_*xen 7 javascript vis.js vis.js-network

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
    }
};
Run Code Online (Sandbox Code Playgroud)

What is the best method to produce this sorted layout?

小智 -4

你应该删除引号。这些是对象的属性,而不是字符串。它应该看起来像这样:

layout: {
    hierarchical: {
        direction: "LR",
        sortMethod: "directed",
        nodeSpacing: 200,
        treeSpacing: 400
    }
},
edges: {
    smooth: {
        type: "continuous"
    }
},
nodes: {
    physics: false
}
Run Code Online (Sandbox Code Playgroud)

  • 使用引号是有效的语法,因此这不会成为任何问题的根源(此外,这在 JSON 中是必需的,它是有效的 JS) (2认同)