From 4267d01bc2c3adc325a2972c2f0d72a44129cbf1 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Sun, 1 Nov 2015 09:07:18 -0500 Subject: [PATCH] Log scale + tests --- src/scales/scale.linear.js | 6 ++++-- src/scales/scale.logarithmic.js | 18 +++++++++++++----- test/scale.logarithmic.tests.js | 20 +++++++++++++++----- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/scales/scale.linear.js b/src/scales/scale.linear.js index 9b6200ef7..ac8954fa1 100644 --- a/src/scales/scale.linear.js +++ b/src/scales/scale.linear.js @@ -83,8 +83,10 @@ helpers.each(valuesPerType, function(valuesForType) { var values = valuesForType.positiveValues.concat(valuesForType.negativeValues); - this.min = Math.min(this.min, helpers.min(values)); - this.max = Math.max(this.max, helpers.max(values)); + var minVal = helpers.min(values); + var maxVal = helpers.max(values); + this.min = this.min === null ? minVal : Math.min(this.min, minVal); + this.max = this.max === null ? maxVal : Math.max(this.max, maxVal); }, this); } else { diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index 9de8e1d4e..467a4cc8b 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -30,13 +30,17 @@ this.min = null; this.max = null; - var values = []; - if (this.options.stacked) { + var valuesPerType = {}; + helpers.each(this.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) { + if (valuesPerType[dataset.type] === undefined) { + valuesPerType[dataset.type] = []; + } + helpers.each(dataset.data, function(rawValue, index) { + var values = valuesPerType[dataset.type]; var value = this.getRightValue(rawValue); if (isNaN(value)) { return; @@ -54,8 +58,12 @@ } }, this); - this.min = helpers.min(values); - this.max = helpers.max(values); + helpers.each(valuesPerType, function(valuesForType) { + var minVal = helpers.min(valuesForType); + var maxVal = helpers.max(valuesForType); + this.min = this.min === null ? minVal : Math.min(this.min, minVal); + this.max = this.max === null ? maxVal : Math.max(this.max, maxVal); + }, this); } else { helpers.each(this.data.datasets, function(dataset) { diff --git a/test/scale.logarithmic.tests.js b/test/scale.logarithmic.tests.js index 72250df73..7493e3de6 100644 --- a/test/scale.logarithmic.tests.js +++ b/test/scale.logarithmic.tests.js @@ -175,13 +175,19 @@ describe('Logarithmic Scale tests', function() { var mockData = { datasets: [{ yAxisID: scaleID, - data: [10, 5, 1, 5, 78, 100] + data: [10, 5, 1, 5, 78, 100], + type: 'bar' }, { yAxisID: 'second scale', data: [-1000, 1000], }, { yAxisID: scaleID, - data: [150, 10, 10, 100, 10, 9] + data: [150, 10, 10, 100, 10, 9], + type: 'bar' + }, { + yAxisID: scaleID, + data: [100, 100, 100, 100, 100, 100], + type: 'line' }] }; @@ -208,17 +214,21 @@ describe('Logarithmic Scale tests', function() { var mockData = { datasets: [{ yAxisID: scaleID, - data: [10, 5, 1, 5, 78, 100] + data: [10, 5, 1, 5, 78, 100], + type: 'bar' }, { yAxisID: 'second scale', data: [-1000, 1000], + type: 'bar' }, { yAxisID: scaleID, - data: [150, 10, 10, 100, 10, 9] + data: [150, 10, 10, 100, 10, 9], + type: 'bar' }, { yAxisID: scaleID, data: [10000, 10000, 10000, 10000, 10000, 10000], - hidden: true + hidden: true, + type: 'bar' }] }; -- 2.47.2