]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Cast getRightValue to number in the bar controller (#5947)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Wed, 2 Jan 2019 14:55:27 +0000 (16:55 +0200)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Wed, 2 Jan 2019 14:55:27 +0000 (15:55 +0100)
src/controllers/controller.bar.js
test/specs/controller.bar.tests.js

index 760fd8887efc40a4635443fe04a67e74e4cb5490..4188160011058598b23f1120485226e3a743421d 100644 (file)
@@ -311,7 +311,7 @@ module.exports = DatasetController.extend({
                var scale = me.getValueScale();
                var isHorizontal = scale.isHorizontal();
                var datasets = chart.data.datasets;
-               var value = scale.getRightValue(datasets[datasetIndex].data[index]);
+               var value = +scale.getRightValue(datasets[datasetIndex].data[index]);
                var minBarLength = scale.options.minBarLength;
                var stacked = scale.options.stacked;
                var stack = meta.stack;
@@ -327,7 +327,7 @@ module.exports = DatasetController.extend({
                                        imeta.controller.getValueScaleId() === scale.id &&
                                        chart.isDatasetVisible(i)) {
 
-                                       ivalue = scale.getRightValue(datasets[i].data[index]);
+                                       ivalue = +scale.getRightValue(datasets[i].data[index]);
                                        if ((value < 0 && ivalue < 0) || (value >= 0 && ivalue > 0)) {
                                                start += ivalue;
                                        }
@@ -337,7 +337,7 @@ module.exports = DatasetController.extend({
 
                base = scale.getPixelForValue(start);
                head = scale.getPixelForValue(start + value);
-               size = (head - base) / 2;
+               size = head - base;
 
                if (minBarLength !== undefined && Math.abs(size) < minBarLength) {
                        size = minBarLength;
index 62ab0acd87a82a9b92e8769a739504a3f478ae3f..4b33c939c612f90ad7ac3908626d228c277d7e20 100644 (file)
@@ -1259,6 +1259,67 @@ describe('Chart.controllers.bar', function() {
                });
        });
 
+       it('should update elements when the scales are stacked and the y axis is logarithmic and data is strings', function() {
+               var chart = window.acquireChart({
+                       type: 'bar',
+                       data: {
+                               datasets: [{
+                                       data: ['10', '100', '10', '100'],
+                                       label: 'dataset1'
+                               }, {
+                                       data: ['100', '10', '0', '100'],
+                                       label: 'dataset2'
+                               }],
+                               labels: ['label1', 'label2', 'label3', 'label4']
+                       },
+                       options: {
+                               legend: false,
+                               title: false,
+                               scales: {
+                                       xAxes: [{
+                                               type: 'category',
+                                               display: false,
+                                               stacked: true,
+                                               barPercentage: 1,
+                                       }],
+                                       yAxes: [{
+                                               type: 'logarithmic',
+                                               display: false,
+                                               stacked: true
+                                       }]
+                               }
+                       }
+               });
+
+               var meta0 = chart.getDatasetMeta(0);
+
+               [
+                       {b: 512, w: 102, x: 64, y: 512},
+                       {b: 512, w: 102, x: 192, y: 118},
+                       {b: 512, w: 102, x: 320, y: 512},
+                       {b: 512, w: 102, x: 449, y: 118}
+               ].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: 512, w: 102, x: 64, y: 102},
+                       {b: 118, w: 102, x: 192, y: 102},
+                       {b: 512, w: 102, x: 320, y: 512},
+                       {b: 118, w: 102, x: 449, y: 0}
+               ].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',