Skip to content

fix: keep inferred category labels in data order with string bounds - #12431

Open
ravitejas-tech wants to merge 4 commits into
chartjs:masterfrom
ravitejas-tech:fix/category-string-bounds
Open

ravitejas-tech wants to merge 4 commits into
chartjs:masterfrom
ravitejas-tech:fix/category-string-bounds

Conversation

@ravitejas-tech

@ravitejas-tech ravitejas-tech commented Nov 23, 2026 •

Copy link
Copy Markdown

Fixes #12430.

String min and max (and suggestedMin / suggestedMax) on a category scale were parsed in init(), before the datasets had added the labels inferred from the data. CategoryScale.parse() adds a string it cannot find, so the bounds became the first labels and the data labels followed.

const chart = new Chart(ctx, {
  type: 'line',
  data: {
    // no labels: the category scale takes them from the x values
    datasets: [{data: ['L0', 'L1', 'L2', 'L3', 'L4', 'L5'].map((x, i) => ({x, y: i}))}]
  },
  options: {scales: {x: {min: 'L1', max: 'L3'}}}
});

chart.scales.x.getLabels(); // before: ['L1', 'L3', 'L0', 'L2', 'L4', 'L5']  after: ['L0', 'L1', 'L2', 'L3', 'L4', 'L5']
[chart.scales.x.min, chart.scales.x.max]; // before: [0, 1]  after: [1, 3]

Changes

Behaviour change

A bound that is not among the data (for example max: 'ZZ') is now added after the data labels instead of before them. This matches what already happens when data.labels is given. The docs say a string bound must be one of the labels, so this case was not documented either way; happy to change it if you would rather ignore an unknown bound.

Tests

  • New specs: string bounds on both index axes (including chart.update() and changed bounds), string suggested bounds, and a bound that is not in the data. They fail before this change and pass after it.
  • All category specs pass in headless Chrome, and eslint and the type check pass.
  • On the full suite locally the only failures are 7 image fixtures (plugin.filler, scale.radialLinear, scale.time) that fail the same way on master in headless Chrome on my machine. I could not run Firefox or Safari locally.

@netlify

netlify Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for chartjs-docs ready!

Name Link
🔨 Latest commit fe26535
🔍 Latest deploy log https://app.netlify.com/projects/chartjs-docs/deploys/6b054f784fcf6407a8031ab2
😎 Deploy Preview https://deploy-preview-12431--chartjs-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@kurkle kurkle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Needs a rebase onto master before it can be merged: #12414 has been merged in a different form, and merging this branch now leaves two beforeLayout() methods in the category scale.

Related pull requests

#12405, #12344 and #12222 change the same file or spec for other issues, and each conflicts with this branch. Whichever is merged later will need a rebase.

This pull request is the first piece of the plan in #12434 for the category scale's labels. Appending an unknown string bound after the data labels, as this PR does, is the behaviour the plan settles on.

this._lookupOnly = false;
}

beforeLayout() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Rebase onto master; the merge drops the suggested-bound re-parse and fails lint. #12414 was squash-merged with its own beforeLayout(), which re-parses only min and max. Git reports a conflict only in the spec file. This file merges without a conflict, but the class then has both methods: this one and master's (line 89 after the merge). The later one wins, so suggestedMin/suggestedMax are no longer re-parsed, and pnpm lint fails:

src/scales/scale.category.js
  89:3  error  Duplicate name 'beforeLayout'  no-dupe-class-members

When you rebase, keep a single beforeLayout() that re-parses all four bounds, and resolve the spec conflict against master's tests from #12414.

Comment on lines +85 to +86
this._suggestedMin = this.parse(options.suggestedMin);
this._suggestedMax = this.parse(options.suggestedMax);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No test covers these two lines. Deleting either one still passes all 33 category specs. "should not reorder inferred labels for string suggested bounds" checks only the label order, which the lookup-only init() already guarantees. A case where the re-parse decides the result is a suggested bound that only a hidden dataset reaches:

const chart = new Chart(ctx, {
  type: 'line',
  data: {datasets: [
    {data: [{x: 'L0', y: 0}, {x: 'L1', y: 1}, {x: 'L2', y: 2}]},
    {data: [{x: 'L3', y: 3}, {x: 'L4', y: 4}], hidden: true}
  ]},
  options: {scales: {x: {bounds: 'data', suggestedMax: 'L4'}}}
});
chart.scales.x.max; // 4 with this PR; 2 when the suggestedMax re-parse is missing

Please add a spec like this for suggestedMax, plus the mirror case for suggestedMin, asserting scale.min and scale.max.

@kurkle

kurkle commented Nov 23, 2026

Copy link
Copy Markdown
Member

A correction to the "Related pull requests" part of my review: #12405, #12344 and #12222 are not just merge-order conflicts. In the plan in #12434, this PR is the first piece (P1) and lands first. #12344's approach becomes P3, where each category axis owns a copy of its labels; #12405 is then not needed, and only the stacked case of #12222 is carried over.

