Skip to content
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
12 changes: 10 additions & 2 deletions src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 +
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 +
Expand Down
55 changes: 55 additions & 0 deletions test/controller.bar.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down