tooltip-hooks.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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="addDataset">Add Dataset</button>
  23. <button id="removeDataset">Remove Dataset</button>
  24. <button id="addData">Add Data</button>
  25. <button id="removeData">Remove Data</button>
  26. <script>
  27. var MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  28. var randomScalingFactor = function() {
  29. return Math.round(Math.random() * 100 * (Math.random() > 0.5 ? -1 : 1));
  30. };
  31. var randomColorFactor = function() {
  32. return Math.round(Math.random() * 255);
  33. };
  34. var randomColor = function(opacity) {
  35. return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
  36. };
  37. var config = {
  38. type: 'line',
  39. data: {
  40. labels: ["January", "February", "March", "April", "May", "June", "July"],
  41. datasets: [{
  42. label: "My First dataset",
  43. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  44. fill: false,
  45. borderDash: [5, 5],
  46. }, {
  47. label: "My Second dataset",
  48. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
  49. }]
  50. },
  51. options: {
  52. responsive: true,
  53. title:{
  54. display:true,
  55. text:"Chart.js Line Chart - Tooltip Hooks"
  56. },
  57. tooltips: {
  58. mode: 'label',
  59. callbacks: {
  60. beforeTitle: function() {
  61. return '...beforeTitle';
  62. },
  63. afterTitle: function() {
  64. return '...afterTitle';
  65. },
  66. beforeBody: function() {
  67. return '...beforeBody';
  68. },
  69. afterBody: function() {
  70. return '...afterBody';
  71. },
  72. beforeFooter: function() {
  73. return '...beforeFooter';
  74. },
  75. footer: function() {
  76. return 'Footer';
  77. },
  78. afterFooter: function() {
  79. return '...afterFooter';
  80. },
  81. }
  82. },
  83. hover: {
  84. mode: 'label'
  85. },
  86. scales: {
  87. xAxes: [{
  88. display: true,
  89. scaleLabel: {
  90. show: true,
  91. labelString: 'Month'
  92. }
  93. }],
  94. yAxes: [{
  95. display: true,
  96. scaleLabel: {
  97. show: true,
  98. labelString: 'Value'
  99. }
  100. }]
  101. }
  102. }
  103. };
  104. $.each(config.data.datasets, function(i, dataset) {
  105. dataset.borderColor = randomColor(0.4);
  106. dataset.backgroundColor = randomColor(0.5);
  107. dataset.pointBorderColor = randomColor(0.7);
  108. dataset.pointBackgroundColor = randomColor(0.5);
  109. dataset.pointBorderWidth = 1;
  110. });
  111. window.onload = function() {
  112. var ctx = document.getElementById("canvas").getContext("2d");
  113. window.myLine = new Chart(ctx, config);
  114. };
  115. $('#randomizeData').click(function() {
  116. $.each(config.data.datasets, function(i, dataset) {
  117. dataset.data = dataset.data.map(function() {
  118. return randomScalingFactor();
  119. });
  120. });
  121. window.myLine.update();
  122. });
  123. $('#addDataset').click(function() {
  124. var newDataset = {
  125. label: 'Dataset ' + config.data.datasets.length,
  126. borderColor: randomColor(0.4),
  127. backgroundColor: randomColor(0.5),
  128. pointBorderColor: randomColor(0.7),
  129. pointBackgroundColor: randomColor(0.5),
  130. pointBorderWidth: 1,
  131. data: [],
  132. };
  133. for (var index = 0; index < config.data.labels.length; ++index) {
  134. newDataset.data.push(randomScalingFactor());
  135. }
  136. config.data.datasets.push(newDataset);
  137. window.myLine.update();
  138. });
  139. $('#addData').click(function() {
  140. if (config.data.datasets.length > 0) {
  141. var month = MONTHS[config.data.labels.length % MONTHS.length];
  142. config.data.labels.push(month);
  143. $.each(config.data.datasets, function(i, dataset) {
  144. dataset.data.push(randomScalingFactor());
  145. });
  146. window.myLine.update();
  147. }
  148. });
  149. $('#removeDataset').click(function() {
  150. config.data.datasets.splice(0, 1);
  151. window.myLine.update();
  152. });
  153. $('#removeData').click(function() {
  154. config.data.labels.splice(-1, 1); // remove the label first
  155. config.data.datasets.forEach(function(dataset, datasetIndex) {
  156. dataset.data.pop();
  157. });
  158. window.myLine.update();
  159. });
  160. </script>
  161. </body>
  162. </html>