Skip to content

fix(scale): detect even major-tick spacing when first major is at index 0 - #12287

Open
contactjawad wants to merge 1 commit into
chartjs:masterfrom
contactjawad:fix-autoskip-even-spacing
Open

fix(scale): detect even major-tick spacing when first major is at index 0#12287
contactjawad wants to merge 1 commit into
chartjs:masterfrom
contactjawad:fix-autoskip-even-spacing

Conversation

@contactjawad

Copy link
Copy Markdown

What

getEvenSpacing (used by autoSkip to place minor ticks so they divide evenly spaced major ticks into even chunks) failed to recognize an evenly spaced set of major-tick indices whenever the first major index differs from the spacing value — most notably when the first major tick is at index 0.

Why

The reference gap was seeded with arr[0] (an absolute index) while the loop body compares consecutive index differences:

for (diff = arr[0], i = 1; i < len; ++i) {
  if (arr[i] - arr[i - 1] !== diff) {
    return false;
  }
}

For majors at indices [0, 20, 40], diff starts as 0, so the very first comparison arr[1] - arr[0] (20) !== 0 returns false, incorrectly reporting the majors as unevenly spaced. autoSkip then falls back to raw ticks.length / ticksLimit spacing instead of a factor of the major spacing.

How

Seed the gap with the first actual difference and begin comparing at the second gap:

for (diff = arr[1] - arr[0], i = 2; i < len; ++i) {

Sets that genuinely start at their spacing (e.g. [20, 40, 60]) are unchanged, non-even sets still return false, and even sets that start at index 0 are now detected correctly.

Test

Added a spec in test/specs/scale.time.tests.js using an hourly time axis that starts on a day boundary, so day-start major ticks fall at indices 0, 24, 48, .... With major.enabled and autoSkip, the kept minor ticks now divide the 24h major interval into even chunks (8h spacing, 18 ticks). Before the fix the even spacing was not detected and the raw ratio was used (6h spacing, 24 ticks); the test asserts the 8h spacing and fails on the previous behavior.

…ex 0

getEvenSpacing seeded the reference gap with arr[0] (an absolute index)
and started comparing at i=1, so any evenly-spaced set of major ticks
whose first index differs from the spacing (e.g. majors at 0, 20, 40)
was wrongly reported as unevenly spaced. Seed the gap with the first
actual difference (arr[1] - arr[0]) and start at i=2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant