interactions.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.7.0/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: 'Scriptable',
  28. name: 'Interactions',
  29. desc: 'This example demonstrates how to levrage scriptable ' +
  30. 'options to change the style and content of the labels ' +
  31. 'when the user interacts with the chart elements'
  32. };
  33. </script>
  34. <script id="script-init">
  35. var DATA_COUNT = 8;
  36. var labels = [];
  37. Samples.srand(100);
  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. bottom: 25,
  47. right: 50,
  48. left: 25,
  49. top: 50
  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. label: 'France',
  70. backgroundColor: Samples.color(0),
  71. borderColor: Samples.color(0),
  72. data: Samples.numbers({
  73. count: DATA_COUNT,
  74. min: 10,
  75. max: 100
  76. }),
  77. datalabels: {
  78. align: function(context) {
  79. return context.active ? 'start' : 'center';
  80. }
  81. }
  82. }, {
  83. label: 'Canada',
  84. backgroundColor: Samples.color(1),
  85. borderColor: Samples.color(1),
  86. data: Samples.numbers({
  87. count: DATA_COUNT,
  88. min: 0,
  89. max: 100
  90. })
  91. }, {
  92. label: 'USA',
  93. backgroundColor: Samples.color(2),
  94. borderColor: Samples.color(2),
  95. data: Samples.numbers({
  96. count: DATA_COUNT,
  97. min: 0,
  98. max: 100
  99. }),
  100. datalabels: {
  101. align: function(context) {
  102. return context.active ? 'end' : 'center';
  103. }
  104. }
  105. }]
  106. },
  107. options: {
  108. hover: {
  109. mode: 'index',
  110. intersect: false
  111. },
  112. plugins: {
  113. datalabels: {
  114. backgroundColor: function(context) {
  115. return context.active ? context.dataset.backgroundColor : 'white';
  116. },
  117. borderColor: function(context) {
  118. return context.dataset.backgroundColor;
  119. },
  120. borderRadius: function(context) {
  121. return context.active ? 0 : 32;
  122. },
  123. borderWidth: 1,
  124. color: function(context) {
  125. return context.active ? 'white' : context.dataset.backgroundColor;
  126. },
  127. font: {
  128. weight: 'bold'
  129. },
  130. formatter: function(value, context) {
  131. value = Math.round(value * 100) / 100;
  132. return context.active
  133. ? context.dataset.label + '\n' + value + '%'
  134. : Math.round(value);
  135. },
  136. offset: 8,
  137. textAlign: 'center'
  138. }
  139. },
  140. scales: {
  141. yAxes: [{
  142. stacked: true
  143. }]
  144. }
  145. }
  146. });
  147. </script>
  148. <script id="script-actions">
  149. function randomize() {
  150. chart.data.datasets.forEach(function(dataset, i) {
  151. dataset.backgroundColor = dataset.borderColor = Samples.color();
  152. dataset.data = dataset.data.map(function(value) {
  153. return Samples.rand(0, 100);
  154. });
  155. });
  156. chart.update();
  157. }
  158. function addData() {
  159. chart.data.labels.push(chart.data.labels.length);
  160. chart.data.datasets.forEach(function(dataset, i) {
  161. dataset.data.push(Samples.rand(0, 100));
  162. });
  163. chart.update();
  164. }
  165. function removeData() {
  166. chart.data.labels.shift();
  167. chart.data.datasets.forEach(function(dataset, i) {
  168. dataset.data.shift();
  169. });
  170. chart.update();
  171. }
  172. </script>
  173. </body>
  174. </html>