highlight.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <!DOCTYPE html>
  2. <html lang="en-US">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" type="text/css" href="../style.css">
  8. <link rel="icon" type="image/ico" href="../favicon.ico">
  9. <script src="https://cdn.jsdelivr.net/npm/chart.js@2.x/dist/Chart.min.js"></script>
  10. <script src="../../dist/chartjs-plugin-datalabels.js"></script>
  11. <script src="../utils.js"></script>
  12. </head>
  13. <body>
  14. <div id="side">
  15. <div id="header"></div>
  16. <div id="actions">
  17. <button onclick="randomize(this)">Randomize</button>
  18. <button onclick="addData(this)">Add Data</button>
  19. <button onclick="removeData(this)">Remove Data</button>
  20. </div>
  21. </div>
  22. <div id="charts">
  23. <div class="wrapper"><canvas id="chart-0"></canvas></div>
  24. </div>
  25. <script>
  26. var SAMPLE_INFO = {
  27. group: 'Events',
  28. name: 'Highlight',
  29. desc: 'Listen the <em>enter</em> and <em>leave</em> events to '
  30. + 'modify the label appearance when hovered by the mouse, '
  31. + 'using scriptable options.'
  32. };
  33. </script>
  34. <script id="script-init">
  35. var DATA_COUNT = 8;
  36. var labels = [];
  37. Samples.srand(0);
  38. for (var i = 0; i < DATA_COUNT; ++i) {
  39. labels.push('' + i);
  40. }
  41. Chart.helpers.merge(Chart.defaults.global, {
  42. aspectRatio: 4/3,
  43. tooltips: false,
  44. layout: {
  45. padding: {
  46. top: 42,
  47. right: 16,
  48. bottom: 32,
  49. left: 8
  50. }
  51. },
  52. elements: {
  53. line: {
  54. fill: false
  55. }
  56. },
  57. plugins: {
  58. legend: false,
  59. title: false
  60. }
  61. });
  62. </script>
  63. <script id="script-construct">
  64. var chart = new Chart('chart-0', {
  65. type: 'line',
  66. data: {
  67. labels: labels,
  68. datasets: [{
  69. backgroundColor: Samples.color(0),
  70. borderColor: Samples.color(0),
  71. data: Samples.numbers({
  72. count: DATA_COUNT,
  73. min: 0,
  74. max: 100
  75. }),
  76. datalabels: {
  77. align: 'start'
  78. }
  79. }, {
  80. backgroundColor: Samples.color(1),
  81. borderColor: Samples.color(1),
  82. data: Samples.numbers({
  83. count: DATA_COUNT,
  84. min: 0,
  85. max: 100
  86. })
  87. }, {
  88. backgroundColor: Samples.color(2),
  89. borderColor: Samples.color(2),
  90. data: Samples.numbers({
  91. count: DATA_COUNT,
  92. min: 0,
  93. max: 100
  94. }),
  95. datalabels: {
  96. align: 'end'
  97. }
  98. }]
  99. },
  100. options: {
  101. plugins: {
  102. datalabels: {
  103. backgroundColor: function(context) {
  104. return context.hovered ? context.dataset.backgroundColor : 'white';
  105. },
  106. borderColor: function(context) {
  107. return context.dataset.backgroundColor;
  108. },
  109. borderRadius: 16,
  110. borderWidth: 1,
  111. color: function(context) {
  112. return context.hovered ? 'white' : context.dataset.backgroundColor;
  113. },
  114. font: {
  115. weight: 'bold'
  116. },
  117. offset: 8,
  118. formatter: Math.round,
  119. listeners: {
  120. enter: function(context) {
  121. context.hovered = true;
  122. return true;
  123. },
  124. leave: function(context) {
  125. context.hovered = false;
  126. return true;
  127. }
  128. }
  129. }
  130. },
  131. scales: {
  132. yAxes: [{
  133. stacked: true
  134. }]
  135. }
  136. }
  137. });
  138. </script>
  139. <script id="script-actions">
  140. function randomize() {
  141. chart.data.datasets.forEach(function(dataset, i) {
  142. dataset.backgroundColor = dataset.borderColor = Samples.color();
  143. dataset.data = dataset.data.map(function(value) {
  144. return Samples.rand(0, 100);
  145. });
  146. });
  147. chart.update();
  148. }
  149. function addData() {
  150. chart.data.labels.push(chart.data.labels.length);
  151. chart.data.datasets.forEach(function(dataset, i) {
  152. dataset.data.push(Samples.rand(0, 100));
  153. });
  154. chart.update();
  155. }
  156. function removeData() {
  157. chart.data.labels.shift();
  158. chart.data.datasets.forEach(function(dataset, i) {
  159. dataset.data.shift();
  160. });
  161. chart.update();
  162. }
  163. </script>
  164. </body>
  165. </html>