Chartjs插件延迟抛出“未捕获的类型错误:无法读取未定义的属性'plugins'”错误

xfb7svmp  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(185)

我想延迟加载Chart.js生成的图表。不幸的是,当我加载插件时,我得到:

  1. Uncaught TypeError: Cannot read property 'plugins' of undefined"

我在用

  1. <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-deferred@1.0.2/dist/chartjs-plugin-deferred.min.js"></script>

  1. <script src="https://cdn.jsdelivr.net/npm/chart.js@3.1.1/dist/chart.min.js"></script>

这里有一个最小的小提琴来说明错误https://jsfiddle.net/aqgst865/

是否有任何方法可以修复此错误或替代延迟加载Chart.js图表?

hsgswve4

hsgswve41#

延迟的插件与chart.js版本3不兼容。要修复错误,您必须降级到版本2.9.4

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
  5. <link href="https://unpkg.com/@tailwindcss/custom-forms@0.2.1/dist/custom-forms.min.css" rel="stylesheet">
  6. <link href="/static/style.css" rel="stylesheet">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  9. <script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.min.js"></script>
  10. <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-deferred@1.0.2/dist/chartjs-plugin-deferred.min.js"></script>
  11. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  12. </head>
  13. <body>
  14. <div class="chartcontainer">
  15. <div class="mx-auto max-w-md">
  16. <canvas id="A" width="400" height="400"></canvas>
  17. </div>
  18. <script>
  19. var ctx = document.getElementById("A").getContext('2d');
  20. var myChart = new Chart(ctx, {
  21. type: 'line',
  22. data: {
  23. labels: ['2016.05.08', '2016.11.06', '2017.05.07', '2017.11.05', '2018.05.06', '2018.11.04', '2019.05.05', '2019.11.03', '2020.05.03', '2020.11.01', '2021.05.02'],
  24. datasets: [{
  25. label: "Search interest",
  26. fill: true,
  27. data: [0.0, 0.65, 0.3, 0.6, 2.7, 6.2, 16.3, 23.65, 53.45, 43.75, 59.27272727272727],
  28. borderColor: 'rgba(55,48,163,0.8)',
  29. backgroundColor: 'rgba(55,48,163,0.5)',
  30. pointHoverRadius: 10
  31. }]
  32. },
  33. options: {
  34. animation: {
  35. duration: 0
  36. },
  37. plugins: {
  38. legend: {
  39. display: false,
  40. positon: "bottom"
  41. },
  42. deferred: {
  43. }
  44. },
  45. elements: {
  46. point: {
  47. radius: 2
  48. }
  49. },
  50. scales: {
  51. y: {
  52. beginAtZero: true,
  53. display: false,
  54. max: 100
  55. },
  56. x: {
  57. display: false,
  58. position: "bottom"
  59. }
  60. }
  61. }
  62. });
  63. </script>
  64. </div>
  65. <div class="mx-auto max-w-md">
  66. <canvas id="B" width="400" height="400"></canvas>
  67. </div>
  68. <script>
  69. var ctx = document.getElementById("B").getContext('2d');
  70. var myChart = new Chart(ctx, {
  71. type: 'line',
  72. data: {
  73. labels: ['2016.05.08', '2016.11.06', '2017.05.07', '2017.11.05', '2018.05.06', '2018.11.04', '2019.05.05', '2019.11.03', '2020.05.03', '2020.11.01', '2021.05.02'],
  74. datasets: [{
  75. label: "Search interest",
  76. fill: true,
  77. data: [0.0, 0.65, 0.3, 0.6, 2.7, 6.2, 16.3, 23.65, 53.45, 43.75, 59.27272727272727],
  78. borderColor: 'rgba(55,48,163,0.8)',
  79. backgroundColor: 'rgba(55,48,163,0.5)',
  80. pointHoverRadius: 10
  81. }]
  82. },
  83. options: {
  84. plugins: {
  85. legend: {
  86. display: false,
  87. positon: "bottom"
  88. },
  89. deferred: {
  90. }
  91. },
  92. elements: {
  93. point: {
  94. radius: 2
  95. }
  96. },
  97. scales: {
  98. y: {
  99. beginAtZero: true,
  100. display: false,
  101. max: 100
  102. },
  103. x: {
  104. display: false,
  105. position: "bottom"
  106. }
  107. }
  108. }
  109. });
  110. </script>
  111. <div class="mx-auto max-w-md">
  112. <canvas id="C" width="400" height="400"></canvas>
  113. </div>
  114. <script>
  115. var ctx = document.getElementById("C").getContext('2d');
  116. var myChart = new Chart(ctx, {
  117. type: 'line',
  118. data: {
  119. labels: ['2016.05.08', '2016.11.06', '2017.05.07', '2017.11.05', '2018.05.06', '2018.11.04', '2019.05.05', '2019.11.03', '2020.05.03', '2020.11.01', '2021.05.02'],
  120. datasets: [{
  121. label: "Search interest",
  122. fill: true,
  123. data: [0.0, 0.65, 0.3, 0.6, 2.7, 6.2, 16.3, 23.65, 53.45, 43.75, 59.27272727272727],
  124. borderColor: 'rgba(55,48,163,0.8)',
  125. backgroundColor: 'rgba(55,48,163,0.5)',
  126. pointHoverRadius: 10
  127. }]
  128. },
  129. options: {
  130. plugins: {
  131. legend: {
  132. display: false,
  133. positon: "bottom"
  134. },
  135. deferred: {
  136. }
  137. },
  138. elements: {
  139. point: {
  140. radius: 2
  141. }
  142. },
  143. scales: {
  144. y: {
  145. beginAtZero: true,
  146. display: false,
  147. max: 100
  148. },
  149. x: {
  150. display: false,
  151. position: "bottom"
  152. }
  153. }
  154. }
  155. });
  156. </script>
  157. <div class="mx-auto max-w-md">
  158. <canvas id="D" width="400" height="400"></canvas>
  159. </div>
  160. <script>
  161. var ctx = document.getElementById("D").getContext('2d');
  162. var myChart = new Chart(ctx, {
  163. type: 'line',
  164. data: {
  165. labels: ['2016.05.08', '2016.11.06', '2017.05.07', '2017.11.05', '2018.05.06', '2018.11.04', '2019.05.05', '2019.11.03', '2020.05.03', '2020.11.01', '2021.05.02'],
  166. datasets: [{
  167. label: "Search interest",
  168. fill: true,
  169. data: [0.0, 0.65, 0.3, 0.6, 2.7, 6.2, 16.3, 23.65, 53.45, 43.75, 59.27272727272727],
  170. borderColor: 'rgba(55,48,163,0.8)',
  171. backgroundColor: 'rgba(55,48,163,0.5)',
  172. pointHoverRadius: 10
  173. }]
  174. },
  175. options: {
  176. plugins: {
  177. legend: {
  178. display: false,
  179. positon: "bottom"
  180. },
  181. deferred: {
  182. }
  183. },
  184. elements: {
  185. point: {
  186. radius: 2
  187. }
  188. },
  189. scales: {
  190. y: {
  191. beginAtZero: true,
  192. display: false,
  193. max: 100
  194. },
  195. x: {
  196. display: false,
  197. position: "bottom"
  198. }
  199. }
  200. }
  201. });
  202. </script>
  203. </body>

编辑:

随着延迟插件版本2的发布,它与chart.js V3兼容

展开查看全部

相关问题