]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Scales will only consider visible datasets when calculating data max and min values
authorEvert Timberg <evert.timberg@gmail.com>
Wed, 7 Oct 2015 23:37:53 +0000 (19:37 -0400)
committerEvert Timberg <evert.timberg@gmail.com>
Wed, 7 Oct 2015 23:37:53 +0000 (19:37 -0400)
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 d522ae91cf6fe7ce972d93459b386f3ddd329e52..79ea5937f204d772addc63c4f2592d05e07eba94 100644 (file)
@@ -21,7 +21,7 @@
 
                        if (this.options.stacked) {
                                helpers.each(this.data.datasets, function(dataset) {
-                                       if (this.isHorizontal() ? dataset.xAxisID === this.id : dataset.yAxisID === this.id) {
+                                       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);
@@ -48,7 +48,7 @@
 
                        } else {
                                helpers.each(this.data.datasets, function(dataset) {
-                                       if (this.isHorizontal() ? dataset.xAxisID === this.id : dataset.yAxisID === this.id) {
+                                       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);
 
index 2db11bf2801c600137af42d27233bdc2663a5e08..eba0877bae4eb83ec54d7bfd8a74d642df4d0b18 100644 (file)
@@ -26,7 +26,7 @@
 
                        if (this.options.stacked) {
                                helpers.each(this.data.datasets, function(dataset) {
-                                       if (this.isHorizontal() ? dataset.xAxisID === this.id : dataset.yAxisID === this.id) {
+                                       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);
@@ -48,7 +48,7 @@
 
                        } else {
                                helpers.each(this.data.datasets, function(dataset) {
-                                       if (this.isHorizontal() ? dataset.xAxisID === this.id : dataset.yAxisID === this.id) {
+                                       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);
 
index b9fae20a7b65fe54882920529701a1c58344ea96..67860d9c97dc3dd6e5417947d733e29fa5cd1d8a 100644 (file)
                        this.max = null;
 
                        helpers.each(this.data.datasets, function(dataset) {
-                               helpers.each(dataset.data, function(value, index) {
-                                       if (value === null) return;
-
-                                       if (this.min === null) {
-                                               this.min = value;
-                                       } else if (value < this.min) {
-                                               this.min = value;
-                                       }
+                               if (helpers.isDatasetVisible(dataset)) {
+                                       helpers.each(dataset.data, function(value, index) {
+                                               if (value === null) return;
+
+                                               if (this.min === null) {
+                                                       this.min = value;
+                                               } else if (value < this.min) {
+                                                       this.min = value;
+                                               }
 
-                                       if (this.max === null) {
-                                               this.max = value;
-                                       } else if (value > this.max) {
-                                               this.max = value;
-                                       }
-                               }, this);
+                                               if (this.max === null) {
+                                                       this.max = value;
+                                               } else if (value > this.max) {
+                                                       this.max = value;
+                                               }
+                                       }, this);
+                               }
                        }, this);
 
                        if (this.min === this.max) {
index c927c97c3726b8483c0642075ae15424f1e8b90e..305c240643f2e906215fdc75992880fc77abc819 100644 (file)
@@ -84,6 +84,44 @@ describe('Linear Scale', function() {
                expect(scale.max).toBe(150);
        });
 
+       it('Should correctly determine the max & min data values ignoring hidden datasets', 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],
+                               hidden: true
+                       }]
+               };
+
+               var Constructor = Chart.scaleService.getScaleConstructor('linear');
+               var scale = new Constructor({
+                       ctx: {},
+                       options: Chart.scaleService.getScaleDefaults('linear'), // use default config for scale
+                       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(80);
+       });
+
        it('Should correctly determine the max & min for scatter data', function() {
                var scaleID = 'myScale';
 
@@ -178,6 +216,46 @@ describe('Linear Scale', function() {
                expect(scale.max).toBe(200);
        });
 
+       it('Should correctly determine the min and max data values when stacked mode is turned on and there are hidden datasets', 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, 0, 0, -100, -10, 9]
+                       }, {
+                               yAxisID: scaleID,
+                               data: [10, 20, 30, 40, 50, 60],
+                               hidden: true
+                       }]
+               };
+
+               var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('linear'));
+               config.stacked = true; // enable scale stacked mode
+
+               var Constructor = Chart.scaleService.getScaleConstructor('linear');
+               var scale = new Constructor({
+                       ctx: {},
+                       options: config,
+                       data: mockData,
+                       id: scaleID
+               });
+
+               // Set arbitrary width and height for now
+               scale.width = 50;
+               scale.height = 400;
+
+               scale.buildTicks();
+               expect(scale.min).toBe(-150);
+               expect(scale.max).toBe(200);
+       });
+
        it('Should ensure that the scale has a max and min that are not equal', function() {
                var scaleID = 'myScale';
 
index 30cc240d9f749aa95f2ead548b7ff9455d8d8545..ea1092749becec3dcbb093014383ba740cd8ab5c 100644 (file)
@@ -80,6 +80,41 @@ describe('Logarithmic Scale tests', function() {
                expect(scale.max).toBe(10000);
        });
 
+       it('Should correctly determine the max & min data values when there are hidden datasets', 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: [50000],
+                               hidden: true
+                       }]
+               };
+
+               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
+                       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 for scatter data', function() {
                var scaleID = 'myScale';
 
@@ -164,6 +199,43 @@ describe('Logarithmic Scale tests', function() {
                expect(scale.max).toBe(1000);
        });
 
+       it('Should correctly determine the min and max data values when stacked mode is turned on ignoring hidden datasets', function() {
+               var scaleID = 'myScale';
+
+               var mockData = {
+                       datasets: [{
+                               yAxisID: scaleID,
+                               data: [10, 5, 1, 5, 78, 100]
+                       }, {
+                               yAxisID: 'second scale',
+                               data: [-1000, 1000],
+                       }, {
+                               yAxisID: scaleID,
+                               data: [150, 10, 10, 100, 10, 9]
+                       }, {
+                               yAxisID: scaleID,
+                               data: [10000, 10000, 10000, 10000, 10000, 10000],
+                               hidden: true
+                       }]
+               };
+
+               var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('logarithmic'));
+               config.stacked = true; // enable scale stacked mode
+
+               var mockContext = window.createMockContext();
+               var Constructor = Chart.scaleService.getScaleConstructor('logarithmic');
+               var scale = new Constructor({
+                       ctx: mockContext,
+                       options: config,
+                       data: mockData,
+                       id: scaleID
+               });
+
+               scale.update(400, 400);
+               expect(scale.min).toBe(10);
+               expect(scale.max).toBe(1000);
+       });
+
        it('Should ensure that the scale has a max and min that are not equal', function() {
                var scaleID = 'myScale';
 
index cfbf9d4919528aca5de4b5df4ff253f727c79286..5a762078918b2c1da7eaf57be3dee55de438466f 100644 (file)
@@ -92,6 +92,38 @@ describe('Test the radial linear scale', function() {
                expect(scale.max).toBe(200);
        });
 
+       it('Should correctly determine the max & min data values when there are hidden datasets', function() {
+               var scaleID = 'myScale';
+
+               var mockData = {
+                       datasets: [{
+                               yAxisID: scaleID,
+                               data: [10, 5, 0, -5, 78, -100]
+                       }, {
+                               yAxisID: scaleID,
+                               data: [150]
+                       }, {
+                               yAxisID: scaleID,
+                               data: [1000],
+                               hidden: true
+                       }],
+                       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
+                       data: mockData,
+                       id: scaleID,
+               });
+
+               scale.update(200, 300);
+               expect(scale.min).toBe(-100);
+               expect(scale.max).toBe(200);
+       });
+
        it('Should ensure that the scale has a max and min that are not equal', function() {
                var scaleID = 'myScale';