]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Respect min and max when building ticks (#4860)
authorBen McCann <benjamin.j.mccann@gmail.com>
Fri, 20 Oct 2017 07:03:38 +0000 (00:03 -0700)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Fri, 20 Oct 2017 07:03:38 +0000 (09:03 +0200)
Generate time scale ticks (`ticks.source: 'auto'`) based on the effective visualized range instead of the actual data range, meaning that the computed units and/or step size may change if the time options min and max are different from the data min and max.

src/scales/scale.time.js
test/specs/scale.time.tests.js

index 27a340a3b1f02b5b2d9176af8022d0c103acdb54..4fe39b810828edca15d8daedf6bb5fc804a3c964 100644 (file)
@@ -504,8 +504,8 @@ module.exports = function(Chart) {
                        var me = this;
                        var chart = me.chart;
                        var timeOpts = me.options.time;
-                       var min = parse(timeOpts.min, me) || MAX_INTEGER;
-                       var max = parse(timeOpts.max, me) || MIN_INTEGER;
+                       var min = MAX_INTEGER;
+                       var max = MIN_INTEGER;
                        var timestamps = [];
                        var datasets = [];
                        var labels = [];
@@ -552,6 +552,9 @@ module.exports = function(Chart) {
                                max = Math.max(max, timestamps[timestamps.length - 1]);
                        }
 
+                       min = parse(timeOpts.min, me) || min;
+                       max = parse(timeOpts.max, me) || max;
+
                        // In case there is no valid min/max, let's use today limits
                        min = min === MAX_INTEGER ? +moment().startOf('day') : min;
                        max = max === MIN_INTEGER ? +moment().endOf('day') + 1 : max;
index 98dd774b6bee30b576903e2ebef6cdc286ecd9d6..19189040ee453d9d62d06c46d56273c0d8004ef6 100755 (executable)
@@ -376,23 +376,36 @@ describe('Time scale tests', function() {
                var config;
                beforeEach(function() {
                        config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('time'));
+                       config.ticks.source = 'labels';
+                       config.time.unit = 'day';
                });
 
-               it('should use the min option', function() {
-                       config.time.unit = 'day';
+               it('should use the min option when less than first label for building ticks', function() {
                        config.time.min = '2014-12-29T04:00:00';
 
                        var scale = createScale(mockData, config);
-                       expect(scale.ticks[0]).toEqual('Dec 31');
+                       expect(scale.ticks[0]).toEqual('Jan 1');
                });
 
-               it('should use the max option', function() {
-                       config.time.unit = 'day';
+               it('should use the min option when greater than first label for building ticks', function() {
+                       config.time.min = '2015-01-02T04:00:00';
+
+                       var scale = createScale(mockData, config);
+                       expect(scale.ticks[0]).toEqual('Jan 2');
+               });
+
+               it('should use the max option when greater than last label for building ticks', function() {
                        config.time.max = '2015-01-05T06:00:00';
 
                        var scale = createScale(mockData, config);
+                       expect(scale.ticks[scale.ticks.length - 1]).toEqual('Jan 3');
+               });
 
-                       expect(scale.ticks[scale.ticks.length - 1]).toEqual('Jan 5');
+               it('should use the max option when less than last label for building ticks', function() {
+                       config.time.max = '2015-01-02T23:00:00';
+
+                       var scale = createScale(mockData, config);
+                       expect(scale.ticks[scale.ticks.length - 1]).toEqual('Jan 2');
                });
        });