]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Make sure data is converted to a number in scales when determining min and max. Add... 1662/head
authorEvert Timberg <evert.timberg@gmail.com>
Tue, 17 Nov 2015 23:43:18 +0000 (18:43 -0500)
committerEvert Timberg <evert.timberg@gmail.com>
Tue, 17 Nov 2015 23:43:18 +0000 (18:43 -0500)
src/scales/scale.linear.js
src/scales/scale.logarithmic.js
src/scales/scale.radialLinear.js
test/scale.linear.tests.js
test/scale.logarithmic.tests.js
test/scale.radialLinear.tests.js

index 8413539265182e3eefb62d0178376cf9b7048952..2cc8254e60f0381916d66f252a748d6f4abfc140 100644 (file)
@@ -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;
                                                        }
                },
 
                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;
 
index ce750c650825c2e0fbc6543cd2d0f9c2d2ba5d7b..f07b81303445971a9843144e57cbd1b45dda9680 100644 (file)
@@ -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;
                                                        }
                },
                // 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);
                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()) {
index a132dcf3b9c3e877b5a3efb274c75f757cbf3815..c436ff805afec08b209d746b59b671ed227d6e32 100644 (file)
@@ -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;
                                                }
                        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());
index 0fd98dc4fb6f97133cdf799a69039e332d67435d..bace791b8128be1db7fb529d9a2182576e14b2ca 100644 (file)
@@ -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';
 
index 85c56321264e2aada6f63dd1b0e1b9b751cd7259..aa8e31283ba0be5e977d6486f437432267366ad7 100644 (file)
@@ -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';
 
index 0720d0e54ffef59ce5eb0127385ca63f1f4303bb..962eb71b8873db04d5516bb09f2b6dd2d0010bca 100644 (file)
@@ -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';