]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add reset method to chart prototype
authoretimberg <evert.timberg@gmail.com>
Sun, 16 Oct 2016 13:28:27 +0000 (09:28 -0400)
committerEvert Timberg <evert.timberg+github@gmail.com>
Mon, 17 Oct 2016 11:40:29 +0000 (06:40 -0500)
docs/09-Advanced.md
src/core/core.controller.js
test/core.controller.tests.js

index 3a361bcfff3a8d170b9cf5b22e444919864ca15e..5ea957179a6d0fbc6ffd05c813a92b338963ed9e 100644 (file)
@@ -34,6 +34,14 @@ myLineChart.data.datasets[0].data[2] = 50; // Would update the first dataset's v
 myLineChart.update(); // Calling update now animates the position of March from 90 to 50.
 ```
 
+#### .reset()
+
+Reset the chart to it's state before the initial animation. A new animation can then be triggered using `update`.
+
+```javascript
+myLineChart.reset();
+```
+
 #### .render(duration, lazy)
 
 Triggers a redraw of all chart elements. Note, this does not update elements for new data. Use `.update()` in that case.
index c08d0192864588ddc6d43661a0562cb140200b2d..3fb4ff1c9955abf1b409c0a99723e18faf7d32f4 100644 (file)
@@ -409,6 +409,11 @@ module.exports = function(Chart) {
                        return newControllers;
                },
 
+               /**
+                * Reset the elements of all datasets
+                * @method resetElements
+                * @private
+                */
                resetElements: function() {
                        var me = this;
                        helpers.each(me.data.datasets, function(dataset, datasetIndex) {
@@ -416,6 +421,15 @@ module.exports = function(Chart) {
                        }, me);
                },
 
+               /**
+               * Resets the chart back to it's state before the initial animation
+               * @method reset
+               */
+               reset: function() {
+                       this.resetElements();
+                       this.tooltip.initialize();
+               },
+
                update: function(animationDuration, lazy) {
                        var me = this;
                        Chart.plugins.notify('beforeUpdate', [me]);
index a7b504ecdcce9b3bf7ade90dfc44ccff7723bc16..1700805596e482c7b82990a84f85e4956b2ce502 100644 (file)
@@ -666,4 +666,38 @@ describe('Chart.Controller', function() {
                        expect(wrapper.firstChild.tagName).toBe('CANVAS');
                });
        });
+
+       describe('controller.reset', function() {
+               it('should reset the chart elements', function() {
+                       var chart = acquireChart({
+                               type: 'line',
+                               data: {
+                                       labels: ['A', 'B', 'C', 'D'],
+                                       datasets: [{
+                                               data: [10, 20, 30, 0]
+                                       }]
+                               },
+                               options: {
+                                       responsive: true
+                               }
+                       });
+
+                       var meta = chart.getDatasetMeta(0);
+
+                       // Verify that points are at their initial correct location,
+                       // then we will reset and see that they moved
+                       expect(meta.data[0]._model.y).toBe(333);
+                       expect(meta.data[1]._model.y).toBe(183);
+                       expect(meta.data[2]._model.y).toBe(32);
+                       expect(meta.data[3]._model.y).toBe(484);
+
+                       chart.reset();
+
+                       // For a line chart, the animation state is the bottom
+                       expect(meta.data[0]._model.y).toBe(484);
+                       expect(meta.data[1]._model.y).toBe(484);
+                       expect(meta.data[2]._model.y).toBe(484);
+                       expect(meta.data[3]._model.y).toBe(484);
+               });
+       });
 });