custom-labels.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. </div>
  19. </div>
  20. <div id="charts">
  21. <div class="wrapper"><canvas id="chart-0"></canvas></div>
  22. <div class="wrapper"><canvas id="chart-1"></canvas></div>
  23. </div>
  24. <script>
  25. var SAMPLE_INFO = {
  26. group: 'Formatting',
  27. name: 'Custom Labels',
  28. desc: 'This example displays the data labels instead '+
  29. 'of the data values, using a custom formatter'
  30. };
  31. </script>
  32. <script id="script-init">
  33. var DATA_COUNT = 5;
  34. var labels = [
  35. 'Earth',
  36. 'Mars',
  37. 'Saturn',
  38. 'Jupiter',
  39. 'Others'
  40. ];
  41. Samples.srand(0);
  42. Chart.helpers.merge(Chart.defaults.global, {
  43. aspectRatio: 4/3,
  44. tooltips: false,
  45. layout: {
  46. padding: {
  47. top: 32
  48. }
  49. },
  50. elements: {
  51. line: {
  52. fill: false
  53. }
  54. },
  55. plugins: {
  56. legend: false,
  57. title: false
  58. }
  59. });
  60. </script>
  61. <script id="script-construct">
  62. var chart = new Chart('chart-0', {
  63. type: 'bar',
  64. data: {
  65. labels: labels,
  66. datasets: [{
  67. backgroundColor: Samples.color(0),
  68. data: Samples.numbers({
  69. count: DATA_COUNT,
  70. min: 0,
  71. max: 100
  72. })
  73. }]
  74. },
  75. options: {
  76. plugins: {
  77. datalabels: {
  78. align: 'end',
  79. anchor: 'end',
  80. color: function(context) {
  81. return context.dataset.backgroundColor;
  82. },
  83. font: function(context) {
  84. var w = context.chart.width;
  85. return {
  86. size: w < 512 ? 12 : 14
  87. }
  88. },
  89. formatter: function(value, context) {
  90. return context.chart.data.labels[context.dataIndex];
  91. }
  92. }
  93. },
  94. scales: {
  95. xAxes: [{
  96. display: false,
  97. offset: true
  98. }],
  99. yAxes: [{
  100. ticks: {
  101. beginAtZero: true
  102. }
  103. }]
  104. }
  105. }
  106. });
  107. </script>
  108. <script id="script-actions">
  109. function randomize() {
  110. chart.data.datasets.forEach(function(dataset, i) {
  111. dataset.backgroundColor = Samples.color(),
  112. dataset.data = dataset.data.map(function(value) {
  113. return Samples.rand(0, 100);
  114. });
  115. });
  116. chart.update();
  117. }
  118. </script>
  119. </body>
  120. </html>