d3.js Zoomable Sunburst visualization from self-referencing CSV input

∥☆過路亽.° 提交于 2019-12-04 18:45:31

Got it. We include these scripts from the DataStructures.Tree project linked in the question : base.js, DataStructures.Tree.js. (you'll find them in /js/lib/ and /js/vendor/)

<script type="text/javascript" src="base.js"></script>
<script type="text/javascript" src="DataStructures.Tree.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>

Then, we replace this line,

d3.json("flare.json", functon(error, root) {

..with these lines:

d3.csv("electrical5.csv", function(data){
var tree = DataStructures.Tree.createFromFlatTable(data),
           root = tree.toSimpleObject(function(objectToDecorate, originalNode) {
                objectToDecorate.size = originalNode.size;
                if (objectToDecorate.children && objectToDecorate.children.length == 0) {
                    delete objectToDecorate.children;
                }
                return objectToDecorate;
            });
//console.log(JSON.stringify(root));

leave everything else just as it is.


Uncomment the console.log line for debugging; it will put the json code in the browser console when you load the page. You can also make a textarea in the webpage and output the json code in that.

document.getElementById("SHOWME").value = JSON.stringify(root);

[at bottom of page]

<textarea id="SHOWME"></textarea>

it will be unformatted json code, so copy-paste it to http://codebeautify.org/jsonviewer and that ought to give you a well-formatted json.


To get more columns in apart from the regular [id,parentId,name,size] , we have to edit DataStructures.Tree.js

Locate these lines:

simpleChildRepresentation.push(decorateNode({
    "name" :  node.name,
    "children" : children
}, node));

And insert the extra columns in the same format as the node.name line.

simpleChildRepresentation.push(decorateNode({
    "name" :  node.name,
     //CUSTOM COLUMNS
    "workcode" : node.id,
    "parentcode" : node.parentId,
    "department" : node.department,
    "totalorextract" : node.totalorextract,
    "total" : node.total,
    "pages" : node.pages,
    //CUSTOM COLUMNS DONE
    "children" : children
}, node));

You can now directly visualize self-referencing csv data on any d3.js visualization that uses flare.json . Animations get a bit clunkier, though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!