-
Notifications
You must be signed in to change notification settings - Fork 12k
Rewrite animation logic #6845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite animation logic #6845
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| extends: chartjs | ||
|
|
||
| env: | ||
| es6: true | ||
| browser: true | ||
| node: true | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,32 +17,23 @@ This must be called before the canvas is reused for a new chart. | |
| myLineChart.destroy(); | ||
| ``` | ||
|
|
||
| ## .update(config) | ||
| ## .update(mode) | ||
|
|
||
| Triggers an update of the chart. This can be safely called after updating the data object. This will update all scales, legends, and then re-render the chart. | ||
|
|
||
| ```javascript | ||
| // duration is the time for the animation of the redraw in milliseconds | ||
| // lazy is a boolean. if true, the animation can be interrupted by other animations | ||
| myLineChart.data.datasets[0].data[2] = 50; // Would update the first dataset's value of 'March' to be 50 | ||
| myLineChart.update(); // Calling update now animates the position of March from 90 to 50. | ||
| ``` | ||
|
|
||
| > **Note:** replacing the data reference (e.g. `myLineChart.data = {datasets: [...]}` only works starting **version 2.6**. Prior that, replacing the entire data object could be achieved with the following workaround: `myLineChart.config.data = {datasets: [...]}`. | ||
|
|
||
| A `config` object can be provided with additional configuration for the update process. This is useful when `update` is manually called inside an event handler and some different animation is desired. | ||
|
|
||
| The following properties are supported: | ||
| * **duration** (number): Time for the animation of the redraw in milliseconds | ||
| * **lazy** (boolean): If true, the animation can be interrupted by other animations | ||
| * **easing** (string): The animation easing function. See [Animation Easing](../configuration/animations.md) for possible values. | ||
| A `mode` string can be provided to indicate what should be updated and what animation configuration should be used. Core calls this method using any of `undefined`, `'reset'`, `'resize'` or `'active'`. `'none'` is also a supported mode for skipping animations for single update. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its the default/normal update. Uses configure animations.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reset: function() {
this._update('reset');
}, |
||
|
|
||
| Example: | ||
|
|
||
| ```javascript | ||
| myChart.update({ | ||
| duration: 800, | ||
| easing: 'easeOutBounce' | ||
| }); | ||
| myChart.update(); | ||
| ``` | ||
|
|
||
| See [Updating Charts](updates.md) for more details. | ||
|
|
@@ -55,25 +46,13 @@ Reset the chart to it's state before the initial animation. A new animation can | |
| myLineChart.reset(); | ||
| ``` | ||
|
|
||
| ## .render(config) | ||
| ## .render() | ||
|
|
||
| Triggers a redraw of all chart elements. Note, this does not update elements for new data. Use `.update()` in that case. | ||
|
|
||
| See `.update(config)` for more details on the config object. | ||
|
|
||
| ```javascript | ||
| // duration is the time for the animation of the redraw in milliseconds | ||
| // lazy is a boolean. if true, the animation can be interrupted by other animations | ||
| myLineChart.render({ | ||
| duration: 800, | ||
| lazy: false, | ||
| easing: 'easeOutBounce' | ||
| }); | ||
| ``` | ||
|
|
||
| ## .stop() | ||
|
|
||
| Use this to stop any current animation loop. This will pause the chart during any current animation frame. Call `.render()` to re-animate. | ||
| Use this to stop any current animation. This will pause the chart during any current animation frame. Call `.render()` to re-animate. | ||
|
|
||
| ```javascript | ||
| // Stops the charts animation loop at its current frame | ||
|
|
@@ -175,5 +154,5 @@ Extensive examples of usage are available in the [Chart.js tests](https://github | |
|
|
||
| ```javascript | ||
| var meta = myChart.getDatasetMeta(0); | ||
| var x = meta.data[0]._model.x; | ||
| var x = meta.data[0].x; | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| <!doctype html> | ||
| <html> | ||
|
|
||
| <head> | ||
| <title>Stacked Bar Chart</title> | ||
| <script src="../../dist/Chart.min.js"></script> | ||
| <script src="../utils.js"></script> | ||
| <style> | ||
| canvas { | ||
| -moz-user-select: none; | ||
| -webkit-user-select: none; | ||
| -ms-user-select: none; | ||
| } | ||
| </style> | ||
| </head> | ||
|
|
||
| <body> | ||
| <div style="width: 75%"> | ||
| <canvas id="canvas"></canvas> | ||
| </div> | ||
| <button id="randomizeData">Randomize Data</button> | ||
| <script> | ||
| var barChartData = { | ||
| labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], | ||
| datasets: [{ | ||
| label: 'Dataset 1', | ||
| backgroundColor: window.chartColors.red, | ||
| data: [ | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor() | ||
| ] | ||
| }, { | ||
| label: 'Dataset 2', | ||
| backgroundColor: window.chartColors.blue, | ||
| data: [ | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor() | ||
| ] | ||
| }, { | ||
| label: 'Dataset 3', | ||
| backgroundColor: window.chartColors.green, | ||
| data: [ | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor(), | ||
| randomScalingFactor() | ||
| ] | ||
| }] | ||
|
|
||
| }; | ||
| window.onload = function() { | ||
| var ctx = document.getElementById('canvas').getContext('2d'); | ||
| var started = {}; | ||
| window.myBar = new Chart(ctx, { | ||
| type: 'bar', | ||
| data: barChartData, | ||
| options: { | ||
| animation: (context) => { | ||
| if (context.active) { | ||
| return { | ||
| duration: 400 | ||
| }; | ||
| } | ||
| var delay = 0; | ||
| var dsIndex = context.datasetIndex; | ||
| var index = context.dataIndex; | ||
| if (!started[index + dsIndex * 1000]) { | ||
| delay = index * 300 + dsIndex * 100; | ||
| started[index + dsIndex * 1000] = true; | ||
| } | ||
| return { | ||
| easing: 'linear', | ||
| duration: 600, | ||
| delay | ||
| }; | ||
| }, | ||
| title: { | ||
| display: true, | ||
| text: 'Chart.js Bar Chart - Stacked' | ||
| }, | ||
| tooltips: { | ||
| mode: 'index', | ||
| intersect: false | ||
| }, | ||
| responsive: true, | ||
| scales: { | ||
| x: { | ||
| stacked: true, | ||
| }, | ||
| y: { | ||
| stacked: true | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| document.getElementById('randomizeData').addEventListener('click', function() { | ||
| barChartData.datasets.forEach(function(dataset) { | ||
| dataset.data = dataset.data.map(function() { | ||
| return randomScalingFactor(); | ||
| }); | ||
| }); | ||
| window.myBar.update(); | ||
| }); | ||
| </script> | ||
| </body> | ||
|
|
||
| </html> |
Uh oh!
There was an error while loading. Please reload this page.