function updateBetter(sex, step) {
state.sex = sex;
state.year += step;
const newData = census.filter(row => isYearAndSex(row, state.year, state.sex));
const bars = __update_better_n__.chart.selectAll('.bar')
.data(newData, (d) => {
if (d.year === state.year) {
return d.age_group - step;
} else {
return d.age_group;
}
});
bars.enter().append('rect')
.attr('class', 'bar')
.attr('x', d => x(d.age_group))
.attr('y', d => y(0))
.attr('width', x.bandwidth())
.attr('height', 0)
.attr('fill', d => color(d.sex))
.transition('enter-transition')
.duration(500)
.attr('y', d => y(d.people))
.attr('height', d => height - y(d.people))
bars
.transition('update-transition')
.duration(500)
.attr('x', d => x(d.age_group))
.attr('y', d => y(d.people))
.attr('height', d => height - y(d.people))
.attr('fill', d => color(d.sex));
bars.exit()
.transition('exit-transition')
.duration(500)
.attr('height', 0)
.attr('y', y(0))
.remove();
document.getElementById('curr-year-better').textContent = state.year;
}