function updateChart(sex, step) {
state.sex = sex;
state.year = state.year + step;
const newYear = state.year;
const newData = census.filter(row => isYearAndSex(row, state.year, state.sex));
console.log(newData);
const bars = chart.selectAll('.bar').data(newData, (d) => {
if (d.year === state.year) {
if (step < 0)
return d.age_group + 5
else return d.age_group - 5
} 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').duration(500).attr('x', d => x(d.age_group))
.attr('y', d => y(d.people))
.attr('width', x.bandwidth())
.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-naive').textContent = state.year;
}