From 464a6406f3c54ef5d25d229a73c84a3c628f39f9 Mon Sep 17 00:00:00 2001 From: Sophia Mersmann Date: Tue, 10 Dec 2024 16:30:21 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20(slope)=20use=20correct=20domain?= =?UTF-8?q?s=20when=20facetted=20and=20axis=20is=20shared?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../grapher/src/scatterCharts/ScatterPlotChart.tsx | 14 ++++++++++---- .../grapher/src/slopeCharts/SlopeChart.tsx | 3 ++- packages/@ourworldindata/utils/src/Util.ts | 6 +++--- 3 files changed, 15 insertions(+), 8 deletions(-) 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 758e202d498..b1c9638dcf0 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