I have used ApexCharts in my Angular 16 application.
Here my response is not appending on the chart. Please help me to modify the API response.
Response from API:
let data = [
{
tenantName: 'OBC+',
labelName: 'Application',
total: 85,
postiveTotal: '21',
negativeTotal: '64',
},
{
tenantName: 'Discovery-world',
labelName: 'Application',
total: 194,
postiveTotal: '119',
negativeTotal: '75',
},
{
tenantName: 'OBC+',
labelName: 'Channels',
total: 40,
postiveTotal: '0',
negativeTotal: '40',
},
{
tenantName: 'Discovery-world',
labelName: 'Channels',
total: 59,
postiveTotal: '0',
negativeTotal: '59',
},
];
I need to modify my response with this format.
Expected Format:
this.stackedChartData = [
{
name: 'OBC Postivie',
group: 'OBC',
data: [21, 0],
},
{
name: 'OBC Negative',
group: 'OBC',
data: [64, 40],
},
{
name: 'Discovery-world Postivie',
group: 'Discovery-world',
data: [119, 0],
},
{
name: 'Discovery-world Negative',
group: 'Discovery-world',
data: [75, 59],
},
];
I have tried with this code but it is not working properly.
let labels = [...new Set(data.map((x: any) => x.labelName))];
let subLabels = data.reduce((acc, cur: any) => {
if (
acc.findIndex(
(x) =>
//console.log(x)
x.tenantName == cur.tenantName && x.labelName == cur.labelName
) == -1
)
acc.push({
tenantName: cur.tenantName,
labelName: cur.labelName,
postiveTotal: cur.postiveTotal,
negativeTotal: cur.negativeTotal,
});
return acc;
}, [] as { tenantName: string; labelName: string; postiveTotal: number; negativeTotal: number }[]);
console.log(subLabels);
I am going to try postiveTotal, negativeTotal with group of labelName.
This is my chart demo link.
You are in the right way to iterate and transform the array via
.reduce().Changes that you need:
The result of the array should be in
{ group: string; name: string; data: number[] }[]type.Check the
groupwhether it was inserted previously. If not inserted, you should add two objects with "Positive" and "Negative".If the
groupis inserted previously, get the objects bygroupandnameand add the value intodataarray in the object.Demo @ StackBlitz