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

Skip ticks by callback value as documented #8914

Merged
merged 1 commit into from Apr 17, 2021
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
8 changes: 8 additions & 0 deletions src/core/core.scale.js
Expand Up @@ -550,6 +550,14 @@ export default class Scale extends Element {
tick = ticks[i];
tick.label = call(tickOpts.callback, [tick.value, i, ticks], me);
}
// Ticks should be skipped when callback returns null or undef, so lets remove those.
for (i = 0; i < ilen; i++) {
if (isNullOrUndef(ticks[i].label)) {
ticks.splice(i, 1);
ilen--;
i--;
}
}
}
afterTickToLabelConversion() {
call(this.options.afterTickToLabelConversion, [this]);
Expand Down
44 changes: 44 additions & 0 deletions test/fixtures/core.scale/ticks/skip-by-callback.js
@@ -0,0 +1,44 @@
module.exports = {
description: 'https://github.com/chartjs/Chart.js/issues/8892',
config: {
type: 'line',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [
{
data: [12, 19, 3, 5, 2, 3],
},
{
data: [7, 11, 5, 8, 3, 7],
}
]
},
options: {
scales: {
x: {
ticks: {
callback: function(val, index) {
if (index === 1) {
return undefined;
}
if (index === 3) {
return null;
}
return this.getLabelForValue(val);
}
}
},
y: {
ticks: {
callback: function(val, index) {
return index % 2 === 0 ? '' + val : null;
}
}
}
},
}
},
options: {
spriteText: true
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.