From ac5be9bde20aa148eab929b2f356834150dec447 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Wed, 17 Feb 2016 12:41:32 -0700 Subject: [PATCH 1/3] Proper line and bar stacking order Stacked line and bar charts now behave predictably with the first dataset on the bottom stacked upwards. --- src/controllers/controller.line.js | 4 ++-- src/core/core.controller.js | 4 ++-- test/controller.line.tests.js | 32 ++++++++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/controllers/controller.line.js b/src/controllers/controller.line.js index 381224c031e..dcce78f995e 100644 --- a/src/controllers/controller.line.js +++ b/src/controllers/controller.line.js @@ -205,7 +205,7 @@ module.exports = function(Chart) { var sumPos = 0, sumNeg = 0; - for (var i = this.chart.data.datasets.length - 1; i > datasetIndex; i--) { + for (var i = 0; i < datasetIndex; i++) { var ds = this.chart.data.datasets[i]; if (ds.type === 'line' && helpers.isDatasetVisible(ds)) { if (ds.data[index] < 0) { @@ -287,4 +287,4 @@ module.exports = function(Chart) { point._model.borderWidth = this.getPointBorderWidth(point, index); } }); -}; \ No newline at end of file +}; diff --git a/src/core/core.controller.js b/src/core/core.controller.js index b1dc8351b26..fc5bbdedc5c 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -314,7 +314,7 @@ module.exports = function(Chart) { if (helpers.isDatasetVisible(dataset)) { dataset.controller.draw(ease); } - }); + }, null, true); // Finally draw the tooltip this.tooltip.transition(easingDecimal).draw(); @@ -557,4 +557,4 @@ module.exports = function(Chart) { return this; } }); -}; \ No newline at end of file +}; diff --git a/test/controller.line.tests.js b/test/controller.line.tests.js index 40c7c2bb053..0b8cc79218d 100644 --- a/test/controller.line.tests.js +++ b/test/controller.line.tests.js @@ -655,7 +655,9 @@ describe('Line controller tests', function() { }; var controller = new Chart.controllers.line(chart, 0); + var controller2 = new Chart.controllers.line(chart, 1); controller.update(); + controller2.update(); // Line element expect(chart.data.datasets[0].metaDataset._model).toEqual(jasmine.objectContaining({ @@ -667,13 +669,13 @@ describe('Line controller tests', function() { expect(chart.data.datasets[0].metaData[0]._model).toEqual(jasmine.objectContaining({ // Point x: 91, - y: 30, + y: 77, })); expect(chart.data.datasets[0].metaData[1]._model).toEqual(jasmine.objectContaining({ // Point x: 141, - y: 18, + y: 65, })); expect(chart.data.datasets[0].metaData[2]._model).toEqual(jasmine.objectContaining({ @@ -683,10 +685,36 @@ describe('Line controller tests', function() { })); expect(chart.data.datasets[0].metaData[3]._model).toEqual(jasmine.objectContaining({ + // Point + x: 242, + y: 109, + })); + + expect(chart.data.datasets[1].metaData[0]._model).toEqual(jasmine.objectContaining({ + // Point + x: 91, + y: 30, + })); + + expect(chart.data.datasets[1].metaData[1]._model).toEqual(jasmine.objectContaining({ + // Point + x: 141, + y: 18, + })); + + expect(chart.data.datasets[1].metaData[2]._model).toEqual(jasmine.objectContaining({ + // Point + x: 192, + y: 30, + })); + + expect(chart.data.datasets[1].metaData[3]._model).toEqual(jasmine.objectContaining({ // Point x: 242, y: 180, })); + + }); it('should find the correct scale zero when the data is all positive', function() { From ee81d4a8046167ce55a8444a617cfc9e1e6b48b3 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Wed, 17 Feb 2016 12:42:09 -0700 Subject: [PATCH 2/3] Reversible Legends Legends can now be reversed with the `reversed: true` property. --- src/core/core.legend.js | 6 +++++- test/core.legend.tests.js | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/core/core.legend.js b/src/core/core.legend.js index e42db5825b6..d695f2df5fc 100644 --- a/src/core/core.legend.js +++ b/src/core/core.legend.js @@ -9,6 +9,7 @@ module.exports = function(Chart) { display: true, position: 'top', fullWidth: true, // marks that this box should take the full width of the canvas (pushing down other boxes) + reverse: false, // a callback that will handle onClick: function(e, legendItem) { @@ -143,6 +144,9 @@ module.exports = function(Chart) { beforeBuildLabels: helpers.noop, buildLabels: function() { this.legendItems = this.options.labels.generateLabels.call(this, this.chart.data); + if(this.options.reverse){ + this.legendItems.reverse(); + } }, afterBuildLabels: helpers.noop, @@ -321,4 +325,4 @@ module.exports = function(Chart) { } }); -}; \ No newline at end of file +}; diff --git a/test/core.legend.tests.js b/test/core.legend.tests.js index fc83e63ce44..1e411827a27 100644 --- a/test/core.legend.tests.js +++ b/test/core.legend.tests.js @@ -11,6 +11,7 @@ describe('Legend block tests', function() { display: true, position: 'top', fullWidth: true, // marks that this box should take the full width of the canvas (pushing down other boxes) + reverse: false, // a callback that will handle onClick: jasmine.any(Function), @@ -305,4 +306,4 @@ describe('Legend block tests', function() { "args": ["dataset3", 228, 132] }]); }); -}); \ No newline at end of file +}); From 50d0c980304730096a23fdaa4b2e77b95cdef25b Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Wed, 17 Feb 2016 12:42:29 -0700 Subject: [PATCH 3/3] Tooltips now respect stacked ordering Tooltips detect the stacked scale property now, reversing when appropriate --- src/core/core.tooltip.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/core.tooltip.js b/src/core/core.tooltip.js index b99d05c8725..ae3e2d58aa7 100644 --- a/src/core/core.tooltip.js +++ b/src/core/core.tooltip.js @@ -253,7 +253,7 @@ module.exports = function(Chart) { datasetIndex: datasetIndex }); } - }); + }, null, element._yScale.options.stacked); helpers.each(this._active, function(active) { if (active) { @@ -262,7 +262,7 @@ module.exports = function(Chart) { backgroundColor: active._view.backgroundColor }); } - }); + }, null, element._yScale.options.stacked); tooltipPosition = this.getAveragePosition(this._active); tooltipPosition.y = this._active[0]._yScale.getPixelForDecimal(0.5); @@ -609,4 +609,4 @@ module.exports = function(Chart) { } } }); -}; \ No newline at end of file +};