Skip to content

Commit

Permalink
✨ use 'nice' axis ticks for linear scales
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Jan 8, 2025
1 parent c8cf78e commit 0a553e6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
7 changes: 4 additions & 3 deletions packages/@ourworldindata/grapher/src/axis/Axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0a553e6

Please sign in to comment.