]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Slight improvement to new min/max options so that linear scales look right
authorEvert Timberg <evert.timberg@gmail.com>
Tue, 15 Dec 2015 23:58:05 +0000 (18:58 -0500)
committerEvert Timberg <evert.timberg@gmail.com>
Tue, 15 Dec 2015 23:58:05 +0000 (18:58 -0500)
src/scales/scale.linear.js

index 18e7301fc4fac615d2dba2a1f18204ec9eb352aa..e21d10225571618ce545cc1a90a247b428316924 100644 (file)
 
                        if (this.options.ticks.suggestedMin) {
                                this.min = Math.min(this.min, this.options.ticks.suggestedMin);
+                       } else if (this.options.ticks.min !== undefined) {
+                               this.min = this.options.ticks.min;
                        }
 
                        if (this.options.ticks.suggestedMax) {
                                this.max = Math.max(this.max, this.options.ticks.suggestedMax);
+                       } else if (this.options.ticks.max !== undefined) {
+                               this.max = this.options.ticks.max;
                        }
 
                        if (this.min === this.max) {
                        var numSpaces = Math.ceil((niceMax - niceMin) / spacing);
 
                        // Put the values into the ticks array
-                       for (var j = 0; j <= numSpaces; ++j) {
+                       this.ticks.push(this.options.ticks.min !== undefined ? this.options.ticks.min : niceMin);
+                       for (var j = 1; j < numSpaces; ++j) {
                                this.ticks.push(niceMin + (j * spacing));
                        }
-
-                       if (this.options.ticks.min !== undefined) {
-                               this.ticks[0] = this.options.ticks.min;
-                       }
-
-                       if (this.options.ticks.max !== undefined) {
-                               this.ticks[this.ticks.length - 1] = this.options.ticks.max;
-                       }
+                       this.ticks.push(this.options.ticks.max !== undefined ? this.options.ticks.max : niceMax);
 
                        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
                        // range of the scale
                        this.max = helpers.max(this.ticks);
                        this.min = helpers.min(this.ticks);
-
-                       if (this.options.ticks.min !== undefined) {
-                               this.min = this.options.ticks.min;
-                       }
-
-                       if (this.options.ticks.max !== undefined) {
-                               this.max = this.options.ticks.max;
-                       }
-
+                       this.ticksAsNumbers = this.ticks.slice();
 
                        if (this.options.ticks.reverse) {
                                this.ticks.reverse();
                        var range = this.end - this.start;
 
                        if (this.isHorizontal()) {
-
                                var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
                                pixel = this.left + (innerWidth / range * (rightValue - this.start));
                                return Math.round(pixel + this.paddingLeft);
                                return Math.round(pixel);
                        }
                },
+               getPixelForTick: function(index, includeOffset) {
+                       return this.getPixelForValue(this.ticksAsNumbers[index], null, null, includeOffset);
+               },
        });
        Chart.scaleService.registerScaleType("linear", LinearScale, defaultConfig);