how to make stacked bar/area in d3

72 Views Asked by At

I'd like to make a stacked bar chart(or stacked area chart) for male and female over time. Male's bar(or area) is below and on top of that there will be female. The data is pretty simple. It has a format like below in CSV:

gender  year    value
male    1999    2845
male    2004    2904
male    2009    3004
male    2014    3056
female  1999    2910
female  2004    3210
female  2009    3340
female  2014    4029

I read it by d3.csv, made nest by year and made some manipulations to adjust it to a format for d3.stack(). Therefore, the final data format looks like this:

Object { male: 2845, female: 2910 }
Object { male: 2904, female: 3210 }
Object { male: 3004, female: 3340 }
Object { male: 3056, female: 4029 }

I tried using d3.stack with the data above but it didn't seem working. I started doubting if this is a right format to fit d3.stack() and if there are better ways to make stacked bar/area chart than this. I was wondering if grouping the data by gender (rahter than year) may make more sense to use d3.stack().

If anyone has any advice to build the stacked bar/area, it will be much appreciated.

Thanks,

0

There are 0 best solutions below