mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
Honour time scale min/max settings (#4522)
This commit is contained in:
parent
4c763bff44
commit
e7445a5f00
@ -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;
|
||||
}
|
||||
|
||||
@ -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',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user