]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
fix stacked scatter line chart 2867/head
authorMitsuhiro Tanda <mitsuhiro.tanda@gmail.com>
Wed, 29 Jun 2016 03:35:52 +0000 (12:35 +0900)
committerMitsuhiro Tanda <mitsuhiro.tanda@gmail.com>
Thu, 30 Jun 2016 13:28:21 +0000 (22:28 +0900)
src/controllers/controller.line.js
test/controller.line.tests.js

index 36b4d8f54973fa45942f80237c71b6c4e24c7f77..f3d7328d431b3ff1a9ea497c9c2b3a9ecce94f88 100644 (file)
@@ -221,18 +221,20 @@ module.exports = function(Chart) {
                                        ds = chart.data.datasets[i];
                                        dsMeta = chart.getDatasetMeta(i);
                                        if (dsMeta.type === 'line' && chart.isDatasetVisible(i)) {
-                                               if (ds.data[index] < 0) {
-                                                       sumNeg += ds.data[index] || 0;
+                                               var stackedRightValue = yScale.getRightValue(ds.data[index]);
+                                               if (stackedRightValue < 0) {
+                                                       sumNeg += stackedRightValue || 0;
                                                } else {
-                                                       sumPos += ds.data[index] || 0;
+                                                       sumPos += stackedRightValue || 0;
                                                }
                                        }
                                }
 
-                               if (value < 0) {
-                                       return yScale.getPixelForValue(sumNeg + value);
+                               var rightValue = yScale.getRightValue(value);
+                               if (rightValue < 0) {
+                                       return yScale.getPixelForValue(sumNeg + rightValue);
                                } else {
-                                       return yScale.getPixelForValue(sumPos + value);
+                                       return yScale.getPixelForValue(sumPos + rightValue);
                                }
                        }
 
index 782cf8f4332112d2026726d51d0e6585daaebbcc..78aecf2d2ad219626a479d8d780d4d6a06b1e29a 100644 (file)
@@ -278,6 +278,76 @@ describe('Line controller tests', function() {
                
        });
 
+       it('should update elements when the y scale is stacked and datasets is scatter data', function() {
+               var chart = window.acquireChart({
+                       type: 'line',
+                       data: {
+                               datasets: [{
+                                       data: [{
+                                               x: 0,
+                                               y: 10
+                                       }, {
+                                               x: 1,
+                                               y: -10
+                                       }, {
+                                               x: 2,
+                                               y: 10
+                                       }, {
+                                               x: 3,
+                                               y: -10
+                                       }],
+                                       label: 'dataset1'
+                               }, {
+                                       data: [{
+                                               x: 0,
+                                               y: 10
+                                       }, {
+                                               x: 1,
+                                               y: 15
+                                       }, {
+                                               x: 2,
+                                               y: 0
+                                       }, {
+                                               x: 3,
+                                               y: -4
+                                       }],
+                                       label: 'dataset2'
+                               }],
+                               labels: ['label1', 'label2', 'label3', 'label4']
+                       },
+                       options: {
+                               scales: {
+                                       yAxes: [{
+                                               stacked: true
+                                       }]
+                               }
+                       }
+               });
+               
+               var meta0 = chart.getDatasetMeta(0);
+
+               [       { x:  38, y: 161 },
+                       { x: 189, y: 419 },
+                       { x: 341, y: 161 },
+                       { x: 492, y: 419 }
+               ].forEach(function(values, i) {
+                               expect(meta0.data[i]._model.x).toBeCloseToPixel(values.x);
+                               expect(meta0.data[i]._model.y).toBeCloseToPixel(values.y);
+               });
+
+               var meta1 = chart.getDatasetMeta(1);
+
+               [       { x:  38, y:  32 },
+                       { x: 189, y:  97 },
+                       { x: 341, y: 161 },
+                       { x: 492, y: 471 }
+               ].forEach(function(values, i) {
+                               expect(meta1.data[i]._model.x).toBeCloseToPixel(values.x);
+                               expect(meta1.data[i]._model.y).toBeCloseToPixel(values.y);
+               });
+               
+       });
+
        it('should find the correct scale zero when the data is all positive', function() {
                var chart = window.acquireChart({
                        type: 'line',