Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (line) deduplicate line labels #4420

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/@ourworldindata/grapher/src/lineCharts/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1363,13 +1363,21 @@ export class LineChart
// Order of the legend items on a line chart should visually correspond
// to the order of the lines as the approach the legend
@computed get lineLegendSeries(): LineLabelSeries[] {
// If there are any projections, ignore non-projection legends
// Bit of a hack
let seriesToShow = this.series
if (seriesToShow.some((series) => !!series.isProjection))
seriesToShow = seriesToShow.filter((series) => series.isProjection)
// If there are any projections, ignore non-projection legends (bit of a hack)
let series = this.series
if (series.some((series) => !!series.isProjection))
series = series.filter((series) => series.isProjection)

// Deduplicate series by seriesName to avoid showing the same label multiple times
const deduplicatedSeries: LineChartSeries[] = []
const seriesGroupedByName = groupBy(series, "seriesName")
for (const duplicates of Object.values(seriesGroupedByName)) {
// keep only the label for the series with the most recent data
// (series are sorted by time, so we can just take the last one)
deduplicatedSeries.push(last(duplicates)!)
}

return seriesToShow.map((series) => {
return deduplicatedSeries.map((series) => {
const { seriesName, color } = series
const lastValue = last(series.points)!.y
return {
Expand Down
Loading