From 0a553e69917bf6571a4ff4f08028a4134fc98236 Mon Sep 17 00:00:00 2001 From: Sophia Mersmann Date: Wed, 8 Jan 2025 13:15:19 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20use=20'nice'=20axis=20ticks=20for?= =?UTF-8?q?=20linear=20scales?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/@ourworldindata/grapher/src/axis/Axis.ts | 7 ++++--- .../@ourworldindata/grapher/src/lineCharts/LineChart.tsx | 2 +- .../grapher/src/stackedCharts/AbstractStackedChart.tsx | 9 +++++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/@ourworldindata/grapher/src/axis/Axis.ts b/packages/@ourworldindata/grapher/src/axis/Axis.ts index eb9f2e9159f..604284d9d32 100644 --- a/packages/@ourworldindata/grapher/src/axis/Axis.ts +++ b/packages/@ourworldindata/grapher/src/axis/Axis.ts @@ -210,10 +210,11 @@ abstract class AbstractAxis { } @computed private get d3_scale(): Scale { - const d3Scale = - this.scaleType === ScaleType.log ? scaleLog : scaleLinear + const isLogScale = this.scaleType === ScaleType.log + const d3Scale = isLogScale ? scaleLog : scaleLinear let scale = d3Scale().domain(this.domain).range(this.range) - scale = this.nice ? scale.nice(this.totalTicksTarget) : scale + scale = + this.nice && !isLogScale ? scale.nice(this.totalTicksTarget) : scale if (this.config.domainValues) { // compute bandwidth and adjust the scale diff --git a/packages/@ourworldindata/grapher/src/lineCharts/LineChart.tsx b/packages/@ourworldindata/grapher/src/lineCharts/LineChart.tsx index dd54175a663..80c0fc18968 100644 --- a/packages/@ourworldindata/grapher/src/lineCharts/LineChart.tsx +++ b/packages/@ourworldindata/grapher/src/lineCharts/LineChart.tsx @@ -1410,9 +1410,9 @@ export class LineChart } @computed private get yAxisConfig(): AxisConfig { - // TODO: enable nice axis ticks for linear scales return new AxisConfig( { + nice: true, // if we only have a single y value (probably 0), we want the // horizontal axis to be at the bottom of the chart. // see https://github.com/owid/owid-grapher/pull/975#issuecomment-890798547 diff --git a/packages/@ourworldindata/grapher/src/stackedCharts/AbstractStackedChart.tsx b/packages/@ourworldindata/grapher/src/stackedCharts/AbstractStackedChart.tsx index 5b40a3086bb..812da2c3814 100644 --- a/packages/@ourworldindata/grapher/src/stackedCharts/AbstractStackedChart.tsx +++ b/packages/@ourworldindata/grapher/src/stackedCharts/AbstractStackedChart.tsx @@ -253,8 +253,13 @@ export class AbstractStackedChart } @computed private get yAxisConfig(): AxisConfig { - // TODO: enable nice axis ticks for linear scales - return new AxisConfig(this.manager.yAxisConfig, this) + return new AxisConfig( + { + nice: true, + ...this.manager.yAxisConfig, + }, + this + ) } // implemented in subclasses