function* drawChart() {
const svg = d3.select(
DOM.svg(
chartWidth +
padding.left +
padding.right +
legendMargin.left +
legendMargin.right,
chartHeight +
padding.top +
padding.bottom +
legendMargin.top +
legendMargin.bottom
)
);
const g = svg.append('g');
const stateBarsG = g
.append('g')
.attr('transform', `translate(${legendMargin.left},${legendMargin.top})`);
stateBarsG
.selectAll('.state-bar')
.data(data.sort((a, b) => d3.ascending(a.value, b.value)))
.join('rect')
.attr('class', 'state-bar')
.attr('transform', (d, i) => `translate(${x(i)},${y(d.value)})`)
.attr('width', Math.ceil(chartWidth / data.length))
.attr('height', d => y.range()[0] - y(d.value))
.attr('fill', d =>
d.state === selectedState ? selectedBarColor : barColor
);
stateBarsG
.append('rect')
.attr('width', chartWidth)
.attr('height', 2)
.attr('x', 0)
.attr('y', y(trendValue))
.attr('fill', trendLineColor);
if (showLegend) {
const legendG = g.append('g').attr('fill', legendTextColor);
legendG
.append('text')
.attr('transform', `translate(${legendMargin.left},0)`)
.attr('fill', labelTextColor)
.attr('alignment-baseline', 'hanging')
.attr('font-size', '.75rem')
.attr('font-weight', 700)
.attr('font-family', 'Arial')
.text(label);
legendG
.append('text')
.attr(
'transform',
`translate(${legendMargin.left},${padding.top +
legendMargin.top +
chartHeight})`
)
.attr('font-size', '12px')
.attr('alignment-baseline', 'hanging')
.text('State Ranking →');
legendG
.append('text')
.attr('transform', `translate(0,${legendMargin.top + y(trendValue)})`)
.attr('font-size', '12px')
.attr('alignment-baseline', 'middle')
.text('U.S.');
}
yield svg.node();
}