From: Evert Timberg Date: Tue, 17 Nov 2015 23:43:18 +0000 (-0500) Subject: Make sure data is converted to a number in scales when determining min and max. Add... X-Git-Tag: 2.0.0-beta1~19^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1662%2Fhead;p=thirdparty%2FChart.js.git Make sure data is converted to a number in scales when determining min and max. Add tests for this condition to linear, radialLinear, and logarithmic scales. --- diff --git a/src/scales/scale.linear.js b/src/scales/scale.linear.js index 841353926..2cc8254e6 100644 --- a/src/scales/scale.linear.js +++ b/src/scales/scale.linear.js @@ -60,7 +60,7 @@ if (helpers.isDatasetVisible(dataset) && (this.isHorizontal() ? dataset.xAxisID === this.id : dataset.yAxisID === this.id)) { helpers.each(dataset.data, function(rawValue, index) { - var value = this.getRightValue(rawValue); + var value = +this.getRightValue(rawValue); if (isNaN(value)) { return; } @@ -93,7 +93,7 @@ helpers.each(this.chart.data.datasets, function(dataset) { if (helpers.isDatasetVisible(dataset) && (this.isHorizontal() ? dataset.xAxisID === this.id : dataset.yAxisID === this.id)) { helpers.each(dataset.data, function(rawValue, index) { - var value = this.getRightValue(rawValue); + var value = +this.getRightValue(rawValue); if (isNaN(value)) { return; } @@ -205,14 +205,14 @@ }, getLabelForIndex: function(index, datasetIndex) { - return this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]); + return +this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]); }, // Utils getPixelForValue: function(value, index, datasetIndex, includeOffset) { // This must be called after fit has been run so that // this.left, this.top, this.right, and this.bottom have been defined - var rightValue = this.getRightValue(value); + var rightValue = +this.getRightValue(value); var pixel; var range = this.end - this.start; diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index ce750c650..f07b81303 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -41,7 +41,7 @@ helpers.each(dataset.data, function(rawValue, index) { var values = valuesPerType[dataset.type]; - var value = this.getRightValue(rawValue); + var value = +this.getRightValue(rawValue); if (isNaN(value)) { return; } @@ -69,7 +69,7 @@ helpers.each(this.chart.data.datasets, function(dataset) { if (helpers.isDatasetVisible(dataset) && (this.isHorizontal() ? dataset.xAxisID === this.id : dataset.yAxisID === this.id)) { helpers.each(dataset.data, function(rawValue, index) { - var value = this.getRightValue(rawValue); + var value = +this.getRightValue(rawValue); if (isNaN(value)) { return; } @@ -145,7 +145,7 @@ }, // Get the correct tooltip label getLabelForIndex: function(index, datasetIndex) { - return this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]); + return +this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]); }, getPixelForTick: function(index, includeOffset) { return this.getPixelForValue(this.tickValues[index], null, null, includeOffset); @@ -153,7 +153,7 @@ getPixelForValue: function(value, index, datasetIndex, includeOffset) { var pixel; - var newVal = this.getRightValue(value); + var newVal = +this.getRightValue(value); var range = helpers.log10(this.end) - helpers.log10(this.start); if (this.isHorizontal()) { diff --git a/src/scales/scale.radialLinear.js b/src/scales/scale.radialLinear.js index a132dcf3b..c436ff805 100644 --- a/src/scales/scale.radialLinear.js +++ b/src/scales/scale.radialLinear.js @@ -70,7 +70,7 @@ helpers.each(this.chart.data.datasets, function(dataset) { if (helpers.isDatasetVisible(dataset)) { helpers.each(dataset.data, function(rawValue, index) { - var value = this.getRightValue(rawValue); + var value = +this.getRightValue(rawValue); if (isNaN(value)) { return; } @@ -153,7 +153,7 @@ this.zeroLineIndex = this.ticks.indexOf(0); }, getLabelForIndex: function(index, datasetIndex) { - return this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]); + return +this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]); }, getCircumference: function() { return ((Math.PI * 2) / this.getValueCount()); diff --git a/test/scale.linear.tests.js b/test/scale.linear.tests.js index 0fd98dc4f..bace791b8 100644 --- a/test/scale.linear.tests.js +++ b/test/scale.linear.tests.js @@ -88,6 +88,45 @@ describe('Linear Scale', function() { expect(scale.max).toBe(150); }); + it('Should correctly determine the max & min of string data values', function() { + var scaleID = 'myScale'; + + var mockData = { + datasets: [{ + yAxisID: scaleID, + data: ['10', '5', '0', '-5', '78', '-100'] + }, { + yAxisID: 'second scale', + data: ['-1000', '1000'], + }, { + yAxisID: scaleID, + data: ['150'] + }] + }; + + var Constructor = Chart.scaleService.getScaleConstructor('linear'); + var scale = new Constructor({ + ctx: {}, + options: Chart.scaleService.getScaleDefaults('linear'), // use default config for scale + chart: { + data: mockData + }, + id: scaleID + }); + + expect(scale).not.toEqual(undefined); // must construct + expect(scale.min).toBe(undefined); // not yet set + expect(scale.max).toBe(undefined); + + // Set arbitrary width and height for now + scale.width = 50; + scale.height = 400; + + scale.buildTicks(); + expect(scale.min).toBe(-100); + expect(scale.max).toBe(150); + }); + it('Should correctly determine the max & min data values ignoring hidden datasets', function() { var scaleID = 'myScale'; diff --git a/test/scale.logarithmic.tests.js b/test/scale.logarithmic.tests.js index 85c563212..aa8e31283 100644 --- a/test/scale.logarithmic.tests.js +++ b/test/scale.logarithmic.tests.js @@ -85,6 +85,42 @@ describe('Logarithmic Scale tests', function() { expect(scale.max).toBe(10000); }); + it('Should correctly determine the max & min of string data values', function() { + var scaleID = 'myScale'; + + var mockData = { + datasets: [{ + yAxisID: scaleID, + data: ['10', '5', '5000', '78', '450'] + }, { + yAxisID: 'second scale', + data: ['1', '1000', '10', '100'], + }, { + yAxisID: scaleID, + data: ['150'] + }] + }; + + var mockContext = window.createMockContext(); + var Constructor = Chart.scaleService.getScaleConstructor('logarithmic'); + var scale = new Constructor({ + ctx: mockContext, + options: Chart.scaleService.getScaleDefaults('logarithmic'), // use default config for scale + chart: { + data: mockData, + }, + id: scaleID + }); + + expect(scale).not.toEqual(undefined); // must construct + expect(scale.min).toBe(undefined); // not yet set + expect(scale.max).toBe(undefined); + + scale.update(400, 400); + expect(scale.min).toBe(1); + expect(scale.max).toBe(10000); + }); + it('Should correctly determine the max & min data values when there are hidden datasets', function() { var scaleID = 'myScale'; diff --git a/test/scale.radialLinear.tests.js b/test/scale.radialLinear.tests.js index 0720d0e54..962eb71b8 100644 --- a/test/scale.radialLinear.tests.js +++ b/test/scale.radialLinear.tests.js @@ -97,6 +97,36 @@ describe('Test the radial linear scale', function() { expect(scale.max).toBe(200); }); + it('Should correctly determine the max & min of string data values', function() { + var scaleID = 'myScale'; + + var mockData = { + datasets: [{ + yAxisID: scaleID, + data: ['10', '5', '0', '-5', '78', '-100'] + }, { + yAxisID: scaleID, + data: ['150'] + }], + labels: ['lablel1', 'label2', 'label3', 'label4', 'label5', 'label6'] + }; + + var mockContext = window.createMockContext(); + var Constructor = Chart.scaleService.getScaleConstructor('radialLinear'); + var scale = new Constructor({ + ctx: mockContext, + options: Chart.scaleService.getScaleDefaults('radialLinear'), // use default config for scale + chart: { + data: mockData + }, + id: scaleID, + }); + + scale.update(200, 300); + expect(scale.min).toBe(-100); + expect(scale.max).toBe(200); + }); + it('Should correctly determine the max & min data values when there are hidden datasets', function() { var scaleID = 'myScale';