bar_chart = {
const dataset = d3.shuffle(d3.range(40, 200, 10));
const width = 600;
const height = 300;
const svg = d3
.create('svg')
.attr('width', width)
.attr('height', height)
.attr('fill', '#064');
const barPadding = 1;
svg
.append('rect')
.attr('width', width)
.attr('height', height)
.attr('fill', 'yellow');
const rects = svg
.selectAll('rect.bar')
.data(dataset)
.join('rect')
.attr('class', 'bar');
rects
.attr('x', 0)
.attr('y', (d, i) => i * (height / dataset.length))
.attr('width', d => (d / d3.max(dataset)) * width)
.attr('height', height / dataset.length - barPadding)
.attr('fill', d => `hsl(220, 50%, ${60 - (d3 / d3.max(dataset)) * 40}%)`);
svg
.selectAll('text')
.data(dataset)
.join('text')
.attr('text-anchor', 'middle')
.attr('x', d => (d / d3.max(dataset)) * width - 20)
.attr(
'y',
(d, i) =>
i * (height / dataset.length) +
(height / dataset.length - barPadding) / 1.5
)
.attr('font-family', 'sans-serif')
.attr('font-weight', 'bold')
.attr('font-size', 11)
.attr('fill', 'white')
.text(d => d);
return svg.node();
}