chart = ({
data,
name,
height,
showLegend = true,
titlePrefix = "Age Distrubution of"
}) => {
height = height || 100;
const container = html`<div class="chart-container">
<style>
.chart-container { padding: 12px; }
.chart-container p.title { margin: 0; font-family: sans-serif; }
.chart-container svg { display: block; margin: 0; }
.chart-container svg:last-of-type { margin-left: 8px; }
</style>
<p class="title"><strong>${titlePrefix} ${name}</strong></p>
<svg viewBox="0, 0, ${width}, ${height}"></svg>
${showLegend ? legend({ color }) : ""}
</div>`;
const svg = d3.select(container).select("svg");
const g = svg.append("g");
const stacks = g
.selectAll("g")
.data(data, (d) => d.key)
.join("g")
.attr("fill", (d) => color(d.key))
.attr("transform", `translate(0 ,${margin.top})`);
stacks
.selectAll("rect")
.data((d) => d)
.join("rect")
.attr("width", (d) => x(d[1]) - x(d[0]))
.attr("height", height - margin.top - margin.bottom)
.attr("x", (d) => x(d[0]))
.attr("y", 0)
.append("title")
.text((d) => d.data[d.key].toLocaleString());
svg.append("g").call(xAxis, data);
return container;
}