]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix infinite loop in logarithmic tick generation
authoretimberg <evert.timberg@gmail.com>
Sun, 4 Dec 2016 03:21:48 +0000 (22:21 -0500)
committerEvert Timberg <evert.timberg+github@gmail.com>
Wed, 7 Dec 2016 23:47:30 +0000 (18:47 -0500)
src/core/core.ticks.js
test/scale.logarithmic.tests.js

index f11f1192bdfe6dac28bee3c8805571d30e8887b9..72441f3a5537246357904b6c4e8ac0a95b297077 100644 (file)
@@ -109,27 +109,33 @@ module.exports = function(Chart) {
                                // the graph
                                var tickVal = getValueOrDefault(generationOptions.min, Math.pow(10, Math.floor(helpers.log10(dataRange.min))));
 
-                               while (tickVal < dataRange.max) {
-                                       ticks.push(tickVal);
+                               var endExp = Math.floor(helpers.log10(dataRange.max));
+                               var endSignificand = Math.ceil(dataRange.max / Math.pow(10, endExp));
+                               var exp;
+                               var significand;
 
-                                       var exp;
-                                       var significand;
+                               if (tickVal === 0) {
+                                       exp = Math.floor(helpers.log10(dataRange.minNotZero));
+                                       significand = Math.floor(dataRange.minNotZero / Math.pow(10, exp));
 
-                                       if (tickVal === 0) {
-                                               exp = Math.floor(helpers.log10(dataRange.minNotZero));
-                                               significand = Math.round(dataRange.minNotZero / Math.pow(10, exp));
-                                       } else {
-                                               exp = Math.floor(helpers.log10(tickVal));
-                                               significand = Math.floor(tickVal / Math.pow(10, exp)) + 1;
-                                       }
+                                       ticks.push(tickVal);
+                                       tickVal = significand * Math.pow(10, exp);
+                               } else {
+                                       exp = Math.floor(helpers.log10(tickVal));
+                                       significand = Math.floor(tickVal / Math.pow(10, exp));
+                               }
+
+                               do {
+                                       ticks.push(tickVal);
 
+                                       ++significand;
                                        if (significand === 10) {
                                                significand = 1;
                                                ++exp;
                                        }
 
                                        tickVal = significand * Math.pow(10, exp);
-                               }
+                               } while (exp < endExp || (exp === endExp && significand < endSignificand));
 
                                var lastTick = getValueOrDefault(generationOptions.max, tickVal);
                                ticks.push(lastTick);
index 64732d833b56e8260e31ae1ba2b03b61c3f574bf..f5ee6eae994f0821887f1956185749b819e6f6d1 100644 (file)
@@ -343,7 +343,7 @@ describe('Logarithmic Scale tests', function() {
                                        data: [10, 5, 1, 5, 78, 100]
                                }, {
                                        yAxisID: 'yScale1',
-                                       data: [-1000, 1000],
+                                       data: [0, 1000],
                                }, {
                                        type: 'bar',
                                        yAxisID: 'yScale0',
@@ -383,7 +383,7 @@ describe('Logarithmic Scale tests', function() {
                                        type: 'bar'
                                }, {
                                        yAxisID: 'yScale1',
-                                       data: [-1000, 1000],
+                                       data: [0, 1000],
                                        type: 'bar'
                                }, {
                                        yAxisID: 'yScale0',