]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Time scale now compensates for rounded tick units 2371/head
authorTanner Linsley <tannerlinsley@gmail.com>
Mon, 25 Apr 2016 22:48:21 +0000 (17:48 -0500)
committerTanner Linsley <tannerlinsley@gmail.com>
Mon, 25 Apr 2016 22:48:21 +0000 (17:48 -0500)
Closes #2277

src/scales/scale.time.js
test/scale.time.tests.js

index 45ce64b43d707d06b1db135b421feb578aaa4d96..620eac88c0a9d263221be42525479e6219261aae 100644 (file)
@@ -205,7 +205,8 @@ module.exports = function(Chart) {
                                                unitDefinition = time.units[unitDefinitionIndex];
 
                                                this.tickUnit = unitDefinition.name;
-                                               this.scaleSizeInUnits = this.lastTick.diff(this.firstTick, this.tickUnit, true);
+                                               this.leadingUnitBuffer = this.firstTick.diff(this.firstTick.clone().startOf(this.tickUnit), this.tickUnit, true);
+                                               this.scaleSizeInUnits = this.lastTick.diff(this.firstTick, this.tickUnit, true) + (this.leadingUnitBuffer > 0 ? 2 : 0);
                                                this.displayFormat = this.options.time.displayFormats[unitDefinition.name];
                                        }
                                }
@@ -305,7 +306,7 @@ module.exports = function(Chart) {
                        if (labelMoment) {
                                var offset = labelMoment.diff(this.firstTick, this.tickUnit, true);
 
-                               var decimal = offset / this.scaleSizeInUnits;
+                               var decimal = offset / (this.scaleSizeInUnits - (this.leadingUnitBuffer > 0 ? 1 : 0));
 
                                if (this.isHorizontal()) {
                                        var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
index c35b4cea32b6b6d0b3447b5c5bb390e6191aafc9..b123b16a105843b3c53e9da2713040e8d5c42c54 100644 (file)
@@ -109,7 +109,7 @@ describe('Time scale tests', function() {
                scale.update(400, 50);
 
                // Counts down because the lines are drawn top to bottom
-               expect(scale.ticks).toEqual([ 'Jan 1, 2015', 'Jan 6, 2015', 'Jan 11, 2015' ]);
+               expect(scale.ticks).toEqual([ 'Dec 28, 2014', 'Jan 11, 2015' ]);
        });
 
        it('should build ticks using date objects', function() {
@@ -137,7 +137,7 @@ describe('Time scale tests', function() {
                scale.update(400, 50);
 
                // Counts down because the lines are drawn top to bottom
-               expect(scale.ticks).toEqual([ 'Jan 1, 2015', 'Jan 6, 2015', 'Jan 11, 2015' ]);
+               expect(scale.ticks).toEqual([ 'Dec 28, 2014', 'Jan 11, 2015' ]);
        });
 
        it('should build ticks when the data is xy points', function() {
@@ -170,7 +170,7 @@ describe('Time scale tests', function() {
                                }, {
                                        x: newDateFromRef(9),
                                        y: 5
-                               }], // days
+                               }] // days
                        }]
                };
 
@@ -188,7 +188,7 @@ describe('Time scale tests', function() {
                scale.update(400, 50);
 
                // Counts down because the lines are drawn top to bottom
-               expect(scale.ticks).toEqual([ 'Jan 1, 2015', 'Jan 6, 2015', 'Jan 11, 2015' ]);
+               expect(scale.ticks).toEqual([ 'Dec 28, 2014', 'Jan 11, 2015' ]);
        });
 
        it('should allow custom time parsers', function() {
@@ -201,7 +201,7 @@ describe('Time scale tests', function() {
                                data: [{
                                        x: 375068900,
                                        y: 1
-                               }],
+                               }]
                        }]
                };
                var verticalScaleConfig = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('time'));
@@ -303,7 +303,7 @@ describe('Time scale tests', function() {
                });
 
                scale.update(400, 50);
-               expect(scale.ticks).toEqual([ 'Jan 1, 2015', 'Jan 3, 2015', 'Jan 5, 2015' ]);
+               expect(scale.ticks).toEqual([ 'Jan 1, 2015', 'Jan 5, 2015' ]);
        });
 
        it('should get the correct pixel for a value', function() {
@@ -336,8 +336,8 @@ describe('Time scale tests', function() {
                scale.top = 10;
                scale.bottom = 38;
 
-               expect(scale.getPixelForValue('', 0, 0)).toBe(81);
-               expect(scale.getPixelForValue('', 6, 0)).toBe(323);
+               expect(scale.getPixelForValue('', 0, 0)).toBe(148);
+               expect(scale.getPixelForValue('', 6, 0)).toBe(299);
 
                var verticalScaleConfig = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('time'));
                verticalScaleConfig.position = "left";
@@ -358,8 +358,8 @@ describe('Time scale tests', function() {
                verticalScale.right = 50;
                verticalScale.bottom = 400;
 
-               expect(verticalScale.getPixelForValue('', 0, 0)).toBe(38);
-               expect(verticalScale.getPixelForValue('', 6, 0)).toBe(375);
+               expect(verticalScale.getPixelForValue('', 0, 0)).toBe(126);
+               expect(verticalScale.getPixelForValue('', 6, 0)).toBe(340);
        });
 
        it('should get the correct label for a data value', function() {