Being a beginner, I am seeking assistance with my variable chartdata that serves as a basis:
var chartdata = {
labels: [
"Bar 1",
"Bar 2",
"Bar 3",
"Bar 4",
],
datasets: [
{
label: "Green data",
type: "bar",
data: [20, 30, 40, 20]
},
{
label: "Red data",
type: "bar",
data: [10, 30, 20, 10]
},
{
label: "Blue data",
type: "bar",
data: [20, 20, 20, 20]
},
Since I have a stacked barchart, I am interested in knowing how I can sum the data points for "Bar 1," which are 20, 10, 20 = 50.
Initially, I attempted using an array, which was as follows:
const array0 = chartdata.datasets[0].data;
console.log(array0.reduce((a, b) => a + b, 0));
...however, this only added 20 + 30 + 40 + 20, encompassing all the data within dataset[0] and similar operations. This meant I could only obtain the sum of all Green data points or others, but not the total sum for an individual stacked bar.
It seemed unattainable to perform an operation like chartdata.datasets[0 to 4].data[always 0]; which would provide the overall value of "Bar 1" on the y-axis.
I attempted to create something using .map, but encountered difficulties in achieving it independently.
Perhaps you, as experts, could offer me a solution that I can immediately execute.
Thank you in advance.