bubble.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Bubble Chart</title>
  5. <script src="../../dist/Chart.bundle.js"></script>
  6. <script src="../utils.js"></script>
  7. <style type="text/css">
  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="container" style="width: 75%;">
  17. <canvas id="canvas"></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 DEFAULT_DATASET_SIZE = 7;
  26. var addedCount = 0;
  27. var color = Chart.helpers.color;
  28. var bubbleChartData = {
  29. animation: {
  30. duration: 10000
  31. },
  32. datasets: [{
  33. label: 'My First dataset',
  34. backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
  35. borderColor: window.chartColors.red,
  36. borderWidth: 1,
  37. data: [{
  38. x: randomScalingFactor(),
  39. y: randomScalingFactor(),
  40. r: Math.abs(randomScalingFactor()) / 5,
  41. }, {
  42. x: randomScalingFactor(),
  43. y: randomScalingFactor(),
  44. r: Math.abs(randomScalingFactor()) / 5,
  45. }, {
  46. x: randomScalingFactor(),
  47. y: randomScalingFactor(),
  48. r: Math.abs(randomScalingFactor()) / 5,
  49. }, {
  50. x: randomScalingFactor(),
  51. y: randomScalingFactor(),
  52. r: Math.abs(randomScalingFactor()) / 5,
  53. }, {
  54. x: randomScalingFactor(),
  55. y: randomScalingFactor(),
  56. r: Math.abs(randomScalingFactor()) / 5,
  57. }, {
  58. x: randomScalingFactor(),
  59. y: randomScalingFactor(),
  60. r: Math.abs(randomScalingFactor()) / 5,
  61. }, {
  62. x: randomScalingFactor(),
  63. y: randomScalingFactor(),
  64. r: Math.abs(randomScalingFactor()) / 5,
  65. }]
  66. }, {
  67. label: 'My Second dataset',
  68. backgroundColor: color(window.chartColors.orange).alpha(0.5).rgbString(),
  69. borderColor: window.chartColors.orange,
  70. borderWidth: 1,
  71. data: [{
  72. x: randomScalingFactor(),
  73. y: randomScalingFactor(),
  74. r: Math.abs(randomScalingFactor()) / 5,
  75. }, {
  76. x: randomScalingFactor(),
  77. y: randomScalingFactor(),
  78. r: Math.abs(randomScalingFactor()) / 5,
  79. }, {
  80. x: randomScalingFactor(),
  81. y: randomScalingFactor(),
  82. r: Math.abs(randomScalingFactor()) / 5,
  83. }, {
  84. x: randomScalingFactor(),
  85. y: randomScalingFactor(),
  86. r: Math.abs(randomScalingFactor()) / 5,
  87. }, {
  88. x: randomScalingFactor(),
  89. y: randomScalingFactor(),
  90. r: Math.abs(randomScalingFactor()) / 5,
  91. }, {
  92. x: randomScalingFactor(),
  93. y: randomScalingFactor(),
  94. r: Math.abs(randomScalingFactor()) / 5,
  95. }, {
  96. x: randomScalingFactor(),
  97. y: randomScalingFactor(),
  98. r: Math.abs(randomScalingFactor()) / 5,
  99. }]
  100. }]
  101. };
  102. window.onload = function() {
  103. var ctx = document.getElementById('canvas').getContext('2d');
  104. window.myChart = new Chart(ctx, {
  105. type: 'bubble',
  106. data: bubbleChartData,
  107. options: {
  108. responsive: true,
  109. title: {
  110. display: true,
  111. text: 'Chart.js Bubble Chart'
  112. },
  113. tooltips: {
  114. mode: 'point'
  115. }
  116. }
  117. });
  118. };
  119. document.getElementById('randomizeData').addEventListener('click', function() {
  120. bubbleChartData.datasets.forEach(function(dataset) {
  121. dataset.data = dataset.data.map(function() {
  122. return {
  123. x: randomScalingFactor(),
  124. y: randomScalingFactor(),
  125. r: Math.abs(randomScalingFactor()) / 5,
  126. };
  127. });
  128. });
  129. window.myChart.update();
  130. });
  131. var colorNames = Object.keys(window.chartColors);
  132. document.getElementById('addDataset').addEventListener('click', function() {
  133. ++addedCount;
  134. var colorName = colorNames[bubbleChartData.datasets.length % colorNames.length];
  135. var dsColor = window.chartColors[colorName];
  136. var newDataset = {
  137. label: 'My added dataset ' + addedCount,
  138. backgroundColor: color(dsColor).alpha(0.5).rgbString(),
  139. borderColor: dsColor,
  140. borderWidth: 1,
  141. data: []
  142. };
  143. for (var index = 0; index < DEFAULT_DATASET_SIZE; ++index) {
  144. newDataset.data.push({
  145. x: randomScalingFactor(),
  146. y: randomScalingFactor(),
  147. r: Math.abs(randomScalingFactor()) / 5,
  148. });
  149. }
  150. bubbleChartData.datasets.push(newDataset);
  151. window.myChart.update();
  152. });
  153. document.getElementById('addData').addEventListener('click', function() {
  154. if (bubbleChartData.datasets.length > 0) {
  155. for (var index = 0; index < bubbleChartData.datasets.length; ++index) {
  156. bubbleChartData.datasets[index].data.push({
  157. x: randomScalingFactor(),
  158. y: randomScalingFactor(),
  159. r: Math.abs(randomScalingFactor()) / 5,
  160. });
  161. }
  162. window.myChart.update();
  163. }
  164. });
  165. document.getElementById('removeDataset').addEventListener('click', function() {
  166. bubbleChartData.datasets.splice(0, 1);
  167. window.myChart.update();
  168. });
  169. document.getElementById('removeData').addEventListener('click', function() {
  170. bubbleChartData.datasets.forEach(function(dataset) {
  171. dataset.data.pop();
  172. });
  173. window.myChart.update();
  174. });
  175. </script>
  176. </body>
  177. </html>