]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Honour time scale min/max settings (#4522)
authorandig <cpuidle@gmx.de>
Sat, 22 Jul 2017 12:22:44 +0000 (14:22 +0200)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Sat, 22 Jul 2017 12:35:06 +0000 (14:35 +0200)
src/helpers/helpers.time.js
test/specs/scale.time.tests.js

index 404708ba7c4c2061872dcf787949c342f7477ad6..5bbcf3eaed0e98f3bd1f4fd231ed01e487521cf3 100644 (file)
@@ -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;
 }
index 057521ab406ec3c0c126599ea9e2d4b71c6ce850..590921101c63d364b0ee734ea8ef3422658d4ddc 100755 (executable)
@@ -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',