Merge pull request #1787 from nnnick/fix/min-max

Updates new min/max settings
This commit is contained in:
Evert Timberg 2015-12-15 19:35:46 -05:00
commit 3ccf3fd249
4 changed files with 127 additions and 135 deletions

View File

@ -158,10 +158,14 @@
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) {
@ -177,17 +181,11 @@
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
@ -198,15 +196,7 @@
// 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();
@ -234,7 +224,6 @@
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);
@ -244,6 +233,9 @@
return Math.round(pixel);
}
},
getPixelForTick: function(index, includeOffset) {
return this.getPixelForValue(this.ticksAsNumbers[index], null, null, includeOffset);
},
});
Chart.scaleService.registerScaleType("linear", LinearScale, defaultConfig);

View File

@ -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 tickVal = this.options.ticks.min !== undefined ? this.options.ticks.min : Math.pow(10, Math.floor(helpers.log10(this.min)));
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);
}
/*var minExponent = Math.floor(helpers.log10(this.min));
var maxExponent = Math.ceil(helpers.log10(this.max));
for (var exponent = minExponent; exponent < maxExponent; ++exponent) {
for (var i = 1; i < 10; ++i) {
this.tickValues.push(i * Math.pow(10, exponent));
if (this.options.ticks.min) {
} else {
this.tickValues.push(i * Math.pow(10, exponent));
}
}
}
}*/
this.tickValues.push(1.0 * Math.pow(10, maxExponent));
if (this.options.ticks.min) {
this.tickValues[0] = this.min;
}
if (this.options.ticks.max) {
this.tickValues[this.tickValues.length - 1] = this.max;
}
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();

View File

