Skip to content

Commit 2c8fe01

Browse files
kurklesimonbrunel
authored andcommitted
Allow specifying labels in time scale options (chartjs#6257)
1 parent 5cbf424 commit 2c8fe01

File tree

5 files changed

+64
-15
lines changed

5 files changed

+64
-15
lines changed

docs/axes/cartesian/time.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ The `ticks.source` property controls the ticks generation.
149149

150150
* `'auto'`: generates "optimal" ticks based on scale size and time options
151151
* `'data'`: generates ticks from data (including labels from data `{t|x|y}` objects)
152-
* `'labels'`: generates ticks from user given `data.labels` values ONLY
152+
* `'labels'`: generates ticks from user given `labels` ONLY
153153

154154
### Parser
155155
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.

src/core/core.scale.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,14 @@ var Scale = Element.extend({
214214
return this._ticks;
215215
},
216216

217+
/**
218+
* @private
219+
*/
220+
_getLabels: function() {
221+
var data = this.chart.data;
222+
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
223+
},
224+
217225
// These methods are ordered by lifecyle. Utilities then follow.
218226
// Any function defined here is inherited by all scale types.
219227
// Any function can be extended by the scale type

src/scales/scale.category.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,9 @@ var defaultConfig = {
77
};
88

99
module.exports = Scale.extend({
10-
/**
11-
* Internal function to get the correct labels. If data.xLabels or data.yLabels are defined, use those
12-
* else fall back to data.labels
13-
* @private
14-
*/
15-
getLabels: function() {
16-
var data = this.chart.data;
17-
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
18-
},
19-
2010
determineDataLimits: function() {
2111
var me = this;
22-
var labels = me.getLabels();
12+
var labels = me._getLabels();
2313
me.minIndex = 0;
2414
me.maxIndex = labels.length - 1;
2515
var findIndex;
@@ -42,7 +32,7 @@ module.exports = Scale.extend({
4232

4333
buildTicks: function() {
4434
var me = this;
45-
var labels = me.getLabels();
35+
var labels = me._getLabels();
4636
// If we are viewing some subset of labels, slice the original array
4737
me.ticks = (me.minIndex === 0 && me.maxIndex === labels.length - 1) ? labels : labels.slice(me.minIndex, me.maxIndex + 1);
4838
},
@@ -72,7 +62,7 @@ module.exports = Scale.extend({
7262
valueCategory = me.isHorizontal() ? value.x : value.y;
7363
}
7464
if (valueCategory !== undefined || (value !== undefined && isNaN(index))) {
75-
var labels = me.getLabels();
65+
var labels = me._getLabels();
7666
value = valueCategory || value;
7767
var idx = labels.indexOf(value);
7868
index = idx !== -1 ? idx : index;

src/scales/scale.time.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ module.exports = Scale.extend({
519519
var datasets = [];
520520
var labels = [];
521521
var i, j, ilen, jlen, data, timestamp;
522-
var dataLabels = chart.data.labels || [];
522+
var dataLabels = me._getLabels();
523523

524524
// Convert labels to timestamps
525525
for (i = 0, ilen = dataLabels.length; i < ilen; ++i) {

test/specs/scale.time.tests.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,57 @@ describe('Time scale tests', function() {
16821682
});
16831683
});
16841684

1685+
describe('labels', function() {
1686+
it('should read labels from scale / xLabels / yLabels', function() {
1687+
var timeOpts = {
1688+
parser: 'YYYY',
1689+
unit: 'year',
1690+
displayFormats: {
1691+
year: 'YYYY'
1692+
}
1693+
};
1694+
var chart = window.acquireChart({
1695+
type: 'line',
1696+
data: {
1697+
labels: ['1975', '1976', '1977'],
1698+
xLabels: ['1985', '1986', '1987'],
1699+
yLabels: ['1995', '1996', '1997']
1700+
},
1701+
options: {
1702+
scales: {
1703+
xAxes: [{
1704+
id: 'x',
1705+
type: 'time',
1706+
labels: ['2015', '2016', '2017'],
1707+
time: timeOpts
1708+
},
1709+
{
1710+
id: 'x2',
1711+
type: 'time',
1712+
time: timeOpts
1713+
}],
1714+
yAxes: [{
1715+
id: 'y',
1716+
type: 'time',
1717+
time: timeOpts
1718+
},
1719+
{
1720+
id: 'y2',
1721+
type: 'time',
1722+
labels: ['2005', '2006', '2007'],
1723+
time: timeOpts
1724+
}]
1725+
}
1726+
}
1727+
});
1728+
1729+
expect(getTicksLabels(chart.scales.x)).toEqual(['2015', '2016', '2017']);
1730+
expect(getTicksLabels(chart.scales.x2)).toEqual(['1985', '1986', '1987']);
1731+
expect(getTicksLabels(chart.scales.y)).toEqual(['1995', '1996', '1997']);
1732+
expect(getTicksLabels(chart.scales.y2)).toEqual(['2005', '2006', '2007']);
1733+
});
1734+
});
1735+
16851736
describe('Deprecations', function() {
16861737
describe('options.time.displayFormats', function() {
16871738
it('should generate defaults from adapter presets', function() {

0 commit comments

Comments
 (0)