Skip to content

Commit

Permalink
🔨 simplify legend in map chart component
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Jan 3, 2025
1 parent c4b9e29 commit 1e443c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ export class HorizontalCategoricalColorLegend {
this.props = props
}

static height(props: HorizontalCategoricalColorLegendProps): number {
const legend = new HorizontalCategoricalColorLegend(props)
return legend.height
}

static numLines(props: HorizontalCategoricalColorLegendProps): number {
const legend = new HorizontalCategoricalColorLegend(props)
return legend.numLines
Expand Down
43 changes: 19 additions & 24 deletions packages/@ourworldindata/grapher/src/mapCharts/MapChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,24 @@ export class MapChart
return this.numericLegendData.length > 1
}

@computed get numericLegendHeight(): number {
// can't use numericLegend due to a circular dependency
return this.hasNumericLegend
? HorizontalNumericColorLegend.height({
fontSize: this.fontSize,
x: this.legendX,
align: this.legendAlign,
maxWidth: this.legendMaxWidth,
numericBins: this.numericLegendData,
equalSizeBins: this.equalSizeBins,
})
: 0
}

@computed get categoryLegendHeight(): number {
return this.categoryLegend ? this.categoryLegend.height + 5 : 0
}

@computed private get categoryLegend():
| HorizontalCategoricalColorLegend
| undefined {
Expand Down Expand Up @@ -561,18 +579,8 @@ export class MapChart
: undefined
}

@computed get categoryLegendHeight(): number {
return this.hasCategoryLegend
? HorizontalCategoricalColorLegend.height({
fontSize: this.fontSize,
align: this.legendAlign,
maxWidth: this.legendMaxWidth,
categoricalBins: this.categoricalLegendData,
}) + 5
: 0
}

@computed get categoryLegendNumLines(): number {
// can't use categoryLegend due to a circular dependency
return this.hasCategoryLegend
? HorizontalCategoricalColorLegend.numLines({
fontSize: this.fontSize,
Expand All @@ -582,19 +590,6 @@ export class MapChart
: 0
}

@computed get numericLegendHeight(): number {
return this.hasNumericLegend
? HorizontalNumericColorLegend.height({
fontSize: this.fontSize,
x: this.legendX,
align: this.legendAlign,
maxWidth: this.legendMaxWidth,
numericBins: this.numericLegendData,
equalSizeBins: this.equalSizeBins,
})
: 0
}

@computed get categoryLegendY(): number {
const { hasCategoryLegend, bounds, categoryLegendHeight } = this

Expand Down

0 comments on commit 1e443c5

Please sign in to comment.