diff --git a/packages/@ourworldindata/grapher/src/scatterCharts/ScatterPlotChart.tsx b/packages/@ourworldindata/grapher/src/scatterCharts/ScatterPlotChart.tsx index 5906adeb482..108272297f4 100644 --- a/packages/@ourworldindata/grapher/src/scatterCharts/ScatterPlotChart.tsx +++ b/packages/@ourworldindata/grapher/src/scatterCharts/ScatterPlotChart.tsx @@ -1144,10 +1144,16 @@ export class ScatterPlotChart // domains across the entire timeline private domainDefault(property: "x" | "y"): [number, number] { const scaleType = property === "x" ? this.xScaleType : this.yScaleType - return domainExtent( - this.pointsForAxisDomains.map((point) => point[property]), - scaleType, - this.manager.zoomToSelection && this.selectedPoints.length ? 1.1 : 1 + const defaultDomain: [number, number] = + scaleType === ScaleType.log ? [1, 100] : [-1, 1] + return ( + domainExtent( + this.pointsForAxisDomains.map((point) => point[property]), + scaleType, + this.manager.zoomToSelection && this.selectedPoints.length + ? 1.1 + : 1 + ) ?? defaultDomain ) } diff --git a/packages/@ourworldindata/grapher/src/slopeCharts/SlopeChart.tsx b/packages/@ourworldindata/grapher/src/slopeCharts/SlopeChart.tsx index 4a10f1424a8..0044ce18374 100644 --- a/packages/@ourworldindata/grapher/src/slopeCharts/SlopeChart.tsx +++ b/packages/@ourworldindata/grapher/src/slopeCharts/SlopeChart.tsx @@ -484,7 +484,8 @@ export class SlopeChart } @computed private get yDomainDefault(): [number, number] { - return domainExtent(this.allYValues, this.yScaleType) + const defaultDomain: [number, number] = [Infinity, -Infinity] + return domainExtent(this.allYValues, this.yScaleType) ?? defaultDomain } @computed private get yDomain(): [number, number] { diff --git a/packages/@ourworldindata/utils/src/Util.ts b/packages/@ourworldindata/utils/src/Util.ts index d5476d8c9dc..1811792dbb7 100644 --- a/packages/@ourworldindata/utils/src/Util.ts +++ b/packages/@ourworldindata/utils/src/Util.ts @@ -420,7 +420,7 @@ export const domainExtent = ( numValues: number[], scaleType: ScaleType, maxValueMultiplierForPadding = 1 -): [number, number] => { +): [number, number] | undefined => { const filterValues = scaleType === ScaleType.log ? numValues.filter((v) => v > 0) : numValues const [minValue, maxValue] = extent(filterValues) @@ -439,9 +439,9 @@ export const domainExtent = ( ? [minValue / 10, minValue * 10] : [minValue - 1, maxValue + 1] } - } else { - return scaleType === ScaleType.log ? [1, 100] : [-1, 1] } + + return undefined } // Compound annual growth rate