selection.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. <code id="logs"></code>
  17. </div>
  18. <div id="charts">
  19. <div class="wrapper"><canvas id="chart-0"></canvas></div>
  20. </div>
  21. <script>
  22. var SAMPLE_INFO = {
  23. group: 'Events',
  24. name: 'Selection',
  25. desc: 'Click events are handled to select labels, returning true to '
  26. + 're-render the chart and thus update the labels appearance.'
  27. };
  28. </script>
  29. <script id="script-init">
  30. var DATA_COUNT = 8;
  31. var selection = [];
  32. var labels = [];
  33. Samples.srand(7);
  34. for (var i = 0; i < DATA_COUNT; ++i) {
  35. labels.push('' + i);
  36. }
  37. Chart.helpers.merge(Chart.defaults.global, {
  38. aspectRatio: 4/3,
  39. tooltips: false,
  40. layout: {
  41. padding: {
  42. top: 42,
  43. right: 16,
  44. bottom: 32,
  45. left: 8
  46. }
  47. },
  48. elements: {
  49. line: {
  50. fill: false
  51. }
  52. },
  53. plugins: {
  54. legend: false,
  55. title: false
  56. }
  57. });
  58. function lookup(context) {
  59. var dataset = context.datasetIndex;
  60. var index = context.dataIndex;
  61. var i, ilen;
  62. for (i = 0, ilen = selection.length; i < ilen; ++i) {
  63. if (selection[i].dataset === dataset && selection[i].index === index) {
  64. return i;
  65. }
  66. }
  67. return -1;
  68. }
  69. function isSelected(context) {
  70. return lookup(context) !== -1;
  71. }
  72. function select(context) {
  73. var dataset = context.datasetIndex;
  74. var index = context.dataIndex;
  75. var value = context.dataset.data[index];
  76. selection.push({
  77. dataset: dataset,
  78. index: index,
  79. value: value})
  80. log(selection);
  81. }
  82. function deselect(context) {
  83. var index = lookup(context);
  84. if (index !== -1) {
  85. selection.splice(index, 1);
  86. log(selection);
  87. }
  88. }
  89. function log(selection) {
  90. var line = document.createElement('div');
  91. var logs = document.getElementById('logs');
  92. line.innerHTML = 'selection: ' + selection.map(function(item) {
  93. return item.value;
  94. }).join(', ');
  95. logs.insertBefore(line, logs.firstChild);
  96. if (logs.childNodes.length > 8) {
  97. logs.removeChild(logs.lastChild);
  98. }
  99. }
  100. </script>
  101. <script id="script-construct">
  102. var chart = new Chart('chart-0', {
  103. type: 'line',
  104. data: {
  105. labels: labels,
  106. datasets: [{
  107. backgroundColor: Samples.color(0),
  108. borderColor: Samples.color(0),
  109. data: Samples.numbers({
  110. count: DATA_COUNT,
  111. decimals: 0,
  112. min: 0,
  113. max: 100
  114. }),
  115. datalabels: {
  116. align: 'start'
  117. }
  118. }, {
  119. backgroundColor: Samples.color(1),
  120. borderColor: Samples.color(1),
  121. data: Samples.numbers({
  122. count: DATA_COUNT,
  123. decimals: 0,
  124. min: 0,
  125. max: 100
  126. })
  127. }, {
  128. backgroundColor: Samples.color(2),
  129. borderColor: Samples.color(2),
  130. data: Samples.numbers({
  131. count: DATA_COUNT,
  132. decimals: 0,
  133. min: 0,
  134. max: 100
  135. }),
  136. datalabels: {
  137. align: 'end'
  138. }
  139. }]
  140. },
  141. options: {
  142. plugins: {
  143. datalabels: {
  144. backgroundColor: function(context) {
  145. return isSelected(context)
  146. ? context.dataset.backgroundColor
  147. : 'white';
  148. },
  149. borderColor: function(context) {
  150. return context.dataset.backgroundColor;
  151. },
  152. borderWidth: 1,
  153. color: function(context) {
  154. return isSelected(context)
  155. ? 'white'
  156. : context.dataset.backgroundColor;
  157. },
  158. font: {
  159. weight: 'bold'
  160. },
  161. offset: 8,
  162. padding: 4,
  163. listeners: {
  164. click: function(context) {
  165. if (isSelected(context)) {
  166. deselect(context);
  167. } else {
  168. select(context);
  169. }
  170. return true;
  171. }
  172. }
  173. }
  174. },
  175. scales: {
  176. yAxes: [{
  177. stacked: true
  178. }]
  179. }
  180. }
  181. });
  182. </script>
  183. </body>
  184. </html>