From f409a544b73959f87cd58a314e58a041ce01c0bd Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 22 Jul 2017 14:22:44 +0200 Subject: [PATCH] Honour time scale min/max settings (#4522) --- src/helpers/helpers.time.js | 18 +++++++++++++++--- test/specs/scale.time.tests.js | 14 +++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/helpers/helpers.time.js b/src/helpers/helpers.time.js index 404708ba7c4..c28de15232e 100644 --- a/src/helpers/helpers.time.js +++ b/src/helpers/helpers.time.js @@ -5,6 +5,8 @@ moment = typeof moment === 'function' ? moment : window.moment; var helpers = require('./helpers.core'); +var helpers = require('./helpers.core'); + var interval = { millisecond: { size: 1, @@ -62,18 +64,28 @@ function generateTicksNiceRange(options, dataRange, niceRange) { var stepValue = interval[options.unit].size * stepSize; var startFraction = startRange % stepValue; var alignedTick = startTick; - if (startFraction && majorUnit && !options.timeOpts.round && !options.timeOpts.isoWeekday) { + + // first tick + if (startFraction && majorUnit && !options.timeOpts.round && !options.timeOpts.isoWeekday && helpers.isNullOrUndef(options.min)) { alignedTick += startFraction - stepValue; ticks.push(alignedTick); } else { ticks.push(startTick); } + + // generate remaining ticks var cur = moment(alignedTick); - var realMax = options.max || niceRange.max; + var realMax = helpers.isNullOrUndef(options.max) ? niceRange.max : options.max; while (cur.add(stepSize, options.unit).valueOf() < realMax) { ticks.push(cur.valueOf()); } - ticks.push(cur.valueOf()); + + // last tick + if (helpers.isNullOrUndef(options.max)) { + ticks.push(cur.valueOf()); + } else { + ticks.push(realMax); + } } return ticks; } diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 057521ab406..590921101c6 100755 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -49,17 +49,17 @@ describe('Time scale tests', function() { }); }); - it('Should load moment.js as a dependency', function() { + it('should load moment.js as a dependency', function() { expect(window.moment).not.toBe(undefined); }); - it('Should register the constructor with the scale service', function() { + it('should register the constructor with the scale service', function() { var Constructor = Chart.scaleService.getScaleConstructor('time'); expect(Constructor).not.toBe(undefined); expect(typeof Constructor).toBe('function'); }); - it('Should have the correct default config', function() { + it('should have the correct default config', function() { var defaultConfig = Chart.scaleService.getScaleDefaults('time'); expect(defaultConfig).toEqual({ display: true, @@ -372,7 +372,7 @@ describe('Time scale tests', function() { config.time.min = '2014-12-29T04:00:00'; var scale = createScale(mockData, config); - expect(scale.ticks[0].value).toEqual('Dec 28'); + expect(scale.ticks[0].value).toEqual('Dec 29'); }); it('should use the max option', function() { @@ -381,11 +381,11 @@ describe('Time scale tests', function() { var scale = createScale(mockData, config); - expect(scale.ticks[scale.ticks.length - 1].value).toEqual('Jan 6'); + expect(scale.ticks[scale.ticks.length - 1].value).toEqual('Jan 5'); }); }); - it('Should use the isoWeekday option', function() { + it('should use the isoWeekday option', function() { var mockData = { labels: [ '2015-01-01T20:00:00', // Thursday @@ -478,7 +478,7 @@ describe('Time scale tests', function() { var step = xScale.ticks[1].time - xScale.ticks[0].time; var stepsAmount = Math.floor((xScale.max - xScale.min) / step); - it('should be bounded by nearest step year starts', function() { + it('should be bounded by nearest step\'s year start and end', function() { expect(xScale.getValueForPixel(xScale.left)).toBeCloseToTime({ value: moment(xScale.min).startOf('year'), unit: 'hour',