@ -406,7 +406,7 @@ describe('Linear Scale', function() {
expect(scale.min).toBe(-1010);
expect(scale.max).toBe(1010);
expect(scale.ticks[0]).toBe(1010);
expect(scale.ticks[scale.ticks.length-1]).toBe(-1010);
expect(scale.ticks[scale.ticks.length - 1]).toBe(-1010);
});
it('should forcibly include 0 in the range if the beginAtZero option is used', function() {
@ -1127,16 +1127,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 30.2]
"args": [25, 30.5]
}, {
"name": "lineTo",
"args": [30, 30.2]
"args": [30, 30.5]
}, {
"name": "moveTo",
"args": [30, 30.2]
"args": [30, 30.5]
}, {
"name": "lineTo",
"args": [130, 30.2]
"args": [130, 30.5]
}, {
"name": "stroke",
"args": []
@ -1145,7 +1145,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 29.7]
"args": [20, 30]
}, {
"name": "rotate",
"args": [-0]
@ -1160,16 +1160,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 59.9]
"args": [25, 59.5]
}, {
"name": "lineTo",
"args": [30, 59.9]
"args": [30, 59.5]
}, {
"name": "moveTo",
"args": [30, 59.9]
"args": [30, 59.5]
}, {
"name": "lineTo",
"args": [130, 59.9]
"args": [130, 59.5]
}, {
"name": "stroke",
"args": []
@ -1178,7 +1178,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 59.4]
"args": [20, 59]
}, {
"name": "rotate",
"args": [-0]
@ -1193,16 +1193,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 89.6]
"args": [25, 89.5]
}, {
"name": "lineTo",
"args": [30, 89.6]
"args": [30, 89.5]
}, {
"name": "moveTo",
"args": [30, 89.6]
"args": [30, 89.5]
}, {
"name": "lineTo",
"args": [130, 89.6]
"args": [130, 89.5]
}, {
"name": "stroke",
"args": []
@ -1211,7 +1211,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 89.1]
"args": [20, 89]
}, {
"name": "rotate",
"args": [-0]
@ -1226,16 +1226,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 119.3]
"args": [25, 119.5]
}, {
"name": "lineTo",
"args": [30, 119.3]
"args": [30, 119.5]
}, {
"name": "moveTo",
"args": [30, 119.3]
"args": [30, 119.5]
}, {
"name": "lineTo",
"args": [130, 119.3]
"args": [130, 119.5]
}, {
"name": "stroke",
"args": []
@ -1244,7 +1244,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 118.8]
"args": [20, 119]
}, {
"name": "rotate",
"args": [-0]
@ -1265,16 +1265,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 149]
"args": [25, 149.5]
}, {
"name": "lineTo",
"args": [30, 149]
"args": [30, 149.5]
}, {
"name": "moveTo",
"args": [30, 149]
"args": [30, 149.5]
}, {
"name": "lineTo",
"args": [130, 149]
"args": [130, 149.5]
}, {
"name": "stroke",
"args": []
@ -1283,7 +1283,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 148.5]
"args": [20, 149]
}, {
"name": "rotate",
"args": [-0]
@ -1304,16 +1304,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 178.7]
"args": [25, 178.5]
}, {
"name": "lineTo",
"args": [30, 178.7]
"args": [30, 178.5]
}, {
"name": "moveTo",
"args": [30, 178.7]
"args": [30, 178.5]
}, {
"name": "lineTo",
"args": [130, 178.7]
"args": [130, 178.5]
}, {
"name": "stroke",
"args": []
@ -1322,7 +1322,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 178.2]
"args": [20, 178]
}, {
"name": "rotate",
"args": [-0]
@ -1337,16 +1337,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 208.4]
"args": [25, 208.5]
}, {
"name": "lineTo",
"args": [30, 208.4]
"args": [30, 208.5]
}, {
"name": "moveTo",
"args": [30, 208.4]
"args": [30, 208.5]
}, {
"name": "lineTo",
"args": [130, 208.4]
"args": [130, 208.5]
}, {
"name": "stroke",
"args": []
@ -1355,7 +1355,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 207.9]
"args": [20, 208]
}, {
"name": "rotate",
"args": [-0]
@ -1370,16 +1370,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 238.1]
"args": [25, 238.5]
}, {
"name": "lineTo",
"args": [30, 238.1]
"args": [30, 238.5]
}, {
"name": "moveTo",
"args": [30, 238.1]
"args": [30, 238.5]
}, {
"name": "lineTo",
"args": [130, 238.1]
"args": [130, 238.5]
}, {
"name": "stroke",
"args": []
@ -1388,7 +1388,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 237.6]
"args": [20, 238]
}, {
"name": "rotate",
"args": [-0]
@ -1403,16 +1403,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 267.8]
"args": [25, 267.5]
}, {
"name": "lineTo",
"args": [30, 267.8]
"args": [30, 267.5]
}, {
"name": "moveTo",
"args": [30, 267.8]
"args": [30, 267.5]
}, {
"name": "lineTo",
"args": [130, 267.8]
"args": [130, 267.5]
}, {
"name": "stroke",
"args": []
@ -1421,7 +1421,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 267.3]
"args": [20, 267]
}, {
"name": "rotate",
"args": [-0]
@ -1650,16 +1650,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 30.2]
"args": [25, 30.5]
}, {
"name": "lineTo",
"args": [30, 30.2]
"args": [30, 30.5]
}, {
"name": "moveTo",
"args": [30, 30.2]
"args": [30, 30.5]
}, {
"name": "lineTo",
"args": [130, 30.2]
"args": [130, 30.5]
}, {
"name": "stroke",
"args": []
@ -1668,7 +1668,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 29.7]
"args": [20, 30]
}, {
"name": "rotate",
"args": [-0]
@ -1683,16 +1683,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 89.6]
"args": [25, 89.5]
}, {
"name": "lineTo",
"args": [30, 89.6]
"args": [30, 89.5]
}, {
"name": "moveTo",
"args": [30, 89.6]
"args": [30, 89.5]
}, {
"name": "lineTo",
"args": [130, 89.6]
"args": [130, 89.5]
}, {
"name": "stroke",
"args": []
@ -1701,7 +1701,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 89.1]
"args": [20, 89]
}, {
"name": "rotate",
"args": [-0]
@ -1722,16 +1722,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 149]
"args": [25, 149.5]
}, {
"name": "lineTo",
"args": [30, 149]
"args": [30, 149.5]
}, {
"name": "moveTo",
"args": [30, 149]
"args": [30, 149.5]
}, {
"name": "lineTo",
"args": [130, 149]
"args": [130, 149.5]
}, {
"name": "stroke",
"args": []
@ -1740,7 +1740,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 148.5]
"args": [20, 149]
}, {
"name": "rotate",
"args": [-0]
@ -1761,16 +1761,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 208.4]
"args": [25, 208.5]
}, {
"name": "lineTo",
"args": [30, 208.4]
"args": [30, 208.5]
}, {
"name": "moveTo",
"args": [30, 208.4]
"args": [30, 208.5]
}, {
"name": "lineTo",
"args": [130, 208.4]
"args": [130, 208.5]
}, {
"name": "stroke",
"args": []
@ -1779,7 +1779,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 207.9]
"args": [20, 208]
}, {
"name": "rotate",
"args": [-0]
@ -1794,16 +1794,16 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "moveTo",
"args": [25, 267.8]
"args": [25, 267.5]
}, {
"name": "lineTo",
"args": [30, 267.8]
"args": [30, 267.5]
}, {
"name": "moveTo",
"args": [30, 267.8]
"args": [30, 267.5]
}, {
"name": "lineTo",
"args": [130, 267.8]
"args": [130, 267.5]
}, {
"name": "stroke",
"args": []
@ -1812,7 +1812,7 @@ describe('Linear Scale', function() {
"args": []
}, {
"name": "translate",
"args": [20, 267.3]
"args": [20, 267]
}, {
"name": "rotate",
"args": [-0]

View File

@ -81,7 +81,7 @@ describe('Logarithmic Scale tests', function() {
scale.update(400, 400);
expect(scale.min).toBe(1);
expect(scale.max).toBe(10000);
expect(scale.max).toBe(5000);
});
it('Should correctly determine the max & min of string data values', function() {
@ -117,7 +117,7 @@ describe('Logarithmic Scale tests', function() {
scale.update(400, 400);
expect(scale.min).toBe(1);
expect(scale.max).toBe(10000);
expect(scale.max).toBe(5000);
});
it('Should correctly determine the max & min data values when there are hidden datasets', function() {
@ -154,7 +154,7 @@ describe('Logarithmic Scale tests', function() {
scale.update(400, 400);
expect(scale.min).toBe(1);
expect(scale.max).toBe(10000);
expect(scale.max).toBe(5000);
});
it('Should correctly determine the max & min for scatter data', function() {
@ -194,7 +194,7 @@ describe('Logarithmic Scale tests', function() {
verticalScale.update(400, 400);
expect(verticalScale.min).toBe(1);
expect(verticalScale.max).toBe(1000);
expect(verticalScale.max).toBe(200);
var horizontalConfig = Chart.helpers.clone(config);
horizontalConfig.position = 'bottom';
@ -250,7 +250,7 @@ describe('Logarithmic Scale tests', function() {
scale.update(400, 400);
expect(scale.min).toBe(10);
expect(scale.max).toBe(1000);
expect(scale.max).toBe(200);
});
it('Should correctly determine the min and max data values when stacked mode is turned on ignoring hidden datasets', function() {
@ -293,7 +293,7 @@ describe('Logarithmic Scale tests', function() {
scale.update(400, 400);
expect(scale.min).toBe(10);
expect(scale.max).toBe(1000);
expect(scale.max).toBe(200);
});
it('Should ensure that the scale has a max and min that are not equal', function() {
@ -392,9 +392,9 @@ describe('Logarithmic Scale tests', function() {
scale.buildTicks();
// Counts down because the lines are drawn top to bottom
expect(scale.ticks).toEqual([100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
expect(scale.ticks).toEqual([80, 70, 60, 50, 40, 30, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
expect(scale.start).toBe(1);
expect(scale.end).toBe(100);
expect(scale.end).toBe(80);
});
it('Should generate tick marks in the correct order in reversed mode', function() {
@ -427,8 +427,8 @@ describe('Logarithmic Scale tests', function() {
scale.buildTicks();
// Counts down because the lines are drawn top to bottom
expect(scale.ticks).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]);
expect(scale.start).toBe(100);
expect(scale.ticks).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80]);
expect(scale.start).toBe(80);
expect(scale.end).toBe(1);
});
@ -456,7 +456,7 @@ describe('Logarithmic Scale tests', function() {
scale.update(400, 400);
expect(scale.ticks).toEqual(['1e+2', '', '', '', '', '5e+1', '', '', '2e+1', '1e+1', '', '', '', '', '5e+0', '', '', '2e+0', '1e+0']);
expect(scale.ticks).toEqual(['8e+1', '', '', '5e+1', '', '', '2e+1', '1e+1', '', '', '', '', '5e+0', '', '', '2e+0', '1e+0']);
});
it('Should build labels using the user supplied callback', function() {
@ -488,7 +488,7 @@ describe('Logarithmic Scale tests', function() {
scale.update(400, 400);
// Just the index
expect(scale.ticks).toEqual(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18']);
expect(scale.ticks).toEqual(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16']);
});
it('Should get the correct pixel value for a point', function() {
@ -526,9 +526,9 @@ describe('Logarithmic Scale tests', function() {
verticalScale.width = 50;
verticalScale.height = 110;
expect(verticalScale.getPixelForValue(100, 0, 0)).toBe(5); // top + paddingTop
expect(verticalScale.getPixelForValue(80, 0, 0)).toBe(5); // top + paddingTop
expect(verticalScale.getPixelForValue(1, 0, 0)).toBe(105); // bottom - paddingBottom
expect(verticalScale.getPixelForValue(10, 0, 0)).toBe(55); // halfway
expect(verticalScale.getPixelForValue(10, 0, 0)).toBeCloseTo(52.4, 1e-4); // halfway
var horizontalConfig = Chart.helpers.clone(config);
horizontalConfig.position = 'bottom';
@ -553,8 +553,8 @@ describe('Logarithmic Scale tests', function() {
horizontalScale.width = 110;
horizontalScale.height = 50;
expect(horizontalScale.getPixelForValue(100, 0, 0)).toBe(105); // right - paddingRight
expect(horizontalScale.getPixelForValue(80, 0, 0)).toBe(105); // right - paddingRight
expect(horizontalScale.getPixelForValue(1, 0, 0)).toBe(5); // left + paddingLeft
expect(horizontalScale.getPixelForValue(10, 0, 0)).toBe(55); // halfway
expect(horizontalScale.getPixelForValue(10, 0, 0)).toBeCloseTo(57.5, 1e-4); // halfway
});
});