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

null or undefined should skip grid lines in the time scale #9252

Merged
merged 2 commits into from Jun 12, 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
18 changes: 10 additions & 8 deletions src/core/core.scale.js
Expand Up @@ -552,14 +552,6 @@ 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 Expand Up @@ -769,6 +761,16 @@ export default class Scale extends Element {

me.generateTickLabels(ticks);

// Ticks should be skipped when callback returns null or undef, so lets remove those.
let i, ilen;
for (i = 0, ilen = ticks.length; i < ilen; i++) {
if (isNullOrUndef(ticks[i].label)) {
ticks.splice(i, 1);
ilen--;
i--;
}
}

me.afterTickToLabelConversion();
}

Expand Down
32 changes: 32 additions & 0 deletions test/fixtures/scale.time/skip-null-gridlines.js
@@ -0,0 +1,32 @@
module.exports = {
threshold: 0.01,
tolerance: 0.0025,
config: {
type: 'line',
data: {
labels: ['2017', '2018', '2019', '2020', '2025'],
datasets: [{data: [0, 1, 2, 3, 4], fill: false}]
},
options: {
scales: {
x: {
type: 'time',
time: {
parser: 'YYYY',
unit: 'year'
},
ticks: {
source: 'auto',
callback: (tick, index) => index % 2 === 0 ? null : tick,
}
},
y: {
display: false
}
}
}
},
options: {
spriteText: true
}
};
Binary file added test/fixtures/scale.time/skip-null-gridlines.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions test/fixtures/scale.time/skip-undefined-gridlines.js
@@ -0,0 +1,32 @@
module.exports = {
threshold: 0.01,
tolerance: 0.0025,
config: {
type: 'line',
data: {
labels: ['2017', '2018', '2019', '2020', '2025'],
datasets: [{data: [0, 1, 2, 3, 4], fill: false}]
},
options: {
scales: {
x: {
type: 'time',
time: {
parser: 'YYYY',
unit: 'year'
},
ticks: {
source: 'auto',
callback: (tick, index) => index % 2 === 0 ? undefined : tick,
}
},
y: {
display: false
}
}
}
},
options: {
spriteText: true
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.