Skip to content

Commit

Permalink
Skip ticks by callback value as documented (#8914)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Apr 17, 2021
1 parent f94e882 commit 8e535c5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
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.

0 comments on commit 8e535c5

Please sign in to comment.