From 334ba44e95224dc14ea4919e6c1f467d264e250d Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Sat, 31 Oct 2015 22:45:25 -0600 Subject: [PATCH] Allow line controller to calculate stacked values --- src/controllers/controller.line.js | 35 +++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/controllers/controller.line.js b/src/controllers/controller.line.js index e034e325c..954686e06 100644 --- a/src/controllers/controller.line.js +++ b/src/controllers/controller.line.js @@ -193,7 +193,7 @@ // 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), @@ -208,6 +208,39 @@ 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) {