]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Update logarithmic algorithm
authorEvert Timberg <evert.timberg@gmail.com>
Wed, 16 Dec 2015 00:18:18 +0000 (19:18 -0500)
committerEvert Timberg <evert.timberg@gmail.com>
Wed, 16 Dec 2015 00:18:18 +0000 (19:18 -0500)
src/scales/scale.logarithmic.js

index 8694608bbd94ec181caa1f3f62b0e787a62fb6ee..0de930ae6a57d8394bce1b839df5942c387c5890 100644 (file)
 
                // 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 '';
                                }, 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) {
                        // 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
                        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();