]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Allow specifying labels in time scale options (#6257)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Tue, 21 May 2019 11:36:16 +0000 (14:36 +0300)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Tue, 21 May 2019 11:36:16 +0000 (13:36 +0200)
docs/axes/cartesian/time.md
src/core/core.scale.js
src/scales/scale.category.js
src/scales/scale.time.js
test/specs/scale.time.tests.js

index bdd4d1b65f4a91428858f4afddefa1c8a2c38c2e..df51f00a111172c3f45022e7971d6b9e4339989c 100644 (file)
@@ -149,7 +149,7 @@ The `ticks.source` property controls the ticks generation.
 
 * `'auto'`: generates "optimal" ticks based on scale size and time options
 * `'data'`: generates ticks from data (including labels from data `{t|x|y}` objects)
-* `'labels'`: generates ticks from user given `data.labels` values ONLY
+* `'labels'`: generates ticks from user given `labels` ONLY
 
 ### Parser
 If this property is defined as a string, it is interpreted as a custom format to be used by Moment.js to parse the date.
index 7fccd4d4a1522b1b849df44c93581798d78a27c8..7d14be21c2427bb97420ec253d1de9ba5b5e5a1e 100644 (file)
@@ -214,6 +214,14 @@ var Scale = Element.extend({
                return this._ticks;
        },
 
+       /**
+       * @private
+       */
+       _getLabels: function() {
+               var data = this.chart.data;
+               return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
+       },
+
        // These methods are ordered by lifecyle. Utilities then follow.
        // Any function defined here is inherited by all scale types.
        // Any function can be extended by the scale type
index b9e51bcb1623479bdaa6f296c816d5188c7270eb..1d8949d4df608c04a892a57812b3a5174c9449a2 100644 (file)
@@ -7,19 +7,9 @@ var defaultConfig = {
 };
 
 module.exports = Scale.extend({
-       /**
-       * Internal function to get the correct labels. If data.xLabels or data.yLabels are defined, use those
-       * else fall back to data.labels
-       * @private
-       */
-       getLabels: function() {
-               var data = this.chart.data;
-               return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
-       },
-
        determineDataLimits: function() {
                var me = this;
-               var labels = me.getLabels();
+               var labels = me._getLabels();
                me.minIndex = 0;
                me.maxIndex = labels.length - 1;
                var findIndex;
@@ -42,7 +32,7 @@ module.exports = Scale.extend({
 
        buildTicks: function() {
                var me = this;
-               var labels = me.getLabels();
+               var labels = me._getLabels();
                // If we are viewing some subset of labels, slice the original array
                me.ticks = (me.minIndex === 0 && me.maxIndex === labels.length - 1) ? labels : labels.slice(me.minIndex, me.maxIndex + 1);
        },
@@ -72,7 +62,7 @@ module.exports = Scale.extend({
                        valueCategory = me.isHorizontal() ? value.x : value.y;
                }
                if (valueCategory !== undefined || (value !== undefined && isNaN(index))) {
-                       var labels = me.getLabels();
+                       var labels = me._getLabels();
                        value = valueCategory || value;
                        var idx = labels.indexOf(value);
                        index = idx !== -1 ? idx : index;
index b00daa2403e674c3dd1d8d71da2480517b445450..1be680ef5ed8edbaad3787830c5a792279606906 100644 (file)
@@ -519,7 +519,7 @@ module.exports = Scale.extend({
                var datasets = [];
                var labels = [];
                var i, j, ilen, jlen, data, timestamp;
-               var dataLabels = chart.data.labels || [];
+               var dataLabels = me._getLabels();
 
                // Convert labels to timestamps
                for (i = 0, ilen = dataLabels.length; i < ilen; ++i) {
index 80f127da527eac34b881d904b15517a824e09a1b..39a920ccd26c110c1eeda69aa0746d54d067f4c4 100755 (executable)
@@ -1682,6 +1682,57 @@ describe('Time scale tests', function() {
                });
        });
 
+       describe('labels', function() {
+               it('should read labels from scale / xLabels / yLabels', function() {
+                       var timeOpts = {
+                               parser: 'YYYY',
+                               unit: 'year',
+                               displayFormats: {
+                                       year: 'YYYY'
+                               }
+                       };
+                       var chart = window.acquireChart({
+                               type: 'line',
+                               data: {
+                                       labels: ['1975', '1976', '1977'],
+                                       xLabels: ['1985', '1986', '1987'],
+                                       yLabels: ['1995', '1996', '1997']
+                               },
+                               options: {
+                                       scales: {
+                                               xAxes: [{
+                                                       id: 'x',
+                                                       type: 'time',
+                                                       labels: ['2015', '2016', '2017'],
+                                                       time: timeOpts
+                                               },
+                                               {
+                                                       id: 'x2',
+                                                       type: 'time',
+                                                       time: timeOpts
+                                               }],
+                                               yAxes: [{
+                                                       id: 'y',
+                                                       type: 'time',
+                                                       time: timeOpts
+                                               },
+                                               {
+                                                       id: 'y2',
+                                                       type: 'time',
+                                                       labels: ['2005', '2006', '2007'],
+                                                       time: timeOpts
+                                               }]
+                                       }
+                               }
+                       });
+
+                       expect(getTicksLabels(chart.scales.x)).toEqual(['2015', '2016', '2017']);
+                       expect(getTicksLabels(chart.scales.x2)).toEqual(['1985', '1986', '1987']);
+                       expect(getTicksLabels(chart.scales.y)).toEqual(['1995', '1996', '1997']);
+                       expect(getTicksLabels(chart.scales.y2)).toEqual(['2005', '2006', '2007']);
+               });
+       });
+
        describe('Deprecations', function() {
                describe('options.time.displayFormats', function() {
                        it('should generate defaults from adapter presets', function() {