generated from UNDP-Data/undp-visualization-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7426108
commit ba61faa
Showing
4 changed files
with
208 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { PROGRAMMES } from './Constants'; | ||
|
||
export interface ProgrammeCount { | ||
value: string; | ||
label: string; | ||
count: number; | ||
subprogrammes?: ProgrammeCount[]; | ||
} | ||
|
||
const calculateCount = (data: any[], value: string): number => { | ||
return data.reduce((sum, item) => sum + (item[value] || 0), 0); | ||
}; | ||
|
||
const calculateSubprogrammeCounts = ( | ||
data: any[], | ||
subprogrammes: any[] = [], | ||
): ProgrammeCount[] => { | ||
return subprogrammes.map(subProg => { | ||
const subprogrammeCounts = subProg.subprogrammes | ||
? calculateSubprogrammeCounts(data, subProg.subprogrammes) | ||
: undefined; | ||
|
||
const subProgrammeCount = subprogrammeCounts | ||
? subprogrammeCounts.reduce((acc, sub) => acc + sub.count, 0) | ||
: calculateCount(data, subProg.value); | ||
|
||
return { | ||
value: subProg.value, | ||
label: subProg.short, | ||
count: subProgrammeCount, | ||
subprogrammes: subprogrammeCounts, | ||
}; | ||
}); | ||
}; | ||
|
||
export const calculateProgrammeCounts = (data: any[]): ProgrammeCount[] => { | ||
const mainProgrammes = PROGRAMMES[0].subprogrammes || []; | ||
|
||
const programmeCounts: ProgrammeCount[] = mainProgrammes.map(prog => { | ||
const subprogrammeCounts = calculateSubprogrammeCounts( | ||
data, | ||
prog.subprogrammes, | ||
); | ||
const mainProgrammeCount = | ||
subprogrammeCounts.reduce((acc, sub) => acc + sub.count, 0) + | ||
calculateCount(data, prog.value); | ||
|
||
return { | ||
value: prog.value, | ||
label: prog.short, | ||
count: mainProgrammeCount, | ||
subprogrammes: subprogrammeCounts, | ||
}; | ||
}); | ||
|
||
const allProgrammesCount = programmeCounts.reduce( | ||
(acc, programme) => acc + programme.count, | ||
0, | ||
); | ||
|
||
return [ | ||
{ | ||
value: 'all_programmes', | ||
label: 'All Programmes', | ||
count: allProgrammesCount, | ||
subprogrammes: programmeCounts, | ||
}, | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters