分组嵌套的达达

时间:2018-09-15 20:38:57

标签: javascript d3.js

假设您有这样的csv数据文件:

    name, age
    Bob, 27
    George, 25
    Bill, 22
    Henry,27
    Carol,25
    Mary, 28
    Harold,27
    Jane, 25

我想按年龄汇总条形图的总数。这样我就得出了这样的总数:27岁(3),25岁(2)等。

我正在使用d3 v4

1 个答案:

答案 0 :(得分:0)

如评论中所述,您可以使用d3.nest

const data = [
  {name: 'Bob', age: 27},
  {name: 'George', age: 25},
  {name: 'Bill', age: 2},
  {name: 'Henry', age: 27},
  {name: 'Carol', age: 25},
  {name: 'Mary', age: 28},
  {name: 'Harold', age: 27},
  {name: 'Jane', age: 25},
];

console.log(d3.nest()
  .key(d => d.age)
  .rollup(results => results.length)
  .entries(data));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script>