From 4b5c2dc29cc678111706a2e23d4d8c550c281d6e Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Wed, 8 May 2019 10:03:54 +0300 Subject: [PATCH 1/5] Allow specifying labels in time scale options --- docs/axes/cartesian/time.md | 2 +- src/scales/scale.time.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/axes/cartesian/time.md b/docs/axes/cartesian/time.md index bdd4d1b65f4..4f9d14872c1 100644 --- a/docs/axes/cartesian/time.md +++ b/docs/axes/cartesian/time.md @@ -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 `scale.labels` or `data.labels` values 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. diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 2e9c14b8e22..d708d364bc6 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -523,7 +523,7 @@ module.exports = Scale.extend({ var datasets = []; var labels = []; var i, j, ilen, jlen, data, timestamp; - var dataLabels = chart.data.labels || []; + var dataLabels = options.labels || chart.data.labels || []; // Convert labels to timestamps for (i = 0, ilen = dataLabels.length; i < ilen; ++i) { From 8d21f5033646455a2e3f6ccc8c590e4cbb555b41 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Sat, 11 May 2019 15:48:13 +0300 Subject: [PATCH 2/5] Consistent _getLabels() --- docs/axes/cartesian/time.md | 2 +- src/core/core.scale.js | 9 +++++++++ src/scales/scale.category.js | 16 +++------------- src/scales/scale.time.js | 2 +- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/docs/axes/cartesian/time.md b/docs/axes/cartesian/time.md index 4f9d14872c1..df51f00a111 100644 --- a/docs/axes/cartesian/time.md +++ b/docs/axes/cartesian/time.md @@ -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 `scale.labels` or `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. diff --git a/src/core/core.scale.js b/src/core/core.scale.js index cf1fb281e88..06a3d548032 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -214,6 +214,15 @@ 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 diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index b9e51bcb162..1d8949d4df6 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -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; diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index d708d364bc6..9e1faaf628c 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -523,7 +523,7 @@ module.exports = Scale.extend({ var datasets = []; var labels = []; var i, j, ilen, jlen, data, timestamp; - var dataLabels = options.labels || chart.data.labels || []; + var dataLabels = me._getLabels(); // Convert labels to timestamps for (i = 0, ilen = dataLabels.length; i < ilen; ++i) { From 0222e3138aecc934772d68c0129b0e4f8ff8e6f9 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Sat, 11 May 2019 20:19:00 +0300 Subject: [PATCH 3/5] Remove extra line --- src/core/core.scale.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/core/core.scale.js b/src/core/core.scale.js index 06a3d548032..d4e49588e24 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -214,7 +214,6 @@ var Scale = Element.extend({ return this._ticks; }, - /** * @private */ From c7c0364ade4fd7d47288668e9e68fd412da6ff8f Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Sat, 18 May 2019 17:17:43 +0300 Subject: [PATCH 4/5] Add a unit test --- test/specs/scale.time.tests.js | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 616f5c40a80..04af7de164a 100755 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -1644,6 +1644,58 @@ describe('Time scale tests', function() { }); }); + describe('labels', function() { + it('should read labels from scale / xLabels / yLabels', function() { + 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: { + parser: 'YYYY', + } + }, + { + id: 'x2', + type: 'time', + time: { + parser: 'YYYY', + } + }], + yAxes: [{ + id: 'y', + type: 'time', + time: { + parser: 'YYYY', + } + }, + { + id: 'y2', + type: 'time', + labels: ['2005', '2006', '2007'], + time: { + parser: 'YYYY', + } + }] + } + } + }); + + expect(getTicksLabels(chart.scales.x)).toEqual(['Jan 2015', 'Jan 2016', 'Jan 2017']); + expect(getTicksLabels(chart.scales.x2)).toEqual(['Jan 1985', 'Jan 1986', 'Jan 1987']); + expect(getTicksLabels(chart.scales.y)).toEqual(['Jan 1995', 'Jan 1996', 'Jan 1997']); + expect(getTicksLabels(chart.scales.y2)).toEqual(['Jan 2005', 'Jan 2006', 'Jan 2007']); + }); + }); + describe('Deprecations', function() { describe('options.time.displayFormats', function() { it('should generate defaults from adapter presets', function() { From 2ada7868379b87118932b30762612633341b6a90 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Tue, 21 May 2019 13:45:26 +0300 Subject: [PATCH 5/5] Stabilize test by providing unit and format --- test/specs/scale.time.tests.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 04af7de164a..73c41981863 100755 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -1646,6 +1646,13 @@ 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: { @@ -1659,40 +1666,32 @@ describe('Time scale tests', function() { id: 'x', type: 'time', labels: ['2015', '2016', '2017'], - time: { - parser: 'YYYY', - } + time: timeOpts }, { id: 'x2', type: 'time', - time: { - parser: 'YYYY', - } + time: timeOpts }], yAxes: [{ id: 'y', type: 'time', - time: { - parser: 'YYYY', - } + time: timeOpts }, { id: 'y2', type: 'time', labels: ['2005', '2006', '2007'], - time: { - parser: 'YYYY', - } + time: timeOpts }] } } }); - expect(getTicksLabels(chart.scales.x)).toEqual(['Jan 2015', 'Jan 2016', 'Jan 2017']); - expect(getTicksLabels(chart.scales.x2)).toEqual(['Jan 1985', 'Jan 1986', 'Jan 1987']); - expect(getTicksLabels(chart.scales.y)).toEqual(['Jan 1995', 'Jan 1996', 'Jan 1997']); - expect(getTicksLabels(chart.scales.y2)).toEqual(['Jan 2005', 'Jan 2006', 'Jan 2007']); + 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']); }); });