Handle the beginAtZero option for a linear scale. Added a helper for Math.sign

This commit is contained in:
Evert Timberg
2015-05-18 08:51:13 -04:00
parent e30a9d2a94
commit 2e383da0d8
2 changed files with 21 additions and 1 deletions

View File

@@ -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) {

View File

@@ -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);