P3 also removes the lookup-only flag this PR adds, so it is a temporary step and does not need to be made more general. What P1 still needs from this PR is the rebase onto master with a single beforeLayout() that parses all four bounds, and a spec that pins the suggestedMin/suggestedMax re-parse.

String min, max, suggestedMin and suggestedMax were parsed in init(),
before the datasets had added the labels inferred from the data. The
bounds were added as the first labels and the data labels came after
them.

Only look the bounds up in init() and resolve them in beforeLayout(),
after dataset parsing. A bound that is not among the data is now added
after the data labels.

Fixes chartjs#12430
The existing suggested bounds spec only checks the label order, which
the lookup in init() already guarantees. These two specs fail when the
suggestedMin or suggestedMax parse in beforeLayout() is removed.
@ravitejas-tech
ravitejas-tech force-pushed the fix/category-string-bounds branch from e8f36c0 to 378db87 Compare November 24, 2026 06:08
@ravitejas-tech

Copy link
Copy Markdown
Author

Thanks for the review. I rebased onto master and addressed both points.

Rebase and single beforeLayout()

The branch now carries only the new work on top of master; the two commits from #12414 are dropped. CategoryScale has one beforeLayout() that parses min, max, suggestedMin and suggestedMax. With the #12414 commits out there was no spec conflict left, and master's tests from #12414 are untouched. pnpm lint-js reports no errors.

Specs for the suggested-bound re-parse

I added two specs based on your hidden-dataset case, each asserting scale.min and scale.max:

  • suggestedMax: 'L4', where only a hidden dataset reaches L4
  • suggestedMin: 'L0', the mirror case with the hidden dataset first

Removing the _suggestedMax line in beforeLayout() fails the first (Expected 2 to be 4), and removing the _suggestedMin line fails the second (Expected 2 to be 0).

All 35 category specs pass locally in Chrome; I did not run Firefox locally. I left the lookup-only flag as it is, since P3 removes it, and I removed the note about #12414 from the description.

@kurkle

kurkle commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thanks for the rebase. The single beforeLayout() and the new specs look right: I ran the category specs on 378db87 (35/35 in Chrome), and removing any of the four re-parses, the lookup-only flag, or its reset makes a spec fail. Two things remain before this matches P1 in #12434:

1. The docs sentence. docs/axes/cartesian/category.md, "Min Max Configuration", still says the value must be a string in the labels array or an index. With this PR, a string bound that is not among the labels is added after them (your max: 'ZZ' spec). Please add one sentence saying so, and that it applies to suggestedMin and suggestedMax too.

2. The suggestedMin hidden-dataset spec also passes on master. On master, init() adds 'L0' as the first label, and the data starts with L0 anyway, so the order and the index come out the same. Using suggestedMin: 'L1' with the same datasets makes it show the fix:

  • master: labels [L1, L0, L2, L3, L4], min 0;
  • without the _suggestedMin re-parse: min 2 (the lookup in init() finds nothing);
  • with this PR: labels [L0, L1, L2, L3, L4], min 1.

So the expectation becomes expect(chart.scales.x.min).toBe(1).

With suggestedMin 'L0' the spec also passed on master, where init() added
the bound as the first label and the data started with L0 anyway. With
'L1' master gives labels [L1, L0, ...] and min 0, a missing re-parse
gives min 2, and the fix gives min 1.
@ravitejas-tech

Copy link
Copy Markdown
Author

Both done in the last two commits.

Docs: added a sentence under "Min Max Configuration" in docs/axes/cartesian/category.md saying that a string that is not among the labels is added after them, and that this also applies to suggestedMin and suggestedMax.

suggestedMin spec: it now uses suggestedMin: 'L1' and expects min to be 1. Run on its own, it gives the three results you listed: with master's scale.category.js the labels start [L1, L0, ...] and min is 0, without the _suggestedMin re-parse min is 2, and on this branch it passes.

All 35 category specs pass in Chrome.

@github-actions

Copy link
Copy Markdown

Size Change: +182 B (+0.05%)

Total Size: 362 kB

📦 View Changed
Filename Size Change
dist/chart.cjs 81.5 kB +45 B (+0.06%)
dist/chart.js 81.8 kB +44 B (+0.05%)
dist/chart.umd.js 72.1 kB +46 B (+0.06%)
dist/chart.umd.min.js 72.1 kB +47 B (+0.07%)
ℹ️ View Unchanged
Filename Size
dist/chunks/helpers.dataset.cjs 25.7 kB
dist/chunks/helpers.dataset.js 25.5 kB
dist/helpers.cjs 1.52 kB
dist/helpers.js 1.33 kB

compressed-size-action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Category scale: string min and max reorder the labels when the labels come from the data

2 participants