custom-line.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Line Chart with Custom Tooltips</title>
  5. <script src="../../dist/Chart.min.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. #chartjs-tooltip {
  14. opacity: 1;
  15. position: absolute;
  16. background: rgba(0, 0, 0, .7);
  17. color: white;
  18. border-radius: 3px;
  19. -webkit-transition: all .1s ease;
  20. transition: all .1s ease;
  21. pointer-events: none;
  22. -webkit-transform: translate(-50%, 0);
  23. transform: translate(-50%, 0);
  24. }
  25. .chartjs-tooltip-key {
  26. display: inline-block;
  27. width: 10px;
  28. height: 10px;
  29. margin-right: 10px;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div id="canvas-holder1" style="width:75%;">
  35. <canvas id="chart"></canvas>
  36. </div>
  37. <script>
  38. Chart.defaults.global.pointHitDetectionRadius = 1;
  39. var customTooltips = function(tooltip) {
  40. // Tooltip Element
  41. var tooltipEl = document.getElementById('chartjs-tooltip');
  42. if (!tooltipEl) {
  43. tooltipEl = document.createElement('div');
  44. tooltipEl.id = 'chartjs-tooltip';
  45. tooltipEl.innerHTML = '<table></table>';
  46. this._chart.canvas.parentNode.appendChild(tooltipEl);
  47. }
  48. // Hide if no tooltip
  49. if (tooltip.opacity === 0) {
  50. tooltipEl.style.opacity = 0;
  51. return;
  52. }
  53. // Set caret Position
  54. tooltipEl.classList.remove('above', 'below', 'no-transform');
  55. if (tooltip.yAlign) {
  56. tooltipEl.classList.add(tooltip.yAlign);
  57. } else {
  58. tooltipEl.classList.add('no-transform');
  59. }
  60. function getBody(bodyItem) {
  61. return bodyItem.lines;
  62. }
  63. // Set Text
  64. if (tooltip.body) {
  65. var titleLines = tooltip.title || [];
  66. var bodyLines = tooltip.body.map(getBody);
  67. var innerHtml = '<thead>';
  68. titleLines.forEach(function(title) {
  69. innerHtml += '<tr><th>' + title + '</th></tr>';
  70. });
  71. innerHtml += '</thead><tbody>';
  72. bodyLines.forEach(function(body, i) {
  73. var colors = tooltip.labelColors[i];
  74. var style = 'background:' + colors.backgroundColor;
  75. style += '; border-color:' + colors.borderColor;
  76. style += '; border-width: 2px';
  77. var span = '<span class="chartjs-tooltip-key" style="' + style + '"></span>';
  78. innerHtml += '<tr><td>' + span + body + '</td></tr>';
  79. });
  80. innerHtml += '</tbody>';
  81. var tableRoot = tooltipEl.querySelector('table');
  82. tableRoot.innerHTML = innerHtml;
  83. }
  84. var positionY = this._chart.canvas.offsetTop;
  85. var positionX = this._chart.canvas.offsetLeft;
  86. // Display, position, and set styles for font
  87. tooltipEl.style.opacity = 1;
  88. tooltipEl.style.left = positionX + tooltip.caretX + 'px';
  89. tooltipEl.style.top = positionY + tooltip.caretY + 'px';
  90. tooltipEl.style.fontFamily = tooltip._bodyFontFamily;
  91. tooltipEl.style.fontSize = tooltip.bodyFontSize + 'px';
  92. tooltipEl.style.fontStyle = tooltip._bodyFontStyle;
  93. tooltipEl.style.padding = tooltip.yPadding + 'px ' + tooltip.xPadding + 'px';
  94. };
  95. var lineChartData = {
  96. labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  97. datasets: [{
  98. label: 'My First dataset',
  99. borderColor: window.chartColors.red,
  100. pointBackgroundColor: window.chartColors.red,
  101. fill: false,
  102. data: [
  103. randomScalingFactor(),
  104. randomScalingFactor(),
  105. randomScalingFactor(),
  106. randomScalingFactor(),
  107. randomScalingFactor(),
  108. randomScalingFactor(),
  109. randomScalingFactor()
  110. ]
  111. }, {
  112. label: 'My Second dataset',
  113. borderColor: window.chartColors.blue,
  114. pointBackgroundColor: window.chartColors.blue,
  115. fill: false,
  116. data: [
  117. randomScalingFactor(),
  118. randomScalingFactor(),
  119. randomScalingFactor(),
  120. randomScalingFactor(),
  121. randomScalingFactor(),
  122. randomScalingFactor(),
  123. randomScalingFactor()
  124. ]
  125. }]
  126. };
  127. window.onload = function() {
  128. var chartEl = document.getElementById('chart');
  129. window.myLine = new Chart(chartEl, {
  130. type: 'line',
  131. data: lineChartData,
  132. options: {
  133. title: {
  134. display: true,
  135. text: 'Chart.js Line Chart - Custom Tooltips'
  136. },
  137. tooltips: {
  138. enabled: false,
  139. mode: 'index',
  140. position: 'nearest',
  141. custom: customTooltips
  142. }
  143. }
  144. });
  145. };
  146. </script>
  147. </body>
  148. </html>