majic / src /algorithm /DemographicsPieByGender.ts
nolual's picture
Upload 55 files
0c20ea8
raw
history blame
No virus
973 Bytes
interface SocioDemographyItem {
age: string;
gender: string;
percentage: number;
social_group: string;
}
export interface SummaryItem {
label: string;
usage: number;
}
export function generateSummary(data: SocioDemographyItem[]): SummaryItem[] {
const summaryData: SummaryItem[] = [];
function calculateUsage(age: string, gender: string): number {
const filteredData = data.filter(
(item) => item.age === age && item.gender.toUpperCase() === gender
);
const totalPercentage = filteredData.reduce(
(total, item) => total + item.percentage,
0
);
return totalPercentage;
}
const ageGroups = ["15-24", "25-34", "35-49", "50-64", "65-PLUS"];
const genders = ["MALE", "FEMALE"];
for (const gender of genders) {
for (const age of ageGroups) {
const label = `${gender} ${age}`;
const usage = calculateUsage(age, gender);
summaryData.push({ label, usage });
}
}
return summaryData;
}