doughnut.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Doughnut Chart</title>
  5. <script src="../../dist/Chart.bundle.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 id="canvas-holder" style="width:40%">
  17. <canvas id="chart-area"></canvas>
  18. </div>
  19. <button id="randomizeData">Randomize Data</button>
  20. <button id="addDataset">Add Dataset</button>
  21. <button id="removeDataset">Remove Dataset</button>
  22. <button id="addData">Add Data</button>
  23. <button id="removeData">Remove Data</button>
  24. <script>
  25. var randomScalingFactor = function() {
  26. return Math.round(Math.random() * 100);
  27. };
  28. var config = {
  29. type: 'doughnut',
  30. data: {
  31. datasets: [{
  32. data: [
  33. randomScalingFactor(),
  34. randomScalingFactor(),
  35. randomScalingFactor(),
  36. randomScalingFactor(),
  37. randomScalingFactor(),
  38. ],
  39. backgroundColor: [
  40. window.chartColors.red,
  41. window.chartColors.orange,
  42. window.chartColors.yellow,
  43. window.chartColors.green,
  44. window.chartColors.blue,
  45. ],
  46. label: 'Dataset 1'
  47. }],
  48. labels: [
  49. 'Red',
  50. 'Orange',
  51. 'Yellow',
  52. 'Green',
  53. 'Blue'
  54. ]
  55. },
  56. options: {
  57. responsive: true,
  58. legend: {
  59. position: 'top',
  60. },
  61. title: {
  62. display: true,
  63. text: 'Chart.js Doughnut Chart'
  64. },
  65. animation: {
  66. animateScale: true,
  67. animateRotate: true
  68. }
  69. }
  70. };
  71. window.onload = function() {
  72. var ctx = document.getElementById('chart-area').getContext('2d');
  73. window.myDoughnut = new Chart(ctx, config);
  74. };
  75. document.getElementById('randomizeData').addEventListener('click', function() {
  76. config.data.datasets.forEach(function(dataset) {
  77. dataset.data = dataset.data.map(function() {
  78. return randomScalingFactor();
  79. });
  80. });
  81. window.myDoughnut.update();
  82. });
  83. var colorNames = Object.keys(window.chartColors);
  84. document.getElementById('addDataset').addEventListener('click', function() {
  85. var newDataset = {
  86. backgroundColor: [],
  87. data: [],
  88. label: 'New dataset ' + config.data.datasets.length,
  89. };
  90. for (var index = 0; index < config.data.labels.length; ++index) {
  91. newDataset.data.push(randomScalingFactor());
  92. var colorName = colorNames[index % colorNames.length];
  93. var newColor = window.chartColors[colorName];
  94. newDataset.backgroundColor.push(newColor);
  95. }
  96. config.data.datasets.push(newDataset);
  97. window.myDoughnut.update();
  98. });
  99. document.getElementById('addData').addEventListener('click', function() {
  100. if (config.data.datasets.length > 0) {
  101. config.data.labels.push('data #' + config.data.labels.length);
  102. var colorName = colorNames[config.data.datasets[0].data.length % colorNames.length];
  103. var newColor = window.chartColors[colorName];
  104. config.data.datasets.forEach(function(dataset) {
  105. dataset.data.push(randomScalingFactor());
  106. dataset.backgroundColor.push(newColor);
  107. });
  108. window.myDoughnut.update();
  109. }
  110. });
  111. document.getElementById('removeDataset').addEventListener('click', function() {
  112. config.data.datasets.splice(0, 1);
  113. window.myDoughnut.update();
  114. });
  115. document.getElementById('removeData').addEventListener('click', function() {
  116. config.data.labels.splice(-1, 1); // remove the label first
  117. config.data.datasets.forEach(function(dataset) {
  118. dataset.data.pop();
  119. dataset.backgroundColor.pop();
  120. });
  121. window.myDoughnut.update();
  122. });
  123. </script>
  124. </body>
  125. </html>