interactions.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Tooltip Interaction Modes</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. .chart-container {
  14. width: 500px;
  15. margin-left: 40px;
  16. margin-right: 40px;
  17. margin-bottom: 40px;
  18. }
  19. .container {
  20. display: flex;
  21. flex-direction: row;
  22. flex-wrap: wrap;
  23. justify-content: center;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="container">
  29. </div>
  30. <script>
  31. function createConfig(mode, intersect) {
  32. return {
  33. type: 'line',
  34. data: {
  35. labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  36. datasets: [{
  37. label: 'My First dataset',
  38. borderColor: window.chartColors.red,
  39. backgroundColor: window.chartColors.red,
  40. data: [10, 30, 46, 2, 8, 50, 0],
  41. fill: false,
  42. }, {
  43. label: 'My Second dataset',
  44. borderColor: window.chartColors.blue,
  45. backgroundColor: window.chartColors.blue,
  46. data: [7, 49, 46, 13, 25, 30, 22],
  47. fill: false,
  48. }]
  49. },
  50. options: {
  51. responsive: true,
  52. title: {
  53. display: true,
  54. text: 'Mode: ' + mode + ', intersect = ' + intersect
  55. },
  56. tooltips: {
  57. mode: mode,
  58. intersect: intersect,
  59. },
  60. hover: {
  61. mode: mode,
  62. intersect: intersect
  63. },
  64. }
  65. };
  66. }
  67. window.onload = function() {
  68. var container = document.querySelector('.container');
  69. [{
  70. mode: 'index',
  71. intersect: true,
  72. }, {
  73. mode: 'index',
  74. intersect: false,
  75. }, {
  76. mode: 'dataset',
  77. intersect: true,
  78. }, {
  79. mode: 'dataset',
  80. intersect: false,
  81. }, {
  82. mode: 'point',
  83. intersect: true,
  84. }, {
  85. mode: 'point',
  86. intersect: false,
  87. }, {
  88. mode: 'nearest',
  89. intersect: true,
  90. }, {
  91. mode: 'nearest',
  92. intersect: false,
  93. }, {
  94. mode: 'x',
  95. intersect: true
  96. }, {
  97. mode: 'x',
  98. intersect: false
  99. }, {
  100. mode: 'y',
  101. intersect: true
  102. }, {
  103. mode: 'y',
  104. intersect: false
  105. }].forEach(function(details) {
  106. var div = document.createElement('div');
  107. div.classList.add('chart-container');
  108. var canvas = document.createElement('canvas');
  109. div.appendChild(canvas);
  110. container.appendChild(div);
  111. var ctx = canvas.getContext('2d');
  112. var config = createConfig(details.mode, details.intersect);
  113. new Chart(ctx, config);
  114. });
  115. };
  116. </script>
  117. </body>
  118. </html>