line.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Line Chart</title>
  5. <script src="../dist/Chart.bundle.js"></script>
  6. <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.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. </div>
  19. <br>
  20. <br>
  21. <button id="randomizeData">Randomize Data</button>
  22. <button id="changeDataObject">Change Data Object</button>
  23. <button id="addDataset">Add Dataset</button>
  24. <button id="removeDataset">Remove Dataset</button>
  25. <button id="addData">Add Data</button>
  26. <button id="removeData">Remove Data</button>
  27. <script>
  28. var MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  29. var randomScalingFactor = function() {
  30. return Math.round(Math.random() * 100);
  31. //return 0;
  32. };
  33. var randomColorFactor = function() {
  34. return Math.round(Math.random() * 255);
  35. };
  36. var randomColor = function(opacity) {
  37. return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
  38. };
  39. var config = {
  40. type: 'line',
  41. data: {
  42. labels: ["January", "February", "March", "April", "May", "June", "July"],
  43. datasets: [{
  44. label: "My First dataset",
  45. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  46. fill: false,
  47. borderDash: [5, 5],
  48. }, {
  49. hidden: true,
  50. label: 'hidden dataset',
  51. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  52. }, {
  53. label: "My Second dataset",
  54. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  55. }]
  56. },
  57. options: {
  58. responsive: true,
  59. title:{
  60. display:true,
  61. text:'Chart.js Line Chart'
  62. },
  63. tooltips: {
  64. mode: 'label',
  65. callbacks: {
  66. // beforeTitle: function() {
  67. // return '...beforeTitle';
  68. // },
  69. // afterTitle: function() {
  70. // return '...afterTitle';
  71. // },
  72. // beforeBody: function() {
  73. // return '...beforeBody';
  74. // },
  75. // afterBody: function() {
  76. // return '...afterBody';
  77. // },
  78. // beforeFooter: function() {
  79. // return '...beforeFooter';
  80. // },
  81. // footer: function() {
  82. // return 'Footer';
  83. // },
  84. // afterFooter: function() {
  85. // return '...afterFooter';
  86. // },
  87. }
  88. },
  89. hover: {
  90. mode: 'dataset'
  91. },
  92. scales: {
  93. xAxes: [{
  94. display: true,
  95. scaleLabel: {
  96. show: true,
  97. labelString: 'Month'
  98. }
  99. }],
  100. yAxes: [{
  101. display: true,
  102. scaleLabel: {
  103. show: true,
  104. labelString: 'Value'
  105. },
  106. ticks: {
  107. suggestedMin: -10,
  108. suggestedMax: 250,
  109. }
  110. }]
  111. }
  112. }
  113. };
  114. $.each(config.data.datasets, function(i, dataset) {
  115. dataset.borderColor = randomColor(0.4);
  116. dataset.backgroundColor = randomColor(0.5);
  117. dataset.pointBorderColor = randomColor(0.7);
  118. dataset.pointBackgroundColor = randomColor(0.5);
  119. dataset.pointBorderWidth = 1;
  120. });
  121. window.onload = function() {
  122. var ctx = document.getElementById("canvas").getContext("2d");
  123. window.myLine = new Chart(ctx, config);
  124. };
  125. $('#randomizeData').click(function() {
  126. $.each(config.data.datasets, function(i, dataset) {
  127. dataset.data = dataset.data.map(function() {
  128. return randomScalingFactor();
  129. });
  130. });
  131. window.myLine.update();
  132. });
  133. $('#changeDataObject').click(function() {
  134. config.data = {
  135. labels: ["July", "August", "September", "October", "November", "December"],
  136. datasets: [{
  137. label: "My First dataset",
  138. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  139. fill: false,
  140. }, {
  141. label: "My Second dataset",
  142. fill: false,
  143. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  144. }]
  145. };
  146. $.each(config.data.datasets, function(i, dataset) {
  147. dataset.borderColor = randomColor(0.4);
  148. dataset.backgroundColor = randomColor(0.5);
  149. dataset.pointBorderColor = randomColor(0.7);
  150. dataset.pointBackgroundColor = randomColor(0.5);
  151. dataset.pointBorderWidth = 1;
  152. });
  153. // Update the chart
  154. window.myLine.update();
  155. });
  156. $('#addDataset').click(function() {
  157. var newDataset = {
  158. label: 'Dataset ' + config.data.datasets.length,
  159. borderColor: randomColor(0.4),
  160. backgroundColor: randomColor(0.5),
  161. pointBorderColor: randomColor(0.7),
  162. pointBackgroundColor: randomColor(0.5),
  163. pointBorderWidth: 1,
  164. data: [],
  165. };
  166. for (var index = 0; index < config.data.labels.length; ++index) {
  167. newDataset.data.push(randomScalingFactor());
  168. }
  169. config.data.datasets.push(newDataset);
  170. window.myLine.update();
  171. });
  172. $('#addData').click(function() {
  173. if (config.data.datasets.length > 0) {
  174. var month = MONTHS[config.data.labels.length % MONTHS.length];
  175. config.data.labels.push(month);
  176. $.each(config.data.datasets, function(i, dataset) {
  177. dataset.data.push(randomScalingFactor());
  178. });
  179. window.myLine.update();
  180. }
  181. });
  182. $('#removeDataset').click(function() {
  183. config.data.datasets.splice(0, 1);
  184. window.myLine.update();
  185. });
  186. $('#removeData').click(function() {
  187. config.data.labels.splice(-1, 1); // remove the label first
  188. config.data.datasets.forEach(function(dataset, datasetIndex) {
  189. dataset.data.pop();
  190. });
  191. window.myLine.update();
  192. });
  193. </script>
  194. </body>
  195. </html>