function createBarChartAll(barcharttitle) {
const barchartcontainer = d3.select("#barchartcontainer");
barchartcontainer.selectAll("*").remove();
split_view.style("display", "flex");
d3.select("#mapcontainer").style("width", "60%");
const buttonContainer = barchartcontainer.append("div");
const aggregatedData = countyRaceGenderAgeData.reduce(
(acc, county) => {
for (const [age, count] of Object.entries(county.ageCounts)) {
acc.ageCounts[age] = (acc.ageCounts[age] || 0) + count;
}
for (const [race, count] of Object.entries(county.raceCounts)) {
acc.raceCounts[race] = (acc.raceCounts[race] || 0) + count;
}
for (const [gender, count] of Object.entries(county.genderCounts)) {
acc.genderCounts[gender] = (acc.genderCounts[gender] || 0) + count;
}
return acc;
},
{ ageCounts: {}, raceCounts: {}, genderCounts: {} }
);
const ageData = Object.entries(aggregatedData.ageCounts).map(
([name, value]) => ({
name,
value
})
);
const raceData = Object.entries(aggregatedData.raceCounts).map(
([name, value]) => ({ name, value })
);
const genderData = Object.entries(aggregatedData.genderCounts).map(
([name, value]) => ({ name, value })
);
// console.log("........");
// console.log(ageData);
// console.log(raceData);
// console.log(genderData);
// Set up SVG within the container
const margin = { top: 100, right: 40, bottom: 10, left: 50 };
// const padding = { top: 50, right: 50, bottom: 50, left: 50 };
const xtranslate = 70;
const ytranslate = 50;
// Update the width and height of the chart
const width = 450 - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;
const svg = barchartcontainer
.append("svg")
.attr("width", "100%")
.attr("height", "100%")
.style("margin", "10px")
.style("padding", "15px 5px")
.append("g");
// .attr("transform", `translate(${margin.left},${margin.top})`);
// Function to create a bar chart
function createMultiBarChart(data, title) {
// Clear previous chart
const filteredData = data.filter((d) => d.value > 0);
if (filteredData.length === 0) {
svg
.append("text")
.attr("x", width / 2 + xtranslate)
.attr("y", height / 2 + ytranslate)
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text("No data available to display");
return;
}
// Clear previous chart
svg.selectAll("*").remove();
// console.log("Loading data for: " + title);
// X scale
const x = d3
.scaleBand()
.range([0, width])
.domain(filteredData.map((d) => d.name))
.padding(0.1);
// Y scale
const y = d3
.scaleLinear()
.range([height, 0])
.domain([0, d3.max(filteredData, (d) => d.value)]);
// Color scale
const color = d3
.scaleOrdinal()
.domain(filteredData.map((d) => d.name))
.range(d3.schemeCategory10);
// Create bars
svg
.selectAll(".bar")
.data(filteredData)
.enter()
.append("rect")
.attr("class", "bar")
.attr("x", (d) => x(d.name) + xtranslate)
.attr("width", x.bandwidth())
.attr("y", (d) => y(d.value) + ytranslate)
.attr("height", (d) => height - y(d.value))
.attr("fill", (d) => color(d.name))
.on("mouseover", function (event, d) {
d3.select(this).attr("stroke", "coral").attr("stroke-width", "3px");
// Show tooltip
tooltip
.style("opacity", 1)
.html(`${d.name}: ${d.value}`)
.style("left", `${event.pageX + 10}px`)
.style("top", `${event.pageY + 10}px`);
})
.on("mouseout", function () {
d3.select(this).attr("stroke", "none");
tooltip.style("opacity", 0);
});
// Add X axis
svg
.append("g")
.attr("transform", `translate(${xtranslate},${height + ytranslate})`)
.call(d3.axisBottom(x))
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", ".15em")
.attr("dy", ".5em")
.attr("transform", "rotate(-45)");
// X-axis title
svg
.append("text")
.attr("class", "x-axis-title")
.attr("text-anchor", "middle")
.attr("x", width / 2 + xtranslate)
.attr(
"y",
height + margin.bottom + margin.top + (title.includes("Race") ? 50 : 0)
)
.style("font-size", "12px")
.text(title);
// Add Y axis
svg
.append("g")
.attr("transform", `translate(${xtranslate}, ${ytranslate})`)
.call(d3.axisLeft(y));
// Y-axis title
svg
.append("text")
.attr("class", "y-axis-title")
.attr("text-anchor", "middle")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2 - xtranslate)
.attr("y", margin.left / 2)
.style("font-size", "12px")
.text("Number of Cases");
// Chart title
svg
.append("text")
.attr("x", width / 2 + xtranslate)
.attr("y", 30)
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text(`${barcharttitle} - ${title}`);
}
buttonContainer
// .style("position", "relative")
.style("justify-content", "center")
.style("display", "flex")
.style("flex-direction", "row")
// .style("top", "20px") // Adjust the vertical position
// .style("right", "20px") // Adjust the horizontal position
.style("width", "100%")
.style("background", "rgba(255, 255, 255, 0.8)")
.style("z-index", "10");
// Styling individual buttons
buttonContainer
.append("button")
.text("Age")
.style("padding", "10px 20px")
.style("margin", "5px 10px")
.style("font-size", "12px")
.style("background-color", "#CBC3E3")
.style("color", "#342047")
.style("border", "none") // Remove border
.style("border-radius", "5px") // Rounded corners
.style("cursor", "pointer") // Pointer cursor on hover
.style("transition", "background-color 0.3s") // Smooth background color transition
.on("click", () => createMultiBarChart(ageData, "Age Distribution"))
.on("mouseover", function () {
d3.select(this).style("background-color", "#B8A8D4");
})
.on("mouseout", function () {
d3.select(this).style("background-color", "#CBC3E3"); // Revert back on mouseout
});
buttonContainer
.append("button")
.text("Race")
.style("padding", "10px 20px")
.style("margin", "5px 10px")
.style("font-size", "12px")
.style("background-color", "#CBC3E3")
.style("color", "#342047")
.style("border", "none") // Remove border
.style("border-radius", "5px") // Rounded corners
.style("cursor", "pointer") // Pointer cursor on hover
.style("transition", "background-color 0.3s") // Smooth background color transition
.on("click", () => createMultiBarChart(raceData, "Race Distribution"))
.on("mouseover", function () {
d3.select(this).style("background-color", "#B8A8D4");
})
.on("mouseout", function () {
d3.select(this).style("background-color", "#CBC3E3");
});
buttonContainer
.append("button")
.text("Biological Sex")
.style("padding", "10px 20px")
.style("margin", "5px 10px")
.style("font-size", "12px")
.style("background-color", "#CBC3E3")
.style("color", "#342047")
.style("border", "none") // Remove border
.style("border-radius", "5px") // Rounded corners
.style("cursor", "pointer") // Pointer cursor on hover
.style("transition", "background-color 0.3s") // Smooth background color transition
.on("click", () => createMultiBarChart(genderData, "Biological Sex Distribution"))
.on("mouseover", function () {
d3.select(this).style("background-color", "#B8A8D4");
})
.on("mouseout", function () {
d3.select(this).style("background-color", "#CBC3E3"); // Revert back on mouseout
});
// buttonContainer
// .append("button")
// .text("Close")
// .style("padding", "10px 20px")
// .style("margin", "5px 10px")
// .style("font-size", "12px")
// .style("background-color", "#CBC3E3")
// .style("color", "#342047")
// .style("border", "none") // Remove border
// .style("border-radius", "5px") // Rounded corners
// .style("cursor", "pointer") // Pointer cursor on hover
// .style("transition", "background-color 0.3s") // Smooth background color transition
// .on("click", () => {
// barchartcontainer.style("display", "none");
// buttonContainer.style("display", "none");
// })
// .on("mouseover", function () {
// d3.select(this).style("background-color", "#B8A8D4");
// })
// .on("mouseout", function () {
// d3.select(this).style("background-color", "#CBC3E3"); // Revert back on mouseout
// });
// Initial view (Age Distribution)
createMultiBarChart(ageData, "Age Distribution");
// Make the container visible
barchartcontainer.style("display", "flex");
// buttonContainer.style("display", "block");
}