]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Handle the beginAtZero option for a linear scale. Added a helper for Math.sign
authorEvert Timberg <evert.timberg@gmail.com>
Mon, 18 May 2015 12:51:13 +0000 (08:51 -0400)
committerEvert Timberg <evert.timberg@gmail.com>
Mon, 18 May 2015 12:51:13 +0000 (08:51 -0400)
src/Chart.Core.js
src/Chart.Scale.js

index f60d7c2e7e07672bda6ddcbbf906f5179bd755d2..e5bfd2af3373f852fb181ea70634b2a2e685f661 100755 (executable)
         min = helpers.min = function(array) {
             return Math.min.apply(Math, array);
         },
+               sign = helpers.sign = function(x) {
+                       if (Math.sign) {
+                               return Math.sign(x);
+                       } else {
+                               x = +x; // convert to a number
+                               if (x === 0 || isNaN(x)) {
+                                       return x;
+                               }
+                               return x > 0 ? 1 : -1;
+                       }
+               },
         cap = helpers.cap = function(valueToCap, maxValue, minValue) {
             if (isNumber(maxValue)) {
                 if (valueToCap > maxValue) {
index 1f2ae87793fe291037934dde47e57bf7350ee91e..c991dfea491c6ce488f053aeec43307f97cf0af1 100644 (file)
                                // do nothing since that would make the chart weird. If the user really wants a weird chart
                                // axis, they can manually override it
                                if (this.options.beginAtZero) {
-                                       this.min = Math.min(this.min, 0);
+                                       var minSign = helpers.sign(this.min);
+                                       var maxSign = helpers.sign(this.max);
+                                       
+                                       if (minSign < 0 && maxSign < 0) {
+                                               // move the top up to 0
+                                               this.max = 0;
+                                       } else if (minSign > 0 && maxSign > 0) {
+                                               // move the botttom down to 0
+                                               this.min = 0;
+                                       }
                                }
                                
                                var niceRange = helpers.niceNum(this.max - this.min, false);