How to load data from an internal JSON array rather than from an external resource / file for a collapsible tree in d3.js?

后端 未结 2 864
孤街浪徒
孤街浪徒 2020-12-14 23:51

I wish to load a JSON data array from within a file to generate a collapsible tree diagram per the Mike Bostock example here. The example uses a correctly formatted external

2条回答
  •  难免孤独
    2020-12-15 00:12

    You are very close, all you need to do is set the treeData you created as the root of the tree. So instead of loading the JSON data, you would do:

    // replace this line
    // d3.json("/d/4063550/flare.json", function(error, flare) {
    root = treeData[0];
    root.x0 = height / 2;
    root.y0 = 0;
    
    function collapse(d) {
        if (d.children) {
            d._children = d.children;
            d._children.forEach(collapse);
            d.children = null;
        }
    }
    
    root.children.forEach(collapse);
    update(root);
    //remove this line
    // });
    

提交回复
热议问题