line-customTooltips.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Line Chart with Custom Tooltips</title>
  5. <script src="../Chart.js"></script>
  6. <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  7. <style>
  8. #canvas-holder1 {
  9. width: 300px;
  10. margin: 20px auto;
  11. }
  12. #canvas-holder2 {
  13. width: 50%;
  14. margin: 20px 25%;
  15. }
  16. #chartjs-tooltip {
  17. opacity: 1;
  18. position: absolute;
  19. background: rgba(0, 0, 0, .7);
  20. color: white;
  21. padding: 3px;
  22. border-radius: 3px;
  23. -webkit-transition: all .1s ease;
  24. transition: all .1s ease;
  25. pointer-events: none;
  26. -webkit-transform: translate(-50%, 0);
  27. transform: translate(-50%, 0);
  28. }
  29. .chartjs-tooltip-key{
  30. display:inline-block;
  31. width:10px;
  32. height:10px;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div id="canvas-holder1">
  38. <canvas id="chart1" width="300" height="30" />
  39. </div>
  40. <div id="canvas-holder2">
  41. <canvas id="chart2" width="450" height="600" />
  42. </div>
  43. <div id="chartjs-tooltip"></div>
  44. <script>
  45. Chart.defaults.global.pointHitDetectionRadius = 1;
  46. Chart.defaults.global.customTooltips = function(tooltip) {
  47. var tooltipEl = $('#chartjs-tooltip');
  48. if (!tooltip) {
  49. tooltipEl.css({
  50. opacity: 0
  51. });
  52. return;
  53. }
  54. tooltipEl.removeClass('above below');
  55. tooltipEl.addClass(tooltip.yAlign);
  56. var innerHtml = '';
  57. for (var i = tooltip.labels.length - 1; i >= 0; i--) {
  58. innerHtml += [
  59. '<div class="chartjs-tooltip-section">',
  60. ' <span class="chartjs-tooltip-key" style="background-color:' + tooltip.legendColors[i].fill + '"></span>',
  61. ' <span class="chartjs-tooltip-value">' + tooltip.labels[i] + '</span>',
  62. '</div>'
  63. ].join('');
  64. }
  65. tooltipEl.html(innerHtml);
  66. tooltipEl.css({
  67. opacity: 1,
  68. left: tooltip.chart.canvas.offsetLeft + tooltip.x + 'px',
  69. top: tooltip.chart.canvas.offsetTop + tooltip.y + 'px',
  70. fontFamily: tooltip.fontFamily,
  71. fontSize: tooltip.fontSize,
  72. fontStyle: tooltip.fontStyle,
  73. });
  74. };
  75. var randomScalingFactor = function() {
  76. return Math.round(Math.random() * 100);
  77. };
  78. var lineChartData = {
  79. labels: ["January", "February", "March", "April", "May", "June", "July"],
  80. datasets: [{
  81. label: "My First dataset",
  82. fillColor: "rgba(220,220,220,0.2)",
  83. strokeColor: "rgba(220,220,220,1)",
  84. pointColor: "rgba(220,220,220,1)",
  85. pointStrokeColor: "#fff",
  86. pointHighlightFill: "#fff",
  87. pointHighlightStroke: "rgba(220,220,220,1)",
  88. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
  89. }, {
  90. label: "My Second dataset",
  91. fillColor: "rgba(151,187,205,0.2)",
  92. strokeColor: "rgba(151,187,205,1)",
  93. pointColor: "rgba(151,187,205,1)",
  94. pointStrokeColor: "#fff",
  95. pointHighlightFill: "#fff",
  96. pointHighlightStroke: "rgba(151,187,205,1)",
  97. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
  98. }]
  99. };
  100. window.onload = function() {
  101. var ctx1 = document.getElementById("chart1").getContext("2d");
  102. window.myLine = new Chart(ctx1).Line(lineChartData, {
  103. showScale: false,
  104. pointDot : true,
  105. responsive: true
  106. });
  107. var ctx2 = document.getElementById("chart2").getContext("2d");
  108. window.myLine = new Chart(ctx2).Line(lineChartData, {
  109. responsive: true
  110. });
  111. };
  112. </script>
  113. </body>
  114. </html>