]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Call animation callbacks. Created a sample file that shows their use by updating... 1775/head
authorEvert Timberg <evert.timberg@gmail.com>
Sat, 12 Dec 2015 15:01:08 +0000 (10:01 -0500)
committerEvert Timberg <evert.timberg@gmail.com>
Sat, 12 Dec 2015 15:01:08 +0000 (10:01 -0500)
docs/00-Getting-Started.md
samples/AnimationCallbacks/progress-bar.html [new file with mode: 0644]
src/core/core.animation.js
src/core/core.controller.js

index 59d6474621bccc5a882f26874f2215e3eb9ee9d7..bb77a218352d8fcc016ce2e9b793efe7b7625969 100644 (file)
@@ -186,9 +186,9 @@ Name | Type | Default | Description
 --- |:---:| --- | ---
 duration | Number | 1000 | The number of milliseconds an animation takes.
 easing | String | "easeOutQuart" | Easing function to use. 
-onProgress | Function | none |
-onComplete | Function |none |
-
+onProgress | Function | none | Callback called on each step of an animation. Passed a single argument, an object, containing the chart instance and an object with details of the animation.
+onComplete | Function | none | Callback called at the end of an animation. Passed the same arguments as `onProgress
+`
 The global options for elements are defined in `Chart.defaults.global.elements`.
 
 Name | Type | Default | Description
