From: Evert Timberg Date: Sun, 17 Apr 2016 14:33:38 +0000 (-0400) Subject: More prework for zoom/pan X-Git-Tag: 2.1.0~70 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1a49872ce4f0856744c137ee78b29f5ee0c7e25b;p=thirdparty%2FChart.js.git More prework for zoom/pan --- diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index a93536129..5b096fef1 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -9,25 +9,31 @@ module.exports = function(Chart) { }; var DatasetScale = Chart.Scale.extend({ - buildTicks: function(index) { - this.startIndex = 0; - this.endIndex = this.chart.data.labels.length; + // Implement this so that + determineDataLimits: function() { + this.minIndex = 0; + this.maxIndex = this.chart.data.labels.length; var findIndex; if (this.options.ticks.min !== undefined) { // user specified min value findIndex = helpers.indexOf(this.chart.data.labels, this.options.ticks.min); - this.startIndex = findIndex !== -1 ? findIndex : this.startIndex; + this.minIndex = findIndex !== -1 ? findIndex : this.minIndex; } if (this.options.ticks.max !== undefined) { // user specified max value findIndex = helpers.indexOf(this.chart.data.labels, this.options.ticks.max); - this.endIndex = findIndex !== -1 ? findIndex : this.endIndex; + this.maxIndex = findIndex !== -1 ? findIndex : this.maxIndex; } + this.min = this.chart.data.labels[this.minIndex]; + this.max = this.chart.data.labels[this.maxIndex]; + }, + + buildTicks: function(index) { // If we are viewing some subset of labels, slice the original array - this.ticks = (this.startIndex === 0 && this.endIndex === this.chart.data.labels.length) ? this.chart.data.labels : this.chart.data.labels.slice(this.startIndex, this.endIndex + 1); + this.ticks = (this.minIndex === 0 && this.maxIndex === this.chart.data.labels.length) ? this.chart.data.labels : this.chart.data.labels.slice(this.minIndex, this.maxIndex + 1); }, getLabelForIndex: function(index, datasetIndex) { @@ -42,7 +48,7 @@ module.exports = function(Chart) { if (this.isHorizontal()) { var innerWidth = this.width - (this.paddingLeft + this.paddingRight); var valueWidth = innerWidth / offsetAmt; - var widthOffset = (valueWidth * (index - this.startIndex)) + this.paddingLeft; + var widthOffset = (valueWidth * (index - this.minIndex)) + this.paddingLeft; if (this.options.gridLines.offsetGridLines && includeOffset) { widthOffset += (valueWidth / 2); @@ -52,7 +58,7 @@ module.exports = function(Chart) { } else { var innerHeight = this.height - (this.paddingTop + this.paddingBottom); var valueHeight = innerHeight / offsetAmt; - var heightOffset = (valueHeight * (index - this.startIndex)) + this.paddingTop; + var heightOffset = (valueHeight * (index - this.minIndex)) + this.paddingTop; if (this.options.gridLines.offsetGridLines && includeOffset) { heightOffset += (valueHeight / 2); @@ -62,7 +68,7 @@ module.exports = function(Chart) { } }, getPixelForTick: function(index, includeOffset) { - return this.getPixelForValue(this.ticks[index], index + this.startIndex, null, includeOffset); + return this.getPixelForValue(this.ticks[index], index + this.minIndex, null, includeOffset); } }); diff --git a/src/scales/scale.linear.js b/src/scales/scale.linear.js index 9c6f1aeba..3cf2b020d 100644 --- a/src/scales/scale.linear.js +++ b/src/scales/scale.linear.js @@ -8,7 +8,8 @@ module.exports = function(Chart) { position: "left", ticks: { callback: function(tickValue, index, ticks) { - var delta = ticks[1] - ticks[0]; + // If we have lots of ticks, don't use the ones + var delta = ticks.length > 3 ? ticks[2] - ticks[1] : ticks[1] - ticks[0]; // If we have a number like 2.5 as the delta, figure out how many decimal places we need if (Math.abs(delta) > 1) { diff --git a/test/scale.category.tests.js b/test/scale.category.tests.js index 47645fe23..8346ff0d1 100644 --- a/test/scale.category.tests.js +++ b/test/scale.category.tests.js @@ -66,6 +66,7 @@ describe('Category scale tests', function() { id: scaleID }); + scale.determineDataLimits(); scale.buildTicks(); expect(scale.ticks).toEqual(mockData.labels); }); @@ -92,6 +93,7 @@ describe('Category scale tests', function() { id: scaleID }); + scale.determineDataLimits(); scale.buildTicks(); expect(scale.getLabelForIndex(1)).toBe('tick2');