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

✨ (map) left-align multi-line categorical legends #4324

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,10 @@ export class HorizontalCategoricalColorLegend extends HorizontalColorLegend {
return max(this.marks.map((mark) => mark.y + mark.rectSize)) ?? 0
}

@computed get numLines(): number {
return this.markLines.length
}

renderLabels(): React.ReactElement {
const { manager, marks } = this
const { focusColors, hoverColors = [] } = manager
Expand Down
10 changes: 5 additions & 5 deletions packages/@ourworldindata/grapher/src/mapCharts/MapChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,11 @@ export class MapChart
}

@computed get legendMaxWidth(): number {
// it seems nice to have just a little bit of
// extra padding left and right
return this.bounds.width * 0.95
return this.bounds.width
}

@computed get legendX(): number {
return this.bounds.x + (this.bounds.width - this.legendMaxWidth) / 2
return this.bounds.x
}

@computed get legendHeight(): number {
Expand Down Expand Up @@ -557,7 +555,9 @@ export class MapChart
}

@computed get legendAlign(): HorizontalAlign {
return HorizontalAlign.center
if (this.numericLegend) return HorizontalAlign.center
const { numLines = 0 } = this.categoryLegend ?? {}
return numLines > 1 ? HorizontalAlign.left : HorizontalAlign.center
}

@computed get numericLegendY(): number {
Expand Down
Loading