From: potatopeelings Date: Sun, 12 Feb 2017 11:59:20 +0000 (+1100) Subject: #3849 - Stack bars in z dimension X-Git-Tag: v2.6.0~2^2~64 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=85ee592b2a93950bf22503f0e5951627afda5d5e;p=thirdparty%2FChart.js.git #3849 - Stack bars in z dimension --- diff --git a/src/controllers/controller.bar.js b/src/controllers/controller.bar.js index cd746701e..0aa35a2e3 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 38f1b733a..1a0837670 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',