]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix bug when calculating if steps fit into scale as a whole number then smal floating...
authorTarqwyn <steven@tarquindandy.com>
Tue, 6 Dec 2016 14:43:29 +0000 (14:43 +0000)
committerEvert Timberg <evert.timberg+github@gmail.com>
Wed, 14 Dec 2016 02:22:02 +0000 (21:22 -0500)
src/core/core.helpers.js
src/core/core.ticks.js
test/core.helpers.tests.js

index 506ad900397f916409387eb4ec24e1bafe6788d2..bfbcf23d5f5b2806ca77cc4c943e25894b19773f 100644 (file)
@@ -237,6 +237,10 @@ module.exports = function(Chart) {
        helpers.almostEquals = function(x, y, epsilon) {
                return Math.abs(x - y) < epsilon;
        };
+       helpers.almostWhole = function(x, epsilon) {
+               var rounded = Math.round(x);
+               return (((rounded - epsilon) < x) && ((rounded + epsilon) > x));
+       };
        helpers.max = function(array) {
                return array.reduce(function(max, value) {
                        if (!isNaN(value)) {
index 72441f3a5537246357904b6c4e8ac0a95b297077..8d84f1f236a7c6c0e1c3ea73692213270b9452e6 100644 (file)
@@ -67,8 +67,8 @@ module.exports = function(Chart) {
 
                                // If min, max and stepSize is set and they make an evenly spaced scale use it.
                                if (generationOptions.min && generationOptions.max && generationOptions.stepSize) {
-                                       var minMaxDeltaDivisibleByStepSize = ((generationOptions.max - generationOptions.min) % generationOptions.stepSize) === 0;
-                                       if (minMaxDeltaDivisibleByStepSize) {
+                                       // If very close to our whole number, use it.
+                                       if (helpers.almostWhole((generationOptions.max - generationOptions.min) / generationOptions.stepSize, spacing / 1000)) {
                                                niceMin = generationOptions.min;
                                                niceMax = generationOptions.max;
                                        }
index 75e0baf900ec88d3dc6e5d3ff65f18ded4475308..296958051d6261adb614fb7d11510460af3fb5d5 100644 (file)
@@ -301,6 +301,11 @@ describe('Core helper tests', function() {
                expect(helpers.almostEquals(1e30, 1e30 + Number.EPSILON, 2 * Number.EPSILON)).toBe(true);
        });
 
+       it('should correctly determine if a numbers are essentially whole', function() {
+               expect(helpers.almostWhole(0.99999, 0.0001)).toBe(true);
+               expect(helpers.almostWhole(0.9, 0.0001)).toBe(false);
+       });
+
        it('should generate integer ids', function() {
                var uid = helpers.uid();
                expect(uid).toEqual(jasmine.any(Number));