diff --git a/packages/@ourworldindata/grapher/src/horizontalColorLegend/HorizontalColorLegends.tsx b/packages/@ourworldindata/grapher/src/horizontalColorLegend/HorizontalColorLegends.tsx index 2816a157a24..9c73efaec5e 100644 --- a/packages/@ourworldindata/grapher/src/horizontalColorLegend/HorizontalColorLegends.tsx +++ b/packages/@ourworldindata/grapher/src/horizontalColorLegend/HorizontalColorLegends.tsx @@ -842,6 +842,7 @@ export class HorizontalCategoricalColorLegend extends HorizontalColorLegend { return ( ) - const { manager, bounds, yAxis, innerBounds } = this + return this.manager.isStatic + ? this.renderStatic() + : this.renderInteractive() + } - const chartContext: StackedBarChartContext = { - yAxis, + @computed get chartContext(): StackedBarChartContext { + return { + yAxis: this.yAxis, targetTime: this.manager.endTime, timeColumn: this.inputTable.timeColumn, formatColumn: this.formatColumn, @@ -621,98 +624,76 @@ export class StackedDiscreteBarChart x0: this.x0, baseFontSize: this.baseFontSize, } + } - const handlePositionUpdate = (d: PlacedItem): HashMap => ({ - translateY: [d.yPosition], - timing: { duration: 350, ease: easeQuadOut }, - }) - - const renderRow = ({ - data, - state, - }: { - data: PlacedItem - state: { translateY: number } - }): React.ReactElement => { - const { entityName, label, bars, totalValue } = data + renderRow({ + data, + state, + }: { + data: PlacedItem + state: { translateY: number } + }): React.ReactElement { + const { yAxis } = this + const { entityName, label, bars, totalValue } = data - const totalLabel = this.formatValueForLabel(totalValue) - const showLabelInsideBar = bars.length > 1 + const totalLabel = this.formatValueForLabel(totalValue) + const showLabelInsideBar = bars.length > 1 - return ( - - {label.render( - yAxis.place(this.x0) - labelToBarPadding, - -label.height / 2, - { - textProps: { - textAnchor: "end", - fill: "#555", - onMouseEnter: (): void => - this.onEntityMouseEnter(label.text), - onMouseLeave: this.onEntityMouseLeave, - }, - } - )} + return ( + + {bars.map((bar) => ( + + ))} + {label.render( + yAxis.place(this.x0) - labelToBarPadding, + -label.height / 2, + { + textProps: { + textAnchor: "end", + fill: "#555", + onMouseEnter: (): void => + this.onEntityMouseEnter(label.text), + onMouseLeave: this.onEntityMouseLeave, + }, + } + )} + {this.showTotalValueLabel && ( + + {totalLabel} + + )} + + ) + } - {bars.map((bar) => ( - - ))} - {this.showTotalValueLabel && ( - - - {totalLabel} - - - )} - - ) - } + renderAxis(): React.ReactElement { + const { manager, bounds, yAxis, innerBounds } = this const axisLineWidth = manager.isStaticAndSmall ? GRAPHER_AXIS_LINE_WIDTH_THICK : GRAPHER_AXIS_LINE_WIDTH_DEFAULT return ( - - + <> {this.showHorizontalAxis && ( <> - {this.showLegend && ( - - )} + + ) + } + + renderLegend(): React.ReactElement | void { + if (!this.showLegend) return + return + } + + renderStatic(): React.ReactElement { + return ( + <> + {this.renderAxis()} + {this.renderLegend()} + + {this.placedItems.map((item) => + this.renderRow({ + data: item, + state: { translateY: item.yPosition }, + }) + )} + + + ) + } + + renderInteractive(): React.ReactElement { + const { bounds } = this + + const handlePositionUpdate = (d: PlacedItem): HashMap => ({ + translateY: [d.yPosition], + timing: { duration: 350, ease: easeQuadOut }, + }) + + // needs to be referenced here, otherwise it's not updated in the renderRow function + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + this.focusSeriesName + + return ( + + + {this.renderAxis()} + {this.renderLegend()} d.entityName} @@ -747,23 +779,14 @@ export class StackedDiscreteBarChart update={handlePositionUpdate} > {(nodes): React.ReactElement => ( - {nodes.map((node) => renderRow(node))} + {nodes.map((node) => this.renderRow(node))} )} - {this.Tooltip} + {this.tooltip} ) } - @computed get activeColors(): string[] | undefined { - const { focusSeriesName } = this - if (!focusSeriesName) return undefined - const activeColors = this.series - .filter((series) => series.seriesName === focusSeriesName) - .map((series) => series.color) - return activeColors.length === 0 ? undefined : activeColors - } - private static Bar(props: { bar: Bar entity: string @@ -799,13 +822,14 @@ export class StackedDiscreteBarChart return ( props?.onMouseEnter(entity, bar.seriesName) } onMouseLeave={props?.onMouseLeave} >