Skip to content

Commit

Permalink
null or undefined should skip grid lines in the time scale (#9252)
Browse files Browse the repository at this point in the history
* `null` or `undefined` should skip grid lines in the time scale

* Refactor implementation per code review
  • Loading branch information
etimberg committed Jun 12, 2021
1 parent 6a46cc3 commit 9db3680
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
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.

0 comments on commit 9db3680

Please sign in to comment.