我有一个政治家的JSON,一个包含他们的名字和党的名单。
很简单,我想要做的是制作条形图,条形图的大小取决于来自不同政党的政治家数量。
键看起来像这样:
{
"party": "National Liberal Party – True Liberalism",
"region": "london",
"name": "Bernard Dube",
"twitter": ""
},
{
"party": "Alliance Party of Northern Ireland",
"region": "northern-ireland",
"name": "Anna Lo",
"twitter": "Anna4Europe"
},
不幸的是,它并不像以下那么简单:
{
"National Liberal Party": "330",
"Alliance Party of Northern Ireland": "200"
}
我怎么能用d3做到这一点?谢谢!
答案 0 :(得分:1)
构建您想要的数据:
var d = {};
records.forEach(function(i) {
if(d[i.party] == null) {
d[i.party] = 0;
}
d[i.parts] += 1;
});