fix(linear-scale): respect stepSize for non-divisible min/max ranges by syawqy · Pull Request #12225 · chartjs/Chart.js
Fixes #12165
Summary
When min, max, and stepSize are set, linear tick generation used a spacing-based tolerance (spacing / 1000) to detect whole step counts.
With larger step sizes, that tolerance can be too loose and misclassify non-divisible ranges as divisible, producing evenly distributed ticks instead of stepSize increments.
This change switches the whole-step check to a machine-precision-based epsilon derived from stepCount.
Behavior after fix
For non-divisible ranges, ticks follow stepSize multiples and keep max as the final shorter interval:
min: 0,max: 3333,stepSize: 500- ticks:
[0, 500, 1000, 1500, 2000, 2500, 3000, 3333]
Floating-point divisible ranges still behave correctly:
min: 0,max: 0.3,stepSize: 0.1
Tests
Added regression tests in test/specs/scale.linear.tests.js for:
- Large non-divisible range (
0..3333,stepSize: 500) - Floating-point divisible range (
0..0.3,stepSize: 0.1)
Validated with:
pnpm run lint-jspnpm run test-ci-karma scale.linear.tests
References
- Issue repro: Inconsistent step size behaviour when step size is not a divisor of (max - min) #12165
- CodePen (reported case): https://codepen.io/Pia-Gerhofer/pen/LENMKOO