Merge pull request #1601 from nnnick/fix/stacked-bar-line-scale

Fixed stacked scale calculation for combo charts
This commit is contained in:
Evert Timberg 2015-11-03 18:14:37 -05:00
commit d6252df4cd
4 changed files with 56 additions and 18 deletions

View File

@ -42,11 +42,21 @@
this.min = null;
this.max = null;
var positiveValues = [];
var negativeValues = [];
if (this.options.stacked) {
var valuesPerType = {};
helpers.each(this.data.datasets, function(dataset) {
if (valuesPerType[dataset.type] === undefined) {
valuesPerType[dataset.type] = {
positiveValues: [],
negativeValues: [],
};
}
// Store these per type
var positiveValues = valuesPerType[dataset.type].positiveValues;
var negativeValues = valuesPerType[dataset.type].negativeValues;
if (helpers.isDatasetVisible(dataset) && (this.isHorizontal() ? dataset.xAxisID === this.id : dataset.yAxisID === this.id)) {
helpers.each(dataset.data, function(rawValue, index) {
@ -71,9 +81,13 @@
}
}, this);
var values = positiveValues.concat(negativeValues);
this.min = helpers.min(values);
this.max = helpers.max(values);
helpers.each(valuesPerType, function(valuesForType) {
var values = valuesForType.positiveValues.concat(valuesForType.negativeValues);
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 {
helpers.each(this.data.datasets, function(dataset) {

View File

@ -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) {

View File

@ -188,13 +188,19 @@ describe('Linear Scale', function() {
var mockData = {
datasets: [{
yAxisID: scaleID,
data: [10, 5, 0, -5, 78, -100]
data: [10, 5, 0, -5, 78, -100],
type: 'bar'
}, {
yAxisID: 'second scale',
data: [-1000, 1000],
}, {
yAxisID: scaleID,
data: [150, 0, 0, -100, -10, 9]
data: [150, 0, 0, -100, -10, 9],
type: 'bar'
}, {
yAxisID: scaleID,
data: [10, 10, 10, 10, 10, 10],
type: 'line'
}]
};

View File

@ -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'
}]
};