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

(re)allow modifying ticks in afterBuildTicks #5913

Merged
merged 3 commits into from Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion docs/axes/README.md
Expand Up @@ -31,7 +31,7 @@ There are a number of config callbacks that can be used to change parameters in
| `beforeDataLimits` | `axis` | Callback that runs before data limits are determined.
| `afterDataLimits` | `axis` | Callback that runs after data limits are determined.
| `beforeBuildTicks` | `axis` | Callback that runs before ticks are created.
| `afterBuildTicks` | `axis` | Callback that runs after ticks are created. Useful for filtering ticks.
| `afterBuildTicks` | `axis`, `ticks` | Callback that runs after ticks are created. Useful for filtering ticks (*Should return the filtered ticks*).
simonbrunel marked this conversation as resolved.
Show resolved Hide resolved
| `beforeTickToLabelConversion` | `axis` | Callback that runs before ticks are converted into strings.
| `afterTickToLabelConversion` | `axis` | Callback that runs after ticks are converted into strings.
| `beforeCalculateTickRotation` | `axis` | Callback that runs before tick rotation is determined.
Expand Down
14 changes: 11 additions & 3 deletions src/core/core.scale.js
Expand Up @@ -193,7 +193,8 @@ module.exports = Element.extend({
// we still support no return (`this.ticks` internally set by calling this method).
ticks = me.buildTicks() || [];

me.afterBuildTicks();
// Allow modification of ticks in callback.
ticks = me.afterBuildTicks(ticks) || ticks;

me.beforeTickToLabelConversion();

Expand Down Expand Up @@ -287,8 +288,15 @@ module.exports = Element.extend({
helpers.callback(this.options.beforeBuildTicks, [this]);
},
buildTicks: helpers.noop,
afterBuildTicks: function() {
helpers.callback(this.options.afterBuildTicks, [this]);
afterBuildTicks: function(ticks) {
var me = this;
// ticks is empty for old axis implementations here
if (ticks.length) {
simonbrunel marked this conversation as resolved.
Show resolved Hide resolved
return helpers.callback(me.options.afterBuildTicks, [me, ticks]);
}
// Support old implementatios (that modified `this.ticks` directly in buildTicks)
simonbrunel marked this conversation as resolved.
Show resolved Hide resolved
me.ticks = helpers.callback(me.options.afterBuildTicks, [me, me.ticks]) || me.ticks;
simonbrunel marked this conversation as resolved.
Show resolved Hide resolved
return ticks;
},

beforeTickToLabelConversion: function() {
Expand Down
95 changes: 95 additions & 0 deletions test/specs/core.scale.tests.js
Expand Up @@ -341,4 +341,99 @@ describe('Core.scale', function() {
});
});
});

describe('afterBuildTicks', function() {
it('should allow filtering of ticks', function() {
var labels = ['tick1', 'tick2', 'tick3', 'tick4', 'tick5'];
var chart = window.acquireChart({
type: 'line',
options: {
scales: {
xAxes: [{
id: 'x',
type: 'category',
labels: labels,
afterBuildTicks: function(axis, ticks) {
return ticks.slice(1);
}
}]
}
}
});

var scale = chart.scales.x;
expect(scale.ticks).toEqual(labels.slice(1));
});

it('should allow filtering of ticks (for new implementation of buildTicks)', function() {
var chart = window.acquireChart({
type: 'line',
data: {
labels: ['2016', '2017', '2018']
},
options: {
scales: {
xAxes: [{
id: 'x',
type: 'time',
time: {
parser: 'YYYY'
},
ticks: {
source: 'labels'
},
afterBuildTicks: function(axis, ticks) {
return ticks.slice(1);
}
}]
}
}
});

var scale = chart.scales.x;
expect(scale.ticks.length).toEqual(2);
});

it('should allow no return value from callback', function() {
var labels = ['tick1', 'tick2', 'tick3', 'tick4', 'tick5'];
var chart = window.acquireChart({
type: 'line',
options: {
scales: {
xAxes: [{
id: 'x',
type: 'category',
labels: labels,
afterBuildTicks: function() { }
}]
}
}
});

var scale = chart.scales.x;
expect(scale.ticks).toEqual(labels);
});

it('should allow empty ticks', function() {
var labels = ['tick1', 'tick2', 'tick3', 'tick4', 'tick5'];
var chart = window.acquireChart({
type: 'line',
options: {
scales: {
xAxes: [{
id: 'x',
type: 'category',
labels: labels,
afterBuildTicks: function() {
return [];
}
}]
}
}
});

var scale = chart.scales.x;
expect(scale.ticks.length).toBe(0);
});
});
});