问题
I am trying to use this code from sunburst diagram to work for my data. Originally my data is from csv which I have converted to json with the help of d3.nest()
d3.csv("chord.csv", function(error,csv_data){
var sunData1 = {"key": "KINGDOM", "values": d3.nest()
.key(function(d) { return d.KINGDOM; })
.key(function(d) { return d.PHYLUM; })
.key(function(d) { return d.CCLASS; })
.key(function(d) { return d.ORDER; })
.key(function(d) { return d.FAMILY; })
.key(function(d) { return d.GENUS; })
.rollup(function(v) {return v.length;})
.entries(csv_data)
};
This gives me labels like
[{"key":"Animalia","values":[{"key":"Chordata","values":[{"key":"Mammalia","values":[{"key":"Chiroptera","values":[{"key":"Vespertilionidae","values":[{"key":"Myotis","values":496},
But in the example, the labels are name, children and size
By assistance from this post I am using the map(function) to change the label to desired as in flare.json. The code for that is here
var sunData2 = {"name":"KINGDOM", "children": sunData1.values.map(function (kingdom){
return {"name":kingdom.key, "children": kingdom.values.map(function(phylum){
return {"name":phylum.key, "children": phylum.values.map(function(cclass){
return{"name":cclass.key, "children": cclass.values.map(function(order){
return {"name":order.key, "children": order.values.map(function(family){
return {"name":family.key, "children": family.values.map(function(genus){
return {"name":genus.key, "children": genus.values};
})};
})};
})};
})};
})};
})};
Now, I am able to change it into the desired format, but now it is changing the size which is "values" in my json into "children" like here
{"name":"KINGDOM","children":[{"name":"Animalia","children":[{"name":"Chordata","children":[{"name":"undefined","children":[{"name":"Chiroptera","children":[{"name":"Vespertilionidae","children":[{"name":"Myotis","children":496},
So, I am looking for some suggestions so that I can change the "children" referring to the count to something distinct, which I can then map in my visualization code.
Issue resolved, I have to delete the data files and comments referring to the data files
回答1:
I am not sure if I am able to get your question correct when you say I am trying to get a different label to the size value instead of children:
But here is my attempt to give count to each node and use it for drawing the arcs:
function makeSize(json){
if (json.children instanceof Array){
json.count = json.children.length;//if array make it count equal to child count
json.children.forEach(function(d){
makeSize(d);//call recursive for all children
});
} else {
if (isFinite(json.children))
json.count = json.children;//if number store that in the count
else
json.count = 0;//if nothing make the count 0
}
}
Click on count radio.
来源:https://stackoverflow.com/questions/35120044/changing-labels-of-json-for-d3-sunburst-diagram