Skip to content

Commit

Permalink
Merge pull request #375 from PEM-Humboldt/feature/percentageTooltipBa…
Browse files Browse the repository at this point in the history
…ckend

Add percentage to graph from backend
  • Loading branch information
hacheG authored Oct 2, 2023
2 parents fb10423 + 5590e1b commit bb0db65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions services/backend/src/service/environmental_authority.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ module.exports = (eaPersistence, seService) => {
values[key] += area.sum;
}
});
const areaAdd = values.reduce((prev, nex) => prev + nex, 0);

return keys.map((fc, idx) => ({ key: fc, area: values[idx] }));
return keys.map((fc, idx) => ({
key: fc,
area: values[idx],
percentage: values[idx] / areaAdd,
}));
},

/**
Expand All @@ -45,7 +50,13 @@ module.exports = (eaPersistence, seService) => {
*/
getAreaByBioticUnit: async (envAuthorityId) => {
const data = await eaPersistence.findAreaByBioticUnit(envAuthorityId);
return data.map((datum) => ({ ...datum, area: Number(datum.area) }));
const dataToNumber = data.map((e) => Number(e.area));
const areaAdd = dataToNumber.reduce((prev, nex) => prev + nex);
return data.map((datum) => ({
...datum,
area: Number(datum.area),
percentage: Number(datum.area) / areaAdd,
}));
},

/**
Expand All @@ -55,7 +66,11 @@ module.exports = (eaPersistence, seService) => {
*
* @returns {Object[]} total area for each biome
*/
getAreaByBiome: async (envAuthorityId) => eaPersistence.findAreaByBiome(envAuthorityId),
getAreaByBiome: async (envAuthorityId) => {
const data = await eaPersistence.findAreaByBiome(envAuthorityId);
const areaAdd = data.reduce((pre, nex) => pre + nex.area, 0);
return data.map((e) => ({ area: e.area, key: e.key, percentage: e.area / areaAdd }));
},

/**
* Get total area grouped by sub-basin given environmental authority filtered by a biome
Expand Down
2 changes: 1 addition & 1 deletion services/forest/src/service/forestLP.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = (ForestLPPersistence, restAPI) => {
const totalArea = item.map((o) => o.area).reduce((prev, next) => prev + next);
const periodData = item.map((period) => ({
...period,
percentage: period.area / totalArea,
percentage: (period.area / totalArea) * 100,
}));
data.push({
id: periods[i],
Expand Down

0 comments on commit bb0db65

Please sign in to comment.