Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bar and line controllers to convert strings to numbers when stacking #2926

Merged
merged 2 commits into from
Jul 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 26 additions & 41 deletions src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,14 @@ module.exports = function(Chart) {
if (yScale.options.stacked) {
var chart = me.chart;
var datasets = chart.data.datasets;
var value = datasets[datasetIndex].data[index];
var value = Number(datasets[datasetIndex].data[index]);

if (value < 0) {
for (var i = 0; i < datasetIndex; i++) {
var negDS = datasets[i];
var negDSMeta = chart.getDatasetMeta(i);
if (negDSMeta.bar && negDSMeta.yAxisID === yScale.id && chart.isDatasetVisible(i)) {
base += negDS.data[index] < 0 ? negDS.data[index] : 0;
}
}
} else {
for (var j = 0; j < datasetIndex; j++) {
var posDS = datasets[j];
var posDSMeta = chart.getDatasetMeta(j);
if (posDSMeta.bar && posDSMeta.yAxisID === yScale.id && chart.isDatasetVisible(j)) {
base += posDS.data[index] > 0 ? posDS.data[index] : 0;
}
for (var i = 0; i < datasetIndex; i++) {
var currentDs = datasets[i];
var currentDsMeta = chart.getDatasetMeta(i);
if (currentDsMeta.bar && currentDsMeta.yAxisID === yScale.id && chart.isDatasetVisible(i)) {
var currentVal = Number(currentDs.data[index]);
base += value < 0 ? Math.min(currentVal, 0) : Math.max(currentVal, 0);
}
}

Expand Down Expand Up @@ -216,7 +207,7 @@ module.exports = function(Chart) {
var me = this;
var meta = me.getMeta();
var yScale = me.getScaleForId(meta.yAxisID);
var value = me.getDataset().data[index];
var value = Number(me.getDataset().data[index]);

if (yScale.options.stacked) {

Expand All @@ -227,10 +218,11 @@ module.exports = function(Chart) {
var ds = me.chart.data.datasets[i];
var dsMeta = me.chart.getDatasetMeta(i);
if (dsMeta.bar && dsMeta.yAxisID === yScale.id && me.chart.isDatasetVisible(i)) {
if (ds.data[index] < 0) {
sumNeg += ds.data[index] || 0;
var stackedVal = Number(ds.data[index]);
if (stackedVal < 0) {
sumNeg += stackedVal || 0;
} else {
sumPos += ds.data[index] || 0;
sumPos += stackedVal || 0;
}
}
}
Expand Down Expand Up @@ -454,24 +446,16 @@ module.exports = function(Chart) {
var base = 0;

if (xScale.options.stacked) {
var chart = me.chart;
var datasets = chart.data.datasets;
var value = Number(datasets[datasetIndex].data[index]);

var value = me.chart.data.datasets[datasetIndex].data[index];

if (value < 0) {
for (var i = 0; i < datasetIndex; i++) {
var negDS = me.chart.data.datasets[i];
var negDSMeta = me.chart.getDatasetMeta(i);
if (negDSMeta.bar && negDSMeta.xAxisID === xScale.id && me.chart.isDatasetVisible(i)) {
base += negDS.data[index] < 0 ? negDS.data[index] : 0;
}
}
} else {
for (var j = 0; j < datasetIndex; j++) {
var posDS = me.chart.data.datasets[j];
var posDSMeta = me.chart.getDatasetMeta(j);
if (posDSMeta.bar && posDSMeta.xAxisID === xScale.id && me.chart.isDatasetVisible(j)) {
base += posDS.data[index] > 0 ? posDS.data[index] : 0;
}
for (var i = 0; i < datasetIndex; i++) {
var currentDs = datasets[i];
var currentDsMeta = chart.getDatasetMeta(i);
if (currentDsMeta.bar && currentDsMeta.xAxisID === xScale.id && chart.isDatasetVisible(i)) {
var currentVal = Number(currentDs.data[index]);
base += value < 0 ? Math.min(currentVal, 0) : Math.max(currentVal, 0);
}
}

Expand Down Expand Up @@ -528,7 +512,7 @@ module.exports = function(Chart) {
var me = this;
var meta = me.getMeta();
var xScale = me.getScaleForId(meta.xAxisID);
var value = me.getDataset().data[index];
var value = Number(me.getDataset().data[index]);

if (xScale.options.stacked) {

Expand All @@ -539,10 +523,11 @@ module.exports = function(Chart) {
var ds = me.chart.data.datasets[i];
var dsMeta = me.chart.getDatasetMeta(i);
if (dsMeta.bar && dsMeta.xAxisID === xScale.id && me.chart.isDatasetVisible(i)) {
if (ds.data[index] < 0) {
sumNeg += ds.data[index] || 0;
var stackedVal = Number(ds.data[index]);
if (stackedVal < 0) {
sumNeg += stackedVal || 0;
} else {
sumPos += ds.data[index] || 0;
sumPos += stackedVal || 0;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/controller.line.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ module.exports = function(Chart) {
ds = chart.data.datasets[i];
dsMeta = chart.getDatasetMeta(i);
if (dsMeta.type === 'line' && chart.isDatasetVisible(i)) {
var stackedRightValue = yScale.getRightValue(ds.data[index]);
var stackedRightValue = Number(yScale.getRightValue(ds.data[index]));
if (stackedRightValue < 0) {
sumNeg += stackedRightValue || 0;
} else {
Expand All @@ -230,7 +230,7 @@ module.exports = function(Chart) {
}
}

var rightValue = yScale.getRightValue(value);
var rightValue = Number(yScale.getRightValue(value));
if (rightValue < 0) {
return yScale.getPixelForValue(sumNeg + rightValue);
} else {
Expand Down
54 changes: 54 additions & 0 deletions test/controller.bar.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,60 @@ describe('Bar controller tests', function() {
});
});

it('should update elements when the scales are stacked and data is strings', function() {
var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
data: ['10', '-10', '10', '-10'],
label: 'dataset1'
}, {
data: ['10', '15', '0', '-4'],
label: 'dataset2'
}],
labels: ['label1', 'label2', 'label3', 'label4']
},
options: {
scales: {
xAxes: [{
type: 'category',
stacked: true
}],
yAxes: [{
type: 'linear',
stacked: true
}]
}
}
});

var meta0 = chart.getDatasetMeta(0);

[ { b: 290, w: 91, x: 95, y: 161 },
{ b: 290, w: 91, x: 209, y: 419 },
{ b: 290, w: 91, x: 322, y: 161 },
{ b: 290, w: 91, x: 436, y: 419 }
].forEach(function(values, i) {
expect(meta0.data[i]._model.base).toBeCloseToPixel(values.b);
expect(meta0.data[i]._model.width).toBeCloseToPixel(values.w);
expect(meta0.data[i]._model.x).toBeCloseToPixel(values.x);
expect(meta0.data[i]._model.y).toBeCloseToPixel(values.y);
});

var meta1 = chart.getDatasetMeta(1);

[ { b: 161, w: 91, x: 95, y: 32 },
{ b: 290, w: 91, x: 209, y: 97 },
{ b: 161, w: 91, x: 322, y: 161 },
{ b: 419, w: 91, x: 436, y: 471 }
].forEach(function(values, i) {
expect(meta1.data[i]._model.base).toBeCloseToPixel(values.b);
expect(meta1.data[i]._model.width).toBeCloseToPixel(values.w);
expect(meta1.data[i]._model.x).toBeCloseToPixel(values.x);
expect(meta1.data[i]._model.y).toBeCloseToPixel(values.y);
});
});

it('should draw all bars', function() {
var chart = window.acquireChart({
type: 'bar',
Expand Down
46 changes: 46 additions & 0 deletions test/controller.line.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,52 @@ describe('Line controller tests', function() {

});

it('should update elements when the y scale is stacked and data is strings', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
data: ['10', '-10', '10', '-10'],
label: 'dataset1'
}, {
data: ['10', '15', '0', '-4'],
label: 'dataset2'
}],
labels: ['label1', 'label2', 'label3', 'label4']
},
options: {
scales: {
yAxes: [{
stacked: true
}]
}
}
});

var meta0 = chart.getDatasetMeta(0);

[ { x: 38, y: 161 },
{ x: 189, y: 419 },
{ x: 341, y: 161 },
{ x: 492, y: 419 }
].forEach(function(values, i) {
expect(meta0.data[i]._model.x).toBeCloseToPixel(values.x);
expect(meta0.data[i]._model.y).toBeCloseToPixel(values.y);
});

var meta1 = chart.getDatasetMeta(1);

[ { x: 38, y: 32 },
{ x: 189, y: 97 },
{ x: 341, y: 161 },
{ x: 492, y: 471 }
].forEach(function(values, i) {
expect(meta1.data[i]._model.x).toBeCloseToPixel(values.x);
expect(meta1.data[i]._model.y).toBeCloseToPixel(values.y);
});

});

it('should find the correct scale zero when the data is all positive', function() {
var chart = window.acquireChart({
type: 'line',
Expand Down