Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash with skipNull and uneven datasets #10454

Merged
merged 1 commit into from Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 16 additions & 21 deletions src/controllers/controller.bar.js
Expand Up @@ -390,41 +390,36 @@ export default class BarController extends DatasetController {
* @private
*/
_getStacks(last, dataIndex) {
const meta = this._cachedMeta;
const iScale = meta.iScale;
const metasets = iScale.getMatchingVisibleMetas(this._type);
const {iScale} = this._cachedMeta;
const metasets = iScale.getMatchingVisibleMetas(this._type)
.filter(meta => meta.controller.options.grouped);
const stacked = iScale.options.stacked;
const ilen = metasets.length;
const stacks = [];
let i, item;

for (i = 0; i < ilen; ++i) {
item = metasets[i];
const skipNull = (meta) => {
const parsed = meta.controller.getParsed(dataIndex);
const val = parsed && parsed[meta.vScale.axis];

if (!item.controller.options.grouped) {
continue;
if (isNullOrUndef(val) || isNaN(val)) {
return true;
}
};

if (typeof dataIndex !== 'undefined') {
const val = item.controller.getParsed(dataIndex)[
item.controller._cachedMeta.vScale.axis
];

if (isNullOrUndef(val) || isNaN(val)) {
continue;
}
for (const meta of metasets) {
if (dataIndex !== undefined && skipNull(meta)) {
continue;
}

// stacked | meta.stack
// | found | not found | undefined
// false | x | x | x
// true | | x |
// undefined | | x | x
if (stacked === false || stacks.indexOf(item.stack) === -1 ||
(stacked === undefined && item.stack === undefined)) {
stacks.push(item.stack);
if (stacked === false || stacks.indexOf(meta.stack) === -1 ||
(stacked === undefined && meta.stack === undefined)) {
stacks.push(meta.stack);
}
if (item.index === last) {
if (meta.index === last) {
break;
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/specs/controller.bar.tests.js
Expand Up @@ -1655,4 +1655,24 @@ describe('Chart.controllers.bar', function() {
expect(ctx.getCalls().filter(x => x.name === 'clip').length).toEqual(0);
});
});

it('should not crash with skipNull and uneven datasets', function() {
function unevenChart() {
window.acquireChart({
type: 'bar',
data: {
labels: [1, 2],
datasets: [
{data: [1, 2]},
{data: [1, 2, 3]},
]
},
options: {
skipNull: true,
}
});
}

expect(unevenChart).not.toThrow();
});
});