Public
Edited
Feb 6, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
graphics = {
// enclosing container, an svg image
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));

// domains of variables
const ageDomain = unique(census.map(r => r.age_group));
const peopleDomain = [0, d3.max(census, r => r.people)];
const sexDomain = [1, 2];

// define scales
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);

// define axis
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));

// axis titles
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');

// legend
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;
};

// bars
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 };
}
Insert cell
graphics.container.node()
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more