]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Allow line controller to calculate stacked values 1600/head
authorTanner Linsley <tannerlinsley@gmail.com>
Sun, 1 Nov 2015 04:45:25 +0000 (22:45 -0600)
committerTanner Linsley <tannerlinsley@gmail.com>
Sun, 1 Nov 2015 04:45:25 +0000 (22:45 -0600)
src/controllers/controller.line.js

index e034e325cdf30ac8244c93dadaae5f03bd46813b..954686e06af39fffff5e006f0aca43db6e68ff5e 100644 (file)
                                // Desired view properties
                                _model: {
                                        x: xScale.getPixelForValue(this.getDataset().data[index], index, this.index, this.chart.isCombo),
-                                       y: reset ? scaleBase : yScale.getPixelForValue(this.getDataset().data[index], index, this.index),
+                                       y: reset ? scaleBase : this.calculatePointY(this.getDataset().data[index], index, this.index, this.chart.isCombo),
                                        // Appearance
                                        tension: point.custom && point.custom.tension ? point.custom.tension : (this.getDataset().tension || this.chart.options.elements.line.tension),
                                        radius: point.custom && point.custom.radius ? point.custom.radius : helpers.getValueAtIndexOrDefault(this.getDataset().radius, index, this.chart.options.elements.point.radius),
                        point._model.skip = point.custom && point.custom.skip ? point.custom.skip : (isNaN(point._model.x) || isNaN(point._model.y));
                },
 
+               calculatePointY: function(value, index, datasetIndex, isCombo) {
+
+                       var xScale = this.getScaleForId(this.getDataset().xAxisID);
+                       var yScale = this.getScaleForId(this.getDataset().yAxisID);
+
+                       if (yScale.options.stacked) {
+
+                               var sumPos = 0,
+                                       sumNeg = 0;
+
+                               for (var i = this.chart.data.datasets.length - 1; i > datasetIndex; i--) {
+                                       var ds = this.chart.data.datasets[i];
+                                       if (helpers.isDatasetVisible(ds)) {
+                                               if (ds.data[index] < 0) {
+                                                       sumNeg += ds.data[index] || 0;
+                                               } else {
+                                                       sumPos += ds.data[index] || 0;
+                                               }
+                                       }
+                               }
+
+                               if (value < 0) {
+                                       return yScale.getPixelForValue(sumNeg + value);
+                               } else {
+                                       return yScale.getPixelForValue(sumPos + value);
+                               }
+
+                               return yScale.getPixelForValue(value);
+                       }
+
+                       return yScale.getPixelForValue(value);
+               },
+
                updateBezierControlPoints: function() {
                        // Update bezier control points
                        helpers.each(this.getDataset().metaData, function(point, index) {