diff --git a/src/controllers/controller.bar.js b/src/controllers/controller.bar.js index cd746701e05..0aa35a2e3a9 100644 --- a/src/controllers/controller.bar.js +++ b/src/controllers/controller.bar.js @@ -169,7 +169,7 @@ module.exports = function(Chart) { if (xScale.options.barThickness) { return xScale.options.barThickness; } - return ruler.barWidth; + return xScale.options.stacked ? ruler.categoryWidth * xScale.options.barPercentage : ruler.barWidth; }, // Get stack index from the given dataset index accounting for stacks and the fact that not all bars are visible @@ -201,6 +201,10 @@ module.exports = function(Chart) { var leftTick = xScale.getPixelForValue(null, index, datasetIndex, me.chart.isCombo); leftTick -= me.chart.isCombo ? (ruler.tickWidth / 2) : 0; + if (xScale.options.stacked) { + return leftTick + (ruler.categoryWidth / 2) + ruler.categorySpacing; + } + return leftTick + (ruler.barWidth / 2) + ruler.categorySpacing + @@ -463,7 +467,7 @@ module.exports = function(Chart) { if (yScale.options.barThickness) { return yScale.options.barThickness; } - return ruler.barHeight; + return yScale.options.stacked ? ruler.categoryHeight * yScale.options.barPercentage : ruler.barHeight; }, // Get stack index from the given dataset index accounting for stacks and the fact that not all bars are visible @@ -530,6 +534,10 @@ module.exports = function(Chart) { var topTick = yScale.getPixelForValue(null, index, datasetIndex, me.chart.isCombo); topTick -= me.chart.isCombo ? (ruler.tickHeight / 2) : 0; + if (yScale.options.stacked) { + return topTick + (ruler.categoryHeight / 2) + ruler.categorySpacing; + } + return topTick + (ruler.barHeight / 2) + ruler.categorySpacing + diff --git a/test/controller.bar.tests.js b/test/controller.bar.tests.js index 38f1b733a1a..1a083767046 100644 --- a/test/controller.bar.tests.js +++ b/test/controller.bar.tests.js @@ -849,6 +849,61 @@ describe('Bar controller tests', function() { }); }); + it('should update elements when only the category scale is stacked', function() { + var chart = window.acquireChart({ + type: 'bar', + data: { + datasets: [{ + data: [20, -10, 10, -10], + label: 'dataset1' + }, { + data: [10, 15, 0, -14], + label: 'dataset2' + }], + labels: ['label1', 'label2', 'label3', 'label4'] + }, + options: { + scales: { + xAxes: [{ + type: 'category', + stacked: true + }], + yAxes: [{ + type: 'linear' + }] + } + } + }); + + var meta0 = chart.getDatasetMeta(0); + + [ + {b: 290, w: 83, x: 86, y: 32}, + {b: 290, w: 83, x: 202, y: 419}, + {b: 290, w: 83, x: 318, y: 161}, + {b: 290, w: 83, x: 434, 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: 290, w: 83, x: 86, y: 161}, + {b: 290, w: 83, x: 202, y: 97}, + {b: 290, w: 83, x: 318, y: 290}, + {b: 290, w: 83, x: 434, 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 update elements when the scales are stacked and data is strings', function() { var chart = window.acquireChart({ type: 'bar',