Grouping nested dada

3841 views d3.js
7

Say you have csv data file like this:

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

I want to aggregate totals by age for a bar chart. So that I get totals like this: age 27(3), age 25(2), etc.

I am using d3 v4

answered question

look at d3.nest

1 Answer

7

As mentioned in the comments, you could use 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>

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories