chart = {
const width = 928;
const height = 600;
const marginTop = 10;
const marginRight = 10;
const marginBottom = 20;
const marginLeft = 40;
const fx = d3.scaleBand()
.domain(new Set(data.map(d => d.state)))
.rangeRound([marginLeft, width - marginRight])
.paddingInner(0.1);
const ages = new Set(data.map(d => d.age));
const x = d3.scaleBand()
.domain(ages)
.rangeRound([0, fx.bandwidth()])
.padding(0.05);
const color = d3.scaleOrdinal()
.domain(ages)
.range(d3.schemeSpectral[ages.size])
.unknown("#ccc");
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.population)]).nice()
.rangeRound([height - marginBottom, marginTop]);
const formatValue = x => isNaN(x) ? "N/A" : x.toLocaleString("en")
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;");
svg.append("g")
.selectAll()
.data(d3.group(data, d => d.state))
.join("g")
.attr("transform", ([state]) => `translate(${fx(state)},0)`)
.selectAll()
.data(([, d]) => d)
.join("rect")
.attr("x", d => x(d.age))
.attr("y", d => y(d.population))
.attr("width", x.bandwidth())
.attr("height", d => y(0) - y(d.population))
.attr("fill", d => color(d.age));
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(fx).tickSizeOuter(0))
.call(g => g.selectAll(".domain").remove());
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y).ticks(null, "s"))
.call(g => g.selectAll(".domain").remove());
return Object.assign(svg.node(), {scales: {color}});
}