Skip to content

Commit

Permalink
✨ (map) left-align multi-line categorical legends (#4324)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann authored Dec 17, 2024
1 parent dfd5177 commit f4a8e4c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
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

0 comments on commit f4a8e4c

Please sign in to comment.