I am trying to create a stacked bar graph, but I can't figure out how to reshape my data.
The data coming from the backend looks something like below, where it has a date and some category key
[
{date: 'January', category: 'A'},
{date: 'January', category: 'B'},
{date: 'February', category: 'A'},
{date: 'February', category: 'B'},
{date: 'February', category: 'C'},
{date: 'February', category: 'B'},
...
]
I need to reshape this data into the form like below, where it will group them into their respective months, and then count the number of times each category was seen
[
{name: 'January', countA: 1, countB: 1, countC: 0 },
{name: 'February', countA: 1, countB: 2, countC: 1 }
...
]
So far, I've only been able to count the total number of categories per month using the reduce function such as
const filterData = data.reduce((groups, curr) => {
const {January = 0, February = 0, ...} = groups;
switch(curr.date){
case 'January': return {...groups, January: January + 1}
case 'February': return {...groups, February: February + 1}
...
}
}, {})
But this won't work for the stacked bar graph.