chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
const group = svg.append("g");
let rect = group.selectAll("rect");
return Object.assign(svg.node(), {
update(year) {
const dx = x.step() * (year - yearMin) / yearStep;
const t = svg.transition()
.ease(d3.easeLinear)
.duration(delay);
rect = rect
.data(data.filter(d => d.year === year), d => `${d.sex}:${d.year - d.age}`)
.join(
enter => enter.append("rect")
.style("mix-blend-mode", "darken")
.attr("fill", d => color(d.sex))
.attr("x", d => x(d.age) + dx)
.attr("y", d => y(0))
.attr("width", x.bandwidth() + 1)
.attr("height", 0),
update => update,
exit => exit.call(rect => rect.transition(t).remove()
.attr("y", y(0))
.attr("height", 0))
);
rect.transition(t)
.attr("y", d => y(d.value))
.attr("height", d => y(0) - y(d.value));
group.transition(t)
.attr("transform", `translate(${-dx},0)`);
}
});
}