diff --git a/packages/@ourworldindata/grapher/src/verticalColorLegend/VerticalColorLegend.ts b/packages/@ourworldindata/grapher/src/verticalColorLegend/VerticalColorLegend.ts index 15c634a3f3..cc0aa12e1c 100644 --- a/packages/@ourworldindata/grapher/src/verticalColorLegend/VerticalColorLegend.ts +++ b/packages/@ourworldindata/grapher/src/verticalColorLegend/VerticalColorLegend.ts @@ -26,7 +26,7 @@ export type VerticalColorLegendBin = | VerticalColorLegendCategoricalBin | VerticalColorLegendNumericBin -interface PlacedBin extends Bin { +export interface PlacedBin extends Bin { textWrap: TextWrap width: number height: number diff --git a/packages/@ourworldindata/grapher/src/verticalColorLegend/VerticalColorLegendComponent.tsx b/packages/@ourworldindata/grapher/src/verticalColorLegend/VerticalColorLegendComponent.tsx index 46207409c3..47db061305 100644 --- a/packages/@ourworldindata/grapher/src/verticalColorLegend/VerticalColorLegendComponent.tsx +++ b/packages/@ourworldindata/grapher/src/verticalColorLegend/VerticalColorLegendComponent.tsx @@ -1,7 +1,7 @@ import React from "react" import { Color, makeIdForHumanConsumption } from "@ourworldindata/utils" -import { VerticalColorLegend } from "./VerticalColorLegend" +import { PlacedBin, VerticalColorLegend } from "./VerticalColorLegend" interface VerticalColorLegendComponentProps { legend: VerticalColorLegend @@ -38,156 +38,167 @@ export function VerticalColorLegendComponent({ className="ScatterColorLegend clickable" > {legend.title && - legend.title.render(x, y, { - textProps: { - fontWeight: 700, - }, - })} - - + legend.title.render(x, y, { textProps: { fontWeight: 700 } })} + + + {legend.placedBins.map((bin) => ( + + + + {legend.placedBins.map((bin) => ( + + ))} + + {isInteractive && ( - + + {legend.placedBins.map((bin) => ( + + ))} + )} ) } -function Labels({ - legend, +function Label({ + bin, x, y, focusColors, + swatchSize, + swatchMarginRight, }: { - legend: VerticalColorLegend + bin: PlacedBin x: number y: number + swatchSize: number + swatchMarginRight: number focusColors?: Color[] }): React.ReactElement { - return ( - - {legend.placedBins.map((series) => { - const isFocus = focusColors?.includes(series.color) ?? false - - const textX = x + legend.swatchSize + legend.swatchMarginRight - const textY = y + series.yOffset - - return ( - - {series.textWrap.render( - textX, - textY, - isFocus - ? { - textProps: { - style: { fontWeight: "bold" }, - }, - } - : undefined - )} - - ) - })} - + const isFocus = focusColors?.includes(bin.color) ?? false + + const textX = x + swatchSize + swatchMarginRight + const textY = y + bin.yOffset + + return bin.textWrap.render( + textX, + textY, + isFocus + ? { + textProps: { + style: { fontWeight: "bold" }, + }, + } + : undefined ) } -function Swatches({ - legend, +function Swatch({ + bin, x, y, + swatchSize, + swatchMarginRight, activeColors, }: { - legend: VerticalColorLegend + bin: PlacedBin x: number y: number + swatchSize: number + swatchMarginRight: number activeColors?: Color[] }): React.ReactElement { - return ( - - {legend.placedBins.map((series) => { - const isActive = activeColors?.includes(series.color) + const isActive = activeColors?.includes(bin.color) - const textX = x + legend.swatchSize + legend.swatchMarginRight - const textY = y + series.yOffset + const textX = x + swatchSize + swatchMarginRight + const textY = y + bin.yOffset - const renderedTextPosition = - series.textWrap.getPositionForSvgRendering(textX, textY) + const renderedTextPosition = bin.textWrap.getPositionForSvgRendering( + textX, + textY + ) - return ( - - ) - })} - + return ( + ) } function InteractiveElement({ + bin, x, y, - legend, + verticalBinMargin, onClick, onMouseOver, onMouseLeave, }: { + bin: PlacedBin x: number y: number - legend: VerticalColorLegend + verticalBinMargin: number onClick?: (color: string) => void onMouseOver?: (color: string) => void onMouseLeave?: () => void }): React.ReactElement { + const mouseOver = onMouseOver + ? (): void => onMouseOver(bin.color) + : undefined + const mouseLeave = onMouseLeave + const click = onClick ? (): void => onClick(bin.color) : undefined + + const cursor = click ? "pointer" : "default" + return ( - - {legend.placedBins.map((series) => { - const mouseOver = onMouseOver - ? (): void => onMouseOver(series.color) - : undefined - const mouseLeave = onMouseLeave - const click = onClick - ? (): void => onClick(series.color) - : undefined - - const cursor = click ? "pointer" : "default" - - return ( - - - - ) - })} + + ) }