Skip to content

Commit 084a99a

Browse files
nagixsimonbrunel
authored andcommitted
Use the appropriate time format for auto tick generation (chartjs#6182)
1 parent 4886922 commit 084a99a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/scales/scale.time.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,9 +774,12 @@ module.exports = Scale.extend({
774774
*/
775775
getLabelCapacity: function(exampleTime) {
776776
var me = this;
777+
var timeOpts = me.options.time;
778+
var displayFormats = timeOpts.displayFormats;
777779

778780
// pick the longest format (milliseconds) for guestimation
779-
var format = me.options.time.displayFormats.millisecond;
781+
var format = displayFormats[timeOpts.unit] || displayFormats.millisecond;
782+
780783
var exampleLabel = me.tickFormatFunction(exampleTime, 0, [], format);
781784
var tickLabelWidth = me.getLabelWidth(exampleLabel);
782785
var innerWidth = me.isHorizontal() ? me.width : me.height;

test/specs/scale.time.tests.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ describe('Time scale tests', function() {
298298
expect(ticks).toEqual(['8PM', '9PM', '10PM', '11PM', '12AM', '1AM', '2AM', '3AM', '4AM', '5AM', '6AM', '7AM', '8AM', '9AM', '10AM', '11AM', '12PM', '1PM', '2PM', '3PM', '4PM', '5PM', '6PM', '7PM', '8PM', '9PM']);
299299
});
300300

301-
it('build ticks honoring the minUnit', function() {
301+
it('should build ticks honoring the minUnit', function() {
302302
var mockData = {
303303
labels: ['2015-01-01T20:00:00', '2015-01-02T21:00:00'], // days
304304
};
@@ -316,6 +316,26 @@ describe('Time scale tests', function() {
316316
expect(ticks).toEqual(['Jan 1', 'Jan 2', 'Jan 3']);
317317
});
318318

319+
it('should build ticks based on the appropriate label capacity', function() {
320+
var mockData = {
321+
labels: [
322+
'2012-01-01', '2013-01-01', '2014-01-01', '2015-01-01',
323+
'2016-01-01', '2017-01-01', '2018-01-01', '2019-01-01'
324+
]
325+
};
326+
327+
var config = Chart.helpers.mergeIf({
328+
time: {
329+
unit: 'year'
330+
}
331+
}, Chart.scaleService.getScaleDefaults('time'));
332+
333+
var scale = createScale(mockData, config);
334+
var ticks = getTicksLabels(scale);
335+
336+
expect(ticks).toEqual(['2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019']);
337+
});
338+
319339
it('should build ticks using the config diff', function() {
320340
var mockData = {
321341
labels: ['2015-01-01T20:00:00', '2015-02-02T21:00:00', '2015-02-21T01:00:00'], // days

0 commit comments

Comments
 (0)