]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix modifying ticks in afterBuildTicks (#5913)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Fri, 11 Jan 2019 06:29:38 +0000 (08:29 +0200)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Fri, 11 Jan 2019 06:29:38 +0000 (07:29 +0100)
docs/axes/README.md
src/core/core.scale.js
test/specs/core.scale.tests.js

index 90b2525e79ecdfd2cd1391e9d7fe80aec5955bfa..ba408409e0cd3966f049f8af0996537604859dc6 100644 (file)
@@ -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.
 | `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.
index f4c38aa256598fb9350b0d6c5094afd83302abda..ff73d98115fc95e868964152e7063a4505ae76da 100644 (file)
@@ -196,7 +196,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();
 
@@ -290,8 +291,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 (helpers.isArray(ticks) && ticks.length) {
+                       return helpers.callback(me.options.afterBuildTicks, [me, ticks]);
+               }
+               // Support old implementations (that modified `this.ticks` directly in buildTicks)
+               me.ticks = helpers.callback(me.options.afterBuildTicks, [me, me.ticks]) || me.ticks;
+               return ticks;
        },
 
        beforeTickToLabelConversion: function() {
index 3cbd0cbe6d0db7a157f2417290f733bf8817a379..130232caa1bf16bcaa68f73c5ac212be18277353 100644 (file)
@@ -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);
+               });
+       });
 });