From: Evert Timberg Date: Wed, 7 Oct 2015 23:37:53 +0000 (-0400) Subject: Scales will only consider visible datasets when calculating data max and min values X-Git-Tag: 2.0.0-beta~15^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aaec5efd5220b6d31736994e4d43403e48f23b77;p=thirdparty%2FChart.js.git Scales will only consider visible datasets when calculating data max and min values --- diff --git a/src/scales/scale.linear.js b/src/scales/scale.linear.js index d522ae91c..79ea5937f 100644 --- a/src/scales/scale.linear.js +++ b/src/scales/scale.linear.js @@ -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); diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index 2db11bf28..eba0877ba 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -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); diff --git a/src/scales/scale.radialLinear.js b/src/scales/scale.radialLinear.js index b9fae20a7..67860d9c9 100644 --- a/src/scales/scale.radialLinear.js +++ b/src/scales/scale.radialLinear.js @@ -68,21 +68,23 @@ 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) { diff --git a/test/scale.linear.tests.js b/test/scale.linear.tests.js index c927c97c3..305c24064 100644 --- a/test/scale.linear.tests.js +++ b/test/scale.linear.tests.js @@ -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'; diff --git a/test/scale.logarithmic.tests.js b/test/scale.logarithmic.tests.js index 30cc240d9..ea1092749 100644 --- a/test/scale.logarithmic.tests.js +++ b/test/scale.logarithmic.tests.js @@ -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'; diff --git a/test/scale.radialLinear.tests.js b/test/scale.radialLinear.tests.js index cfbf9d491..5a7620789 100644 --- a/test/scale.radialLinear.tests.js +++ b/test/scale.radialLinear.tests.js @@ -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';