JSON中出现次数的条形图

时间:2014-05-13 11:28:52

标签: javascript json d3.js

我有一个政治家的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做到这一点?谢谢!

1 个答案:

答案 0 :(得分:1)

构建您想要的数据:

var d = {};
records.forEach(function(i)  {
  if(d[i.party] == null) {
    d[i.party] = 0;
  }
  d[i.parts] += 1;
});