{
let maxTotal=0;
nestedData.forEach(function(yearGroup) {
yearGroup.year = +yearGroup.key;
yearGroup.bars = [];
var total = 0;
yearGroup.values.forEach(function(row) {
yearGroup.bars.push(
{
year:+row.Year,
sum: +row.Sum,
species: "Lutra_lutra",
use: row.Use,
y0: total,
y1: (total = total + +row.Lutra_lutra)
}
);
yearGroup.bars.push(
{
year:+row.Year,
sum: +row.Sum,
species: "Not_identified",
use: row.Use,
y0: total, //total value *before* this bar
y1: (total = total + +row.Not_identified) //new total
}
);
yearGroup.bars.push(
{
year:+row.Year,
sum: +row.Sum,
species: "Unsure",
use: row.Use,
y0: total, //total value *before* this bar
y1: (total = total + +row.Unsure) //new total
}
);
yearGroup.bars.push(
{
year:+row.Year,
sum: +row.Sum,
species: "Multiple",
use: row.Use,
y0: total, //total value *before* this bar
y1: (total = total + +row.Multiple) //new total
}
);
yearGroup.bars.push(
{
year:+row.Year,
sum: +row.Sum,
species: "Aonyx_cinereus",
use: row.Use,
y0: total, //total value *before* this bar
y1: (total = total + +row.Aonyx_cinereus) //new total
}
);
yearGroup.bars.push(
{
year:+row.Year,
sum: +row.Sum,
species: "Lutrogale_perspicillata",
use: row.Use,
y0: total, //total value *before* this bar
y1: (total = total + +row.Lutrogale_perspicillata) //new total
}
);
yearGroup.bars.push(
{
year:+row.Year,
sum: +row.Sum,
species: "Lutra_sumatrana",
use: row.Use,
y0: total, //total value *before* this bar
y1: (total = total + +row.Lutra_sumatrana) //new total
}
);
});
maxTotal = Math.max(maxTotal, total); //update max
});
return [maxTotal, nestedData]
}