Skip to content

fix(category): do not modify the user provided labels array - #12344

Open
saadkhantareen wants to merge 1 commit into
chartjs:masterfrom
saadkhantareen:fix/category-scale-labels-mutation
Open

saadkhantareen wants to merge 1 commit into
chartjs:masterfrom
saadkhantareen:fix/category-scale-labels-mutation

Conversation

@saadkhantareen

Copy link
Copy Markdown

Summary

The category scale generates labels for data values that are missing from the labels array, but it did so by pushing to (and later splicing out of) the exact array instance the user provided, either as data.labels or as the scale labels option. The user's array was left modified after the chart ran. The scale now works on its own copy of the labels array, so the array provided by the user is never modified, while the generated labels keep working exactly as before.

Changes

  • src/scales/scale.category.js: CategoryScale caches a copy of the labels array (taken in the new getLabels() override) and discards the cache on init(), when the copy is rebuilt from the current source array. The previous init() cleanup that spliced parse-added labels back out of the user's array is no longer needed and was removed.
  • test/specs/scale.category.tests.js: two regression tests covering both label sources (scale labels option and data.labels) asserting that missing labels are still generated for the ticks while the user-provided array stays unchanged.

Tests

Karma + Jasmine in headless Chrome (specs selected by file name via --grep):

  • karma start --single-run --browsers ChromeHeadless --grep scale.category — 26 passed (24 existing + 2 new), 0 failed. The two new tests were verified to fail before the fix.
  • Remaining spec groups, all passing with 0 failures: --grep scale 440 passed, --grep controller 553 passed, --grep core 419 passed, --grep plugin 242 passed, --grep element 59 passed, --grep helpers 219 passed, --grep platform 26 passed, --grep global 9 passed, --grep mixed 4 passed.
  • eslint src/scales/scale.category.js test/specs/scale.category.tests.js — clean.

Fixes #11893

Copilot AI balanced review requested due to automatic review settings October 14, 2026 10:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

Array passed as labels option is modified by Chart.js

2 participants