]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
#3849 - Stack bars in z dimension
authorpotatopeelings <potato.peelings@gmail.com>
Sun, 12 Feb 2017 11:59:20 +0000 (22:59 +1100)
committerEvert Timberg <evert.timberg+github@gmail.com>
Thu, 16 Feb 2017 22:58:18 +0000 (17:58 -0500)
src/controllers/controller.bar.js
test/controller.bar.tests.js

index cd746701e051add5a8a628f454540e265bceb90e..0aa35a2e3a95b59ebe9457ed5e7a20b087ae4375 100644 (file)
@@ -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 +
index 38f1b733a1add2f9e40a3ffda2871ee882be8dfc..1a08376704674dc0d3faef433298cbebdf8c064c 100644 (file)
@@ -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',