viz5={
const Plotly = await require("https://cdn.plot.ly/plotly-latest.min.js");
const createDropdown = (name, options) => {
const select = document.createElement("select");
select.name = name;
options.forEach(option => {
const optionElement = document.createElement("option");
optionElement.value = option;
optionElement.text = option;
select.appendChild(optionElement);
});
select.onchange = updateChart;
return select;
};
const data = ipums_df;
const raceOptions = ["All", "White", "Black", "Asian", "Hispanic", "Native American"];
const employmentStatusOptions = ["All", "Employed", "Unemployed", "Retired"];
const raceDropdown = createDropdown("Race", raceOptions);
const employmentStatusDropdown = createDropdown("Employment Status", employmentStatusOptions);
const chartContainer = document.createElement("div");
chartContainer.style.width = "100%";
chartContainer.style.height = "600px";
function updateChart() {
const selectedRace = raceDropdown.value;
const selectedEmploymentStatus = employmentStatusDropdown.value;
// Filter data based on selected race and employment status
const filteredData = data.filter(entry => {
let include = true;
if (selectedRace !== "All" && entry.RACE !== selectedRace) include = false;
if (selectedEmploymentStatus !== "All" && entry.EMPSTAT !== selectedEmploymentStatus) include = false;
return include;
});
// Define income brackets and educational levels for the heatmap
const incomeBrackets = ["<25k", "25k-50k", "50k-75k", "75k-100k", ">100k"];
const educationLevels = ["High School Diploma", "Some College", "Bachelor's Degree", "Master's Degree", "Ph.D.", "Associate's Degree"];
// Calculate counts for the heatmap matrix
const heatmapMatrix = educationLevels.map(education => {
return incomeBrackets.map(bracket => {
return filteredData.filter(entry => {
const income = parseInt(entry.INCOME.replace(/[^0-9]/g, ''), 10); // Clean income data
let incomeInBracket = false;
if (bracket === "<25k") incomeInBracket = income < 25000;
if (bracket === "25k-50k") incomeInBracket = income >= 25000 && income < 50000;
if (bracket === "50k-75k") incomeInBracket = income >= 50000 && income < 75000;
if (bracket === "75k-100k") incomeInBracket = income >= 75000 && income < 100000;
if (bracket === ">100k") incomeInBracket = income >= 100000;
return entry.EDUC === education && incomeInBracket;
}).length;
});
});
// Define the heatmap trace with a different color scheme (Blues)
const trace = {
z: heatmapMatrix,
x: incomeBrackets,
y: educationLevels,
type: 'heatmap',
colorscale: 'Cividis' // Updated color scheme
};
// Define layout for the heatmap
const layout = {
title: 'Income Bracket Distribution by Employment Status and Education',
xaxis: { title: 'Income Brackets' },
yaxis: { title: 'Education Levels' }
};
// Plot the heatmap
Plotly.newPlot(chartContainer, [trace], layout);
}
// Create UI container for the dropdown filters
const uiContainer = document.createElement("div");
uiContainer.appendChild(document.createTextNode("Select Filters: "));
uiContainer.appendChild(raceDropdown);
uiContainer.appendChild(employmentStatusDropdown);
// Append UI and chart containers
const container = document.createElement("div");
container.appendChild(uiContainer);
container.appendChild(chartContainer);
// Initial rendering of the chart
updateChart();
return container;
}