graphics = {
let container = d3
.create('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom);
let chart = container
.append('g')
.attr('id', 'chart')
.attr('transform', `translate(${margin.left}, ${margin.top})`);
container
.insert('rect', ':first-child')
.attr('width', '100%')
.attr('height', '100%')
.attr('fill', '#ffffff');
chart
.append('rect')
.attr('width', width)
.attr('height', height)
.attr('fill', '#ffffff');
const unique = i => Array.from(new Set(i));
const ageDomain = unique(census.map(r => r.age_group));
const peopleDomain = [0, d3.max(census, r => r.people)];
const sexDomain = [1, 2];
const x = d3
.scaleBand()
.rangeRound([0, width])
.padding(0.1)
.domain(ageDomain);
const y = d3
.scaleLinear()
.range([height, 0])
.domain(peopleDomain)
.nice();
const color = d3
.scaleQuantize()
.range(['#42adf4', '#ff96ca'])
.domain(sexDomain);
const xAxis = chart
.append('g')
.attr('class', 'axis axis--x')
.attr('transform', `translate(0, ${height})`)
.call(d3.axisBottom(x));
const yAxis = chart
.append('g')
.attr('class', 'axis axis--y')
.call(d3.axisLeft(y));
const title = container
.append('text')
.attr(
'transform',
`translate(${(width + margin.left + margin.right) / 2}, ${margin.top /
2})`
)
.style('text-anchor', 'middle')
.style('font-weight', 700)
.text('Census Age Group and Population by Sex');
const xTitle = chart
.append('text')
.attr(
'transform',
`translate(${width / 2}, ${height + margin.bottom * 0.8})`
)
.style('text-anchor', 'middle')
.text('Age Group');
const yTitle = chart
.append('text')
.attr('transform', 'rotate(-90)')
.attr('y', 0 - margin.left)
.attr('x', 0 - height / 2)
.attr('dy', '1em')
.style('text-anchor', 'middle')
.text('Population');
const legend = chart
.append('g')
.attr('class', 'legend')
.attr('transform', `translate(${width - 60}, 28)`);
const legendLine = legend
.selectAll(".legend-line")
.data(color.domain())
.enter()
.append('g')
.attr('class', 'legend-line')
.attr('transform', (d, i) => `translate(0, ${i * 20})`);
legendLine
.append('rect')
.attr('class', 'legend-line-rect')
.attr('width', 12)
.attr('height', 12)
.style('fill', color);
legendLine
.append('text')
.attr('class', 'legend-line-text')
.attr('x', 18)
.attr('dy', '0.9em')
.style('font-size', '12px')
.text(d => (d == 1 ? 'Male' : 'Female'));
const updateChart = function(sex, step) {
state.sex = sex;
state.year += step;
const data = census.filter(r => isYearAndSex(r, state.year, state.sex));
const bars = container.selectAll('.bar').data(data);
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));
document.getElementById('curr-year-naive').textContent = state.year;
};
const bars = chart.selectAll('.bar').data(filteredData);
bars
.join('rect')
.attr('class', 'bar')
.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));
container.selectAll('text').style('font-family', 'sans-serif');
return { container: container, updateChart: updateChart };
}