From: Evert Timberg Date: Wed, 16 Dec 2015 00:18:18 +0000 (-0500) Subject: Update logarithmic algorithm X-Git-Tag: 2.0.0-beta2~19^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f51d7753bd3dea42e51487831c3b0f6f732460c1;p=thirdparty%2FChart.js.git Update logarithmic algorithm --- diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index 8694608bb..0de930ae6 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -10,10 +10,10 @@ // label settings ticks: { - callback: function(value) { + callback: function(value, index, arr) { var remain = value / (Math.pow(10, Math.floor(Chart.helpers.log10(value)))); - if (remain === 1 || remain === 2 || remain === 5) { + if (remain === 1 || remain === 2 || remain === 5 || index === 0 || index === arr.length - 1) { return value.toExponential(); } else { return ''; @@ -90,13 +90,8 @@ }, this); } - if (this.options.ticks.max) { - this.max = this.options.ticks.max; - } - - if (this.options.ticks.min) { - this.min = this.options.ticks.min; - } + this.min = this.options.ticks.min !== undefined ? this.options.ticks.min : this.min; + this.max = this.options.ticks.max !== undefined ? this.options.ticks.max : this.max; if (this.min === this.max) { if (this.min !== 0 && this.min !== null) { @@ -118,24 +113,37 @@ // We also limit the maximum number of ticks to 11 which gives a nice 10 squares on // the graph - var minExponent = Math.floor(helpers.log10(this.min)); - var maxExponent = Math.ceil(helpers.log10(this.max)); + var tickVal = this.options.ticks.min !== undefined ? this.options.ticks.min : Math.pow(10, Math.floor(helpers.log10(this.min))); - for (var exponent = minExponent; exponent < maxExponent; ++exponent) { - for (var i = 1; i < 10; ++i) { - this.tickValues.push(i * Math.pow(10, exponent)); + while (tickVal < this.max) { + this.tickValues.push(tickVal); + + var exp = Math.floor(helpers.log10(tickVal)); + var significand = Math.floor(tickVal / Math.pow(10, exp)) + 1; + + if (significand === 10) { + significand = 1; + ++exp; } + + tickVal = significand * Math.pow(10, exp); } - this.tickValues.push(1.0 * Math.pow(10, maxExponent)); + /*var minExponent = Math.floor(helpers.log10(this.min)); + var maxExponent = Math.ceil(helpers.log10(this.max)); - if (this.options.ticks.min) { - this.tickValues[0] = this.min; - } + for (var exponent = minExponent; exponent < maxExponent; ++exponent) { + for (var i = 1; i < 10; ++i) { + if (this.options.ticks.min) { - if (this.options.ticks.max) { - this.tickValues[this.tickValues.length - 1] = this.max; - } + } else { + this.tickValues.push(i * Math.pow(10, exponent)); + } + } + }*/ + + var lastTick = this.options.ticks.max !== undefined ? this.options.ticks.max : tickVal; + this.tickValues.push(lastTick); if (this.options.position == "left" || this.options.position == "right") { // We are in a vertical orientation. The top value is the highest. So reverse the array @@ -147,14 +155,6 @@ this.max = helpers.max(this.tickValues); this.min = helpers.min(this.tickValues); - if (this.options.ticks.min) { - this.min = this.options.ticks.min; - } - - if (this.options.ticks.max) { - this.max = this.options.ticks.max; - } - if (this.options.ticks.reverse) { this.tickValues.reverse();