为d3 sunburst图更改json的标签 [英] Changing labels of json for d3 sunburst diagram
问题描述
我尝试使用sunburst图表中的此代码为我的工作数据。最初我的数据来自csv,我已经转换为json与帮助d3.nest()
d3.csv( chord.csv,function(error,csv_data){
var sunData1 = {key:KINGDOM,values:d3.nest()
.key 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)
};
这给我的标签是
[{key: Animal,values:[{key:Chordata,values:[{key:Mammalia,values {key:Vespertilionidae,values:[{key:Myotis,values:496},
但在这个例子中,标签是name,children和size。 stackoverflow.com/questions/17416186/d3-nest-key-and-values-conversion-to-name-and-children \">post 我使用地图(函数)将标签更改为所需的flare.json。这里的代码是
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(order){
return {name:cclass.key,children name:order.key,children:order.values.map(function(family){
return {name:family.key,children:family.values.map ){
return {name:genus.key,children:genus.values};
})};
})};
}) b $ b})};
})};
})};
现在,我可以将其更改为所需的格式,
{name:KINGDOM,children: [{name:Animalia,children:[{name:Chordata,children:[{name:undefined,children:[{name:Chiroptera ,children:[{name:Vespertilionidae,children:[{name:Myotis,children:496},
pre>
所以,我正在寻找一些建议,所以我可以改变孩子,指的计数到不同的东西,然后我可以映射在可视化代码。
问题已解决,我必须删除指向数据文件的数据文件和注释。
解决方案我不确定是否能够在您说
时得到正确的问题我试图为大小值而不是儿童获取不同的标签:
但这里是我试图给每个节点计数,并用它来绘制弧线:
function makeSize(json){
if(json.children instanceof Array){
json.count = json.children.length;如果数组使其计数等于子计数
json.children.forEach(function(d){
makeSize(d); //对所有子进程调用递归
});
} else {
if(isFinite(json.children))
json.count = json.children; //如果数字存储在计数
else
json.count = 0; //如果没有计数0
}
}
点击计数广告。
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
解决方案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.
这篇关于为d3 sunburst图更改json的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!