Repository navigation
fix: keep inferred category labels in data order with string bounds - #12431
ravitejas-tech wants to merge 4 commits into
Conversation
✅ Deploy Preview for chartjs-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
kurkle
left a comment
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
| this._suggestedMin = this.parse(options.suggestedMin); | ||
| this._suggestedMax = this.parse(options.suggestedMax); |
There was a problem hiding this comment.
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 missingPlease add a spec like this for suggestedMax, plus the mirror case for suggestedMin, asserting scale.min and scale.max.
|
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 |
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.
e8f36c0 to
378db87
Compare
|
Thanks for the review. I rebased onto master and addressed both points. Rebase and single The branch now carries only the new work on top of master; the two commits from #12414 are dropped. Specs for the suggested-bound re-parse I added two specs based on your hidden-dataset case, each asserting
Removing the 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. |
|
Thanks for the rebase. The single 1. The docs sentence. 2. The
So the expectation becomes |
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.
|
Both done in the last two commits. Docs: added a sentence under "Min Max Configuration" in
All 35 category specs pass in Chrome. |
|
Size Change: +182 B (+0.05%) Total Size: 362 kB 📦 View Changed
ℹ️ View Unchanged
|
Fixes #12430.
String
minandmax(andsuggestedMin/suggestedMax) on a category scale were parsed ininit(), 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.Changes
init()now only looks string bounds up and never adds them, so they cannot land in front of the inferred labels.beforeLayout()(from fix: apply category bounds after inferring labels #12414) also parsessuggestedMinandsuggestedMaxagain, since they had the same problem.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 whendata.labelsis 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
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.eslintand the type check pass.plugin.filler,scale.radialLinear,scale.time) that fail the same way onmasterin headless Chrome on my machine. I could not run Firefox or Safari locally.