]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Use the appropriate time format for auto tick generation (#6182)
authorAkihiko Kusanagi <nagi@nagi-p.com>
Fri, 26 Apr 2019 07:43:36 +0000 (15:43 +0800)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Fri, 26 Apr 2019 07:43:36 +0000 (09:43 +0200)
src/scales/scale.time.js
test/specs/scale.time.tests.js

index eebacacbff660294d14a8970fb8e782102cdbeea..dd202c70f20aace5d9a34200063353893f16efd2 100644 (file)
@@ -774,9 +774,12 @@ module.exports = Scale.extend({
         */
        getLabelCapacity: function(exampleTime) {
                var me = this;
+               var timeOpts = me.options.time;
+               var displayFormats = timeOpts.displayFormats;
 
                // pick the longest format (milliseconds) for guestimation
-               var format = me.options.time.displayFormats.millisecond;
+               var format = displayFormats[timeOpts.unit] || displayFormats.millisecond;
+
                var exampleLabel = me.tickFormatFunction(exampleTime, 0, [], format);
                var tickLabelWidth = me.getLabelWidth(exampleLabel);
                var innerWidth = me.isHorizontal() ? me.width : me.height;
index d67b60cbb1202a512c3256162b56a0a33dba1da1..77b3cbf2b66379150ea9420b991d2d3270ff5955 100755 (executable)
@@ -298,7 +298,7 @@ describe('Time scale tests', function() {
                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']);
        });
 
-       it('build ticks honoring the minUnit', function() {
+       it('should build ticks honoring the minUnit', function() {
                var mockData = {
                        labels: ['2015-01-01T20:00:00', '2015-01-02T21:00:00'], // days
                };
@@ -316,6 +316,26 @@ describe('Time scale tests', function() {
                expect(ticks).toEqual(['Jan 1', 'Jan 2', 'Jan 3']);
        });
 
+       it('should build ticks based on the appropriate label capacity', function() {
+               var mockData = {
+                       labels: [
+                               '2012-01-01', '2013-01-01', '2014-01-01', '2015-01-01',
+                               '2016-01-01', '2017-01-01', '2018-01-01', '2019-01-01'
+                       ]
+               };
+
+               var config = Chart.helpers.mergeIf({
+                       time: {
+                               unit: 'year'
+                       }
+               }, Chart.scaleService.getScaleDefaults('time'));
+
+               var scale = createScale(mockData, config);
+               var ticks = getTicksLabels(scale);
+
+               expect(ticks).toEqual(['2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019']);
+       });
+
        it('should build ticks using the config diff', function() {
                var mockData = {
                        labels: ['2015-01-01T20:00:00', '2015-02-02T21:00:00', '2015-02-21T01:00:00'], // days