progress-bar.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title> Animation Callbacks </title>
  5. <script src="../../dist/Chart.min.js"></script>
  6. <script src="../utils.js"></script>
  7. <style>
  8. canvas {
  9. -moz-user-select: none;
  10. -webkit-user-select: none;
  11. -ms-user-select: none;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div style="width: 75%;">
  17. <canvas id="canvas"></canvas>
  18. <progress id="animationProgress" max="1" value="0" style="width: 100%"></progress>
  19. </div>
  20. <br>
  21. <br>
  22. <button id="randomizeData">Randomize Data</button>
  23. <script>
  24. var progress = document.getElementById('animationProgress');
  25. var config = {
  26. type: 'line',
  27. data: {
  28. labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  29. datasets: [{
  30. label: 'My First dataset',
  31. fill: false,
  32. borderColor: window.chartColors.red,
  33. backgroundColor: window.chartColors.red,
  34. data: [
  35. randomScalingFactor(),
  36. randomScalingFactor(),
  37. randomScalingFactor(),
  38. randomScalingFactor(),
  39. randomScalingFactor(),
  40. randomScalingFactor(),
  41. randomScalingFactor()
  42. ]
  43. }, {
  44. label: 'My Second dataset ',
  45. fill: false,
  46. borderColor: window.chartColors.blue,
  47. backgroundColor: window.chartColors.blue,
  48. data: [
  49. randomScalingFactor(),
  50. randomScalingFactor(),
  51. randomScalingFactor(),
  52. randomScalingFactor(),
  53. randomScalingFactor(),
  54. randomScalingFactor(),
  55. randomScalingFactor()
  56. ]
  57. }]
  58. },
  59. options: {
  60. title: {
  61. display: true,
  62. text: 'Chart.js Line Chart - Animation Progress Bar'
  63. },
  64. animation: {
  65. duration: 2000,
  66. onProgress: function(animation) {
  67. progress.value = animation.currentStep / animation.numSteps;
  68. },
  69. onComplete: function() {
  70. window.setTimeout(function() {
  71. progress.value = 0;
  72. }, 2000);
  73. }
  74. }
  75. }
  76. };
  77. window.onload = function() {
  78. var ctx = document.getElementById('canvas').getContext('2d');
  79. window.myLine = new Chart(ctx, config);
  80. };
  81. document.getElementById('randomizeData').addEventListener('click', function() {
  82. config.data.datasets.forEach(function(dataset) {
  83. dataset.data = dataset.data.map(function() {
  84. return randomScalingFactor();
  85. });
  86. });
  87. window.myLine.update();
  88. });
  89. </script>
  90. </body>
  91. </html>