Skip to content

Commit b64fd5d

Browse files
benmccannsimonbrunel
authored andcommitted
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.
1 parent 11315fb commit b64fd5d

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/scales/scale.time.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ module.exports = function(Chart) {
504504
var me = this;
505505
var chart = me.chart;
506506
var timeOpts = me.options.time;
507-
var min = parse(timeOpts.min, me) || MAX_INTEGER;
508-
var max = parse(timeOpts.max, me) || MIN_INTEGER;
507+
var min = MAX_INTEGER;
508+
var max = MIN_INTEGER;
509509
var timestamps = [];
510510
var datasets = [];
511511
var labels = [];
@@ -552,6 +552,9 @@ module.exports = function(Chart) {
552552
max = Math.max(max, timestamps[timestamps.length - 1]);
553553
}
554554

555+
min = parse(timeOpts.min, me) || min;
556+
max = parse(timeOpts.max, me) || max;
557+
555558
// In case there is no valid min/max, let's use today limits
556559
min = min === MAX_INTEGER ? +moment().startOf('day') : min;
557560
max = max === MIN_INTEGER ? +moment().endOf('day') + 1 : max;

test/specs/scale.time.tests.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,23 +376,36 @@ describe('Time scale tests', function() {
376376
var config;
377377
beforeEach(function() {
378378
config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('time'));
379+
config.ticks.source = 'labels';
380+
config.time.unit = 'day';
379381
});
380382

381-
it('should use the min option', function() {
382-
config.time.unit = 'day';
383+
it('should use the min option when less than first label for building ticks', function() {
383384
config.time.min = '2014-12-29T04:00:00';
384385

385386
var scale = createScale(mockData, config);
386-
expect(scale.ticks[0]).toEqual('Dec 31');
387+
expect(scale.ticks[0]).toEqual('Jan 1');
387388
});
388389

389-
it('should use the max option', function() {
390-
config.time.unit = 'day';
390+
it('should use the min option when greater than first label for building ticks', function() {
391+
config.time.min = '2015-01-02T04:00:00';
392+
393+
var scale = createScale(mockData, config);
394+
expect(scale.ticks[0]).toEqual('Jan 2');
395+
});
396+
397+
it('should use the max option when greater than last label for building ticks', function() {
391398
config.time.max = '2015-01-05T06:00:00';
392399

393400
var scale = createScale(mockData, config);
401+
expect(scale.ticks[scale.ticks.length - 1]).toEqual('Jan 3');
402+
});
394403

395-
expect(scale.ticks[scale.ticks.length - 1]).toEqual('Jan 5');
404+
it('should use the max option when less than last label for building ticks', function() {
405+
config.time.max = '2015-01-02T23:00:00';
406+
407+
var scale = createScale(mockData, config);
408+
expect(scale.ticks[scale.ticks.length - 1]).toEqual('Jan 2');
396409
});
397410
});
398411

0 commit comments

Comments
 (0)