Selecting a Year on a Line Chart
A line chart displays population over time for multiple regions, with a vertical line marking a selected year. Users can hover anywhere on the chart to update the selection, which triggers a re-render of the visualization. The code uses D3 v5’s `scaleTime`, `scaleLinear`, `axisBottom`, `axisLeft`, and `line` with `curveBasis` to draw the chart, while a `mouse` event on a transparent `rect` calls `setSelectedYear` to change the year. The data comes from UN population estimates and medium-variant projections, loaded via `csv` and processed with `nest`, `descending`, and a custom `melt` function. The rendering is SVG-based, with a color legend created by `colorLegend` and axes styled via CSS.