diff --git a/samples/AnimationCallbacks/progress-bar.html b/samples/AnimationCallbacks/progress-bar.html
new file mode 100644 (file)
index 0000000..0dbc14d
--- /dev/null
@@ -0,0 +1,161 @@
+<!doctype html>
+<html>
+<head>
+       <title> Animation Callbacks </title>
+    <script src="../../Chart.js"></script>
+    <script src="../../node_modules/jquery/dist/jquery.min.js"></script>
+    <style>
+        canvas {
+            -webkit-box-shadow: 0 0 20px 0 rgba(0, 0, 0, .5);
+        }
+    </style>
+</head>
+
+<body>
+    <div style="width: 100 %;">
+        <canvas id="canvas" style="width: 100 % ;height: 100 %"></canvas>
+        <progress id="animationProgress" max="1" value="0" style="width: 100%"></progress>
+    </div>
+    <br>
+    <br>
+    <button id="randomizeData">Randomize Data</button>
+    <button id="addDataset">Add Dataset</button>
+    <button id="removeDataset">Remove Dataset</button>
+    <button id="addData">Add Data</button>
+    <button id="removeData">Remove Data</button>
+    <script>
+        var randomScalingFactor = function() {
+            return Math.round(Math.random() * 100);
+        };
+        var randomColorFactor = function() {
+            return Math.round(Math.random() * 255);
+        };
+        var randomColor = function(opacity) {
+            return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
+        };
+
+        var $progress = $('#animationProgress');
+
+        var config = {
+            type: 'line',
+            data: {
+                labels: ["January", "February", "March", "April", "May", "June", "July"],
+                datasets: [{
+                    label: "My First dataset ",
+                    data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
+                    fill: false,
+                    borderDash: [5, 5],
+                }, {
+                    label: "My Second dataset ",
+                    data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
+                }]
+            },
+            options: {
+                animation: {
+                    duration: 2000,
+                    onProgress: function(animation) {
+                        $progress.attr({
+                            value: animation.animationObject.currentStep / animation.animationObject.numSteps,
+                        });
+                    },
+                    onComplete: function(animation) {
+                        window.setTimeout(function() {
+                            $progress.attr({
+                                value: 0
+                            });
+                        }, 2000);
+                    }
+                },
+                tooltips: {
+                    mode: 'label',
+                },
+                scales: {
+                    xAxes: [{
+                        scaleLabel: {
+                            show: true,
+                            labelString: 'Month'
+                        }
+                    }],
+                    yAxes: [{
+                        scaleLabel: {
+                            show: true,
+                            labelString: 'Value'
+                        },
+                    }]
+                }
+            }
+        };
+
+        $.each(config.data.datasets, function(i, dataset) {
+            dataset.borderColor = randomColor(0.4);
+            dataset.backgroundColor = randomColor(0.5);
+            dataset.pointBorderColor = randomColor(0.7);
+            dataset.pointBackgroundColor = randomColor(0.5);
+            dataset.pointBorderWidth = 1;
+        });
+
+        window.onload = function() {
+            var ctx = document.getElementById("canvas").getContext("2d");
+            window.myLine = new Chart(ctx, config);
+        };
+
+        $('#randomizeData').click(function() {
+            $.each(config.data.datasets, function(i, dataset) {
+                dataset.data = dataset.data.map(function() {
+                    return randomScalingFactor();
+                });
+
+            });
+
+            window.myLine.update();
+        });
+
+        $('#addDataset').click(function() {
+            var newDataset = {
+                label: 'Dataset ' + config.data.datasets.length,
+                borderColor: randomColor(0.4),
+                backgroundColor: randomColor(0.5),
+                pointBorderColor: randomColor(0.7),
+                pointBackgroundColor: randomColor(0.5),
+                pointBorderWidth: 1,
+                data: [],
+            };
+
+            for (var index = 0; index < config.data.labels.length; ++index) {
+                newDataset.data.push(randomScalingFactor());
+            }
+
+            config.data.datasets.push(newDataset);
+            window.myLine.update();
+        });
+
+        $('#addData').click(function() {
+            if (config.data.datasets.length > 0) {
+                config.data.labels.push('dataset #' + config.data.labels.length);
+
+                $.each(config.data.datasets, function(i, dataset) {
+                    dataset.data.push(randomScalingFactor());
+                });
+
+                window.myLine.update();
+            }
+        });
+
+        $('#removeDataset').click(function() {
+            config.data.datasets.splice(0, 1);
+            window.myLine.update();
+        });
+
+        $('#removeData').click(function() {
+            config.data.labels.splice(-1, 1); // remove the label first
+
+            config.data.datasets.forEach(function(dataset, datasetIndex) {
+                dataset.data.pop();
+            });
+
+            window.myLine.update();
+        });
+    </script>
+</body>
+
+</html>
\ No newline at end of file
index cb6c6fc0ad10f98b16e2893c95748e5e02b61a16..e874155a22475e5df8a8eba31e562c8359e1bb4d 100644 (file)
@@ -9,8 +9,8 @@
        Chart.defaults.global.animation = {
                duration: 1000,
                easing: "easeOutQuart",
-               onProgress: function() {},
-               onComplete: function() {},
+               onProgress: helpers.noop,
+               onComplete: helpers.noop,
        };
 
        Chart.Animation = Chart.Element.extend({
                        }
 
                        for (var i = 0; i < this.animations.length; i++) {
-
                                if (this.animations[i].animationObject.currentStep === null) {
                                        this.animations[i].animationObject.currentStep = 0;
                                }
+
                                this.animations[i].animationObject.currentStep += 1 + framesToDrop;
+
                                if (this.animations[i].animationObject.currentStep > this.animations[i].animationObject.numSteps) {
                                        this.animations[i].animationObject.currentStep = this.animations[i].animationObject.numSteps;
                                }
 
                                this.animations[i].animationObject.render(this.animations[i].chartInstance, this.animations[i].animationObject);
+                               if (this.animations[i].animationObject.onAnimationProgress && this.animations[i].animationObject.onAnimationProgress.call) {
+                                       this.animations[i].animationObject.onAnimationProgress.call(this.animations[i].chartInstance, this.animations[i]);
+                               }
 
                                if (this.animations[i].animationObject.currentStep == this.animations[i].animationObject.numSteps) {
+                                       if (this.animations[i].animationObject.onAnimationComplete && this.animations[i].animationObject.onAnimationComplete.call) {
+                                               this.animations[i].animationObject.onAnimationComplete.call(this.animations[i].chartInstance, this.animations[i]);
+                                       }
+                                       
                                        // executed the last frame. Remove the animation.
                                        this.animations[i].chartInstance.animating = false;
                                        this.animations.splice(i, 1);
index 9a8304b3d5d287c758c66b0ae8682110c42798ca..d57ebcc8aa34e6532c4984f1b58afb2a12c9d34d 100644 (file)
 
                render: function render(duration, lazy) {
 
-                       if (this.options.animation !== false && ((typeof duration !== 'undefined' && duration !== 0) || (typeof duration == 'undefined' && this.options.animation.duration !== 0))) {
+                       if (this.options.animation && ((typeof duration !== 'undefined' && duration !== 0) || (typeof duration == 'undefined' && this.options.animation.duration !== 0))) {
                                var animation = new Chart.Animation();
                                animation.numSteps = (duration || this.options.animation.duration) / 16.66; //60 fps
                                animation.easing = this.options.animation.easing;
                                };
 
                                // user events
-                               animation.onAnimationProgress = this.options.onAnimationProgress;
-                               animation.onAnimationComplete = this.options.onAnimationComplete;
+                               animation.onAnimationProgress = this.options.animation.onProgress;
+                               animation.onAnimationComplete = this.options.animation.onComplete;
 
                                Chart.animationService.addAnimation(this, animation, duration, lazy);
                        } else {
                                this.draw();
-                               if (this.options.onAnimationComplete && this.options.onAnimationComplete.call) {
-                                       this.options.onAnimationComplete.call(this);
+                               if (this.options.animation && this.options.animation.onComplete && this.options.animation.onComplete.call) {
+                                       this.options.animation.onComplete.call(this);
                                }
                        }
                        return this;