function createChart() {
const width = 800;
const height = 500;
const margin = { top: 20, right: 20, bottom: 50, left: 60 };
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.style("background", "transparent");
const years = [...new Set(maryland_incarceration_data.map(d => d.year))];
const races = ["Black", "White"];
const xScale = d3.scaleBand()
.domain(years)
.range([margin.left, width - margin.right])
.padding(0.1);
const xSubgroupScale = d3.scaleBand()
.domain(races)
.range([0, xScale.bandwidth()])
.padding(0.05);
const yScale = d3.scaleLinear()
.domain([0, d3.max(maryland_incarceration_data, d => d.count)])
.range([height - margin.bottom, margin.top]);
const tooltip = d3.select("body")
.append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.style("padding", "10px")
.style("background", "rgba(0,0,0,0.6)")
.style("border-radius", "4px")
.style("color", "#fff");
svg.append("g")
.selectAll("g")
.data(years)
.enter().append("g")
.attr("transform", d => `translate(${xScale(d)}, 0)`)
.selectAll("rect")
.data(year => maryland_incarceration_data.filter(data => data.year === year))
.enter().append("rect")
.attr("x", d => xSubgroupScale(d.race))
.attr("y", d => yScale(d.count))
.attr("width", xSubgroupScale.bandwidth())
.attr("height", d => height - margin.bottom - yScale(d.count))
.attr("fill", d => {
if (d.race === "Black") return "darkred";
if (d.race === "White") return "#FFDAB9";
return "gray";
})
.on("mouseover", function(event, d) {
tooltip
.html(`<div>${d.year}: ${d.count}</div>`)
.style("visibility", "visible");
d3.select(this).attr("fill", "orange");
})
.on("mousemove", function(event) {
tooltip
.style("top", (event.pageY + 10) + "px")
.style("left", (event.pageX + 10) + "px");
})
.on("mouseout", function() {
tooltip.html(``).style("visibility", "hidden");
d3.select(this).transition().attr("fill", d => {
if (d.race === "Black") return "darkred";
if (d.race === "White") return "#edad6c";
return "gray";
});
});
const xAxis = d3.axisBottom(xScale);
const yAxis = d3.axisLeft(yScale).ticks(5);
svg.append("g")
.attr("transform", `translate(0, ${height - margin.bottom})`)
.call(xAxis);
svg.append("g")
.attr("transform", `translate(${margin.left}, 0)`)
.call(yAxis);
return svg.node();
}