data-labelling.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Labelling Data Points</title>
  5. <script src="../../dist/Chart.bundle.js"></script>
  6. <script src="../utils.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. <button id="randomizeData">Randomize Data</button>
  20. <script>
  21. var color = Chart.helpers.color;
  22. var barChartData = {
  23. labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  24. datasets: [{
  25. type: 'bar',
  26. label: 'Dataset 1',
  27. backgroundColor: color(window.chartColors.red).alpha(0.2).rgbString(),
  28. borderColor: window.chartColors.red,
  29. data: [
  30. randomScalingFactor(),
  31. randomScalingFactor(),
  32. randomScalingFactor(),
  33. randomScalingFactor(),
  34. randomScalingFactor(),
  35. randomScalingFactor(),
  36. randomScalingFactor()
  37. ]
  38. }, {
  39. type: 'line',
  40. label: 'Dataset 2',
  41. backgroundColor: color(window.chartColors.blue).alpha(0.2).rgbString(),
  42. borderColor: window.chartColors.blue,
  43. data: [
  44. randomScalingFactor(),
  45. randomScalingFactor(),
  46. randomScalingFactor(),
  47. randomScalingFactor(),
  48. randomScalingFactor(),
  49. randomScalingFactor(),
  50. randomScalingFactor()
  51. ]
  52. }, {
  53. type: 'bar',
  54. label: 'Dataset 3',
  55. backgroundColor: color(window.chartColors.green).alpha(0.2).rgbString(),
  56. borderColor: window.chartColors.green,
  57. data: [
  58. randomScalingFactor(),
  59. randomScalingFactor(),
  60. randomScalingFactor(),
  61. randomScalingFactor(),
  62. randomScalingFactor(),
  63. randomScalingFactor(),
  64. randomScalingFactor()
  65. ]
  66. }]
  67. };
  68. // Define a plugin to provide data labels
  69. Chart.plugins.register({
  70. afterDatasetsDraw: function(chart) {
  71. var ctx = chart.ctx;
  72. chart.data.datasets.forEach(function(dataset, i) {
  73. var meta = chart.getDatasetMeta(i);
  74. if (!meta.hidden) {
  75. meta.data.forEach(function(element, index) {
  76. // Draw the text in black, with the specified font
  77. ctx.fillStyle = 'rgb(0, 0, 0)';
  78. var fontSize = 16;
  79. var fontStyle = 'normal';
  80. var fontFamily = 'Helvetica Neue';
  81. ctx.font = Chart.helpers.fontString(fontSize, fontStyle, fontFamily);
  82. // Just naively convert to string for now
  83. var dataString = dataset.data[index].toString();
  84. // Make sure alignment settings are correct
  85. ctx.textAlign = 'center';
  86. ctx.textBaseline = 'middle';
  87. var padding = 5;
  88. var position = element.tooltipPosition();
  89. ctx.fillText(dataString, position.x, position.y - (fontSize / 2) - padding);
  90. });
  91. }
  92. });
  93. }
  94. });
  95. window.onload = function() {
  96. var ctx = document.getElementById('canvas').getContext('2d');
  97. window.myBar = new Chart(ctx, {
  98. type: 'bar',
  99. data: barChartData,
  100. options: {
  101. responsive: true,
  102. title: {
  103. display: true,
  104. text: 'Chart.js Combo Bar Line Chart'
  105. },
  106. }
  107. });
  108. };
  109. document.getElementById('randomizeData').addEventListener('click', function() {
  110. barChartData.datasets.forEach(function(dataset) {
  111. dataset.data = dataset.data.map(function() {
  112. return randomScalingFactor();
  113. });
  114. });
  115. window.myBar.update();
  116. });
  117. </script>
  118. </body>
  119. </html>