Skip to content

Commit

Permalink
Honour time scale min/max settings (#4522)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored and simonbrunel committed Jul 22, 2017
1 parent 4c763bf commit e7445a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
16 changes: 13 additions & 3 deletions src/helpers/helpers.time.js
Expand Up @@ -62,18 +62,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;
}
Expand Down
14 changes: 7 additions & 7 deletions test/specs/scale.time.tests.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit e7445a5

Please sign in to comment.