chart = {
selectedGenders;
selectedRaces;
selectedAges;
stackBy;
filteredData;
diseaseCounts;
const diseases = ["HeartDisease", "Stroke", "Asthma", "KidneyDisease", "Diabetic", "SkinCancer"];
const title = `Prevalence of Diseases${
selectedGenders.includes("All") ? "" : ` for ${selectedGenders.join(", ")}`
}${
selectedRaces.includes("All") ? "" : `, ${selectedRaces.join(", ")}`
}${
selectedAges.includes("All") ? "" : `, aged ${selectedAges.join(", ")}`
}`;
if (stackBy !== "None") {
const stackColumnMap = {
Gender: "Sex",
Race: "Race",
AgeCategory: "AgeCategory"
};
const stackColumn = stackColumnMap[stackBy];
const groups = Array.from(new Set(filteredData.map(d => d[stackColumn]).filter(d => d)));
const groupedCounts = groups.flatMap(group => {
const subset = filteredData.filter(d => d[stackColumn] === group);
const total = subset.length;
return diseases.map(disease => ({
group: group ?? "Unknown",
disease,
percent: total > 0 ? subset.filter(d => d[disease] === "Yes").length / total * 100 : 0
}));
});
return Plot.plot({
title,
y: { label: "Prevalence (%)" },
x: { label: "Disease" },
color: { legend: true, label: stackBy, type: "categorical" },
marks: [
Plot.barY(groupedCounts, {
x: "disease",
y: "percent",
fill: "group",
stack: true,
tip: true
}),
Plot.ruleY([0])
]
});
} else {
return Plot.plot({
title,
y: { label: "Prevalence (%)" },
x: { label: "Disease" },
marks: [
Plot.barY(diseaseCounts, {
x: "disease",
y: "percent",
fill: "#4e79a7",
tip: true
}),
Plot.ruleY([0])
]
});
}
}