From: Ben McCann Date: Fri, 20 Oct 2017 07:03:38 +0000 (-0700) Subject: Respect min and max when building ticks (#4860) X-Git-Tag: v2.7.1~1^2~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b64fd5db2526b33b5da6b8182f50bba8042b82af;p=thirdparty%2FChart.js.git Respect min and max when building ticks (#4860) 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. --- diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 27a340a3b..4fe39b810 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -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; diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 98dd774b6..19189040e 100755 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -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'); }); });