From: Evert Timberg Date: Mon, 18 May 2015 12:51:13 +0000 (-0400) Subject: Handle the beginAtZero option for a linear scale. Added a helper for Math.sign X-Git-Tag: v2.0-alpha~8^2~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2e383da0d806c638988de15e958720fdbb71f444;p=thirdparty%2FChart.js.git Handle the beginAtZero option for a linear scale. Added a helper for Math.sign --- diff --git a/src/Chart.Core.js b/src/Chart.Core.js index f60d7c2e7..e5bfd2af3 100755 --- a/src/Chart.Core.js +++ b/src/Chart.Core.js @@ -327,6 +327,17 @@ 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) { diff --git a/src/Chart.Scale.js b/src/Chart.Scale.js index 1f2ae8779..c991dfea4 100644 --- a/src/Chart.Scale.js +++ b/src/Chart.Scale.js @@ -348,7 +348,16 @@ // 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);