Skip to content

Commit f409a54

Browse files
andigsimonbrunel
authored andcommitted
Honour time scale min/max settings (#4522)
1 parent 4c763bf commit f409a54

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/helpers/helpers.time.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ moment = typeof moment === 'function' ? moment : window.moment;
55

66
var helpers = require('./helpers.core');
77

8+
var helpers = require('./helpers.core');
9+
810
var interval = {
911
millisecond: {
1012
size: 1,
@@ -62,18 +64,28 @@ function generateTicksNiceRange(options, dataRange, niceRange) {
6264
var stepValue = interval[options.unit].size * stepSize;
6365
var startFraction = startRange % stepValue;
6466
var alignedTick = startTick;
65-
if (startFraction && majorUnit && !options.timeOpts.round && !options.timeOpts.isoWeekday) {
67+
68+
// first tick
69+
if (startFraction && majorUnit && !options.timeOpts.round && !options.timeOpts.isoWeekday && helpers.isNullOrUndef(options.min)) {
6670
alignedTick += startFraction - stepValue;
6771
ticks.push(alignedTick);
6872
} else {
6973
ticks.push(startTick);
7074
}
75+
76+
// generate remaining ticks
7177
var cur = moment(alignedTick);
72-
var realMax = options.max || niceRange.max;
78+
var realMax = helpers.isNullOrUndef(options.max) ? niceRange.max : options.max;
7379
while (cur.add(stepSize, options.unit).valueOf() < realMax) {
7480
ticks.push(cur.valueOf());
7581
}
76-
ticks.push(cur.valueOf());
82+
83+
// last tick
84+
if (helpers.isNullOrUndef(options.max)) {
85+
ticks.push(cur.valueOf());
86+
} else {
87+
ticks.push(realMax);
88+
}
7789
}
7890
return ticks;
7991
}

test/specs/scale.time.tests.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ describe('Time scale tests', function() {
4949
});
5050
});
5151

52-
it('Should load moment.js as a dependency', function() {
52+
it('should load moment.js as a dependency', function() {
5353
expect(window.moment).not.toBe(undefined);
5454
});
5555

56-
it('Should register the constructor with the scale service', function() {
56+
it('should register the constructor with the scale service', function() {
5757
var Constructor = Chart.scaleService.getScaleConstructor('time');
5858
expect(Constructor).not.toBe(undefined);
5959
expect(typeof Constructor).toBe('function');
6060
});
6161

62-
it('Should have the correct default config', function() {
62+
it('should have the correct default config', function() {
6363
var defaultConfig = Chart.scaleService.getScaleDefaults('time');
6464
expect(defaultConfig).toEqual({
6565
display: true,
@@ -372,7 +372,7 @@ describe('Time scale tests', function() {
372372
config.time.min = '2014-12-29T04:00:00';
373373

374374
var scale = createScale(mockData, config);
375-
expect(scale.ticks[0].value).toEqual('Dec 28');
375+
expect(scale.ticks[0].value).toEqual('Dec 29');
376376
});
377377

378378
it('should use the max option', function() {
@@ -381,11 +381,11 @@ describe('Time scale tests', function() {
381381

382382
var scale = createScale(mockData, config);
383383

384-
expect(scale.ticks[scale.ticks.length - 1].value).toEqual('Jan 6');
384+
expect(scale.ticks[scale.ticks.length - 1].value).toEqual('Jan 5');
385385
});
386386
});
387387

388-
it('Should use the isoWeekday option', function() {
388+
it('should use the isoWeekday option', function() {
389389
var mockData = {
390390
labels: [
391391
'2015-01-01T20:00:00', // Thursday
@@ -478,7 +478,7 @@ describe('Time scale tests', function() {
478478
var step = xScale.ticks[1].time - xScale.ticks[0].time;
479479
var stepsAmount = Math.floor((xScale.max - xScale.min) / step);
480480

481-
it('should be bounded by nearest step year starts', function() {
481+
it('should be bounded by nearest step\'s year start and end', function() {
482482
expect(xScale.getValueForPixel(xScale.left)).toBeCloseToTime({
483483
value: moment(xScale.min).startOf('year'),
484484
unit: 'hour',

0 commit comments

Comments
 (0)