Skip to content

fix(scale): avoid duplicate final tick with bounds:'data' and fractional max - #12288

Open
contactjawad wants to merge 1 commit into
chartjs:masterfrom
contactjawad:fix-linear-bounds-data-duplicate-tick
Open

fix(scale): avoid duplicate final tick with bounds:'data' and fractional max#12288
contactjawad wants to merge 1 commit into
chartjs:masterfrom
contactjawad:fix-linear-bounds-data-duplicate-tick

Conversation

@contactjawad

Copy link
Copy Markdown

What

A linear scale using bounds: 'data' emits a duplicate final tick (and a duplicate axis label) when the data maximum has a fractional part that rounds down to the last generated integer tick. For example, data [0, 5.4] produces labels ['0','1','2','3','4','5','5'].

Why

In generateTicks, with bounds: 'data' the niceMax is the raw data max, and numSpaces is Math.ceil((niceMax - niceMin) / spacing). The loop therefore emits ticks up to and including the integer just below the data max (e.g. 5 for a max of 5.4). niceMax is then rounded to the tick factor and unconditionally pushed again:

} else if (!maxDefined || niceMax === max) {
  ticks.push({value: niceMax});
}

When the rounded niceMax equals the last loop-emitted tick, that value is added twice.

How

Guard the final push so it does not duplicate the last emitted tick:

} else if (!maxDefined || niceMax === max) {
  if (!ticks.length || ticks[ticks.length - 1].value !== niceMax) {
    ticks.push({value: niceMax});
  }
}

Test

Added a spec in test/specs/scale.linear.tests.js alongside the existing "Should bound to data" test, using dataset [0, 5.4], bounds: 'data' and ticks.autoSkip: false, asserting the labels are ['0','1','2','3','4','5']. Before the fix the labels are ['0','1','2','3','4','5','5'], so the test fails on the previous behavior and passes with the fix.

…nal max

When a linear scale bounds to data and the data max has a fractional part
that rounds down to the last emitted tick, the rounded niceMax was pushed
again, producing a duplicate final tick and axis label. Skip the push when
it would duplicate the last tick.
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