ChartJS 我无法使用Chart JS添加多个图表

sshcrbum  于 2022-11-06  发布在  Chart.js
关注(0)|答案(2)|浏览(240)

我正在制作一个需要显示图表的网站。为此,我决定使用图表JS。
问题是,当我试图在同一页上加载多个图表时,我无法做到。
由于某种原因,它只加载了一个图表,但第二个图表没有加载。
我已经检查了变量名称和ID,我没有看到失败可能在哪里。
谢谢你,谢谢你

  1. <!DOCTYPE html>
  2. <html lang="es">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Title</title>
  8. <link rel="stylesheet" href="./assets/css/normalize.css">
  9. <link rel="stylesheet" href="./assets/css/style.css">
  10. </head>
  11. <body>
  12. <!-- CONTENIDO PRINCIPAL -->
  13. <main id="main-content">
  14. <div class="general-chart">
  15. <span>X€</span><br>
  16. <span style="font-size: 0.9em; font-weight: 400;">in Octobre</span>
  17. <canvas id="myChart"></canvas>
  18. </div>
  19. <div class="income-chart">
  20. <canvas id="myChart_second"></canvas>
  21. </div>
  22. </main>
  23. <!-- CHART JS -->
  24. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.min.js"
  25. integrity="sha512-ElRFoEQdI5Ht6kZvyzXhYG9NqjtkmlkfYk0wr6wHxU9JEHakS7UJZNeml5ALk+8IKlU6jDgMabC3vkumRokgJA=="
  26. crossorigin="anonymous"
  27. referrerpolicy="no-referrer">
  28. </script>
  29. <!-- Scripts -->
  30. <script src="./js/menu.js"></script>
  31. <!-- GRAFICO SALDO -->
  32. <script>
  33. const ctx = document.getElementById("myChart").getContext("2d");
  34. const labels = [
  35. '2012',
  36. '2013',
  37. '2014',
  38. '2015',
  39. '2016',
  40. '2017',
  41. '2018',
  42. '2019',
  43. '2020',
  44. ];
  45. //Gradient Fill
  46. var gradient = ctx.createLinearGradient(0, 0, 0, 400);
  47. gradient.addColorStop(0, 'rgba(105,53,211, 1');
  48. gradient.addColorStop(1, 'rgba(105,53,211, 0.2');
  49. const data = {
  50. labels,
  51. datasets: [
  52. {
  53. data: [211, 326, 165, 350, 420, 370, 500, 376, 415],
  54. label: 'Titulo',
  55. fill: true,
  56. backgroundColor: gradient,
  57. borderColor: '#6935D3',
  58. pointBackgroundColor: '#4D1BB1',
  59. },
  60. ],
  61. };
  62. const config = {
  63. type: 'line',
  64. data: data,
  65. options: {
  66. responsive: true,
  67. plugins: {legend:{display: false}},
  68. scales: {
  69. x: {
  70. grid: {
  71. display: false,
  72. drawBorder: false
  73. }
  74. },
  75. y: {
  76. grid: {
  77. display: false,
  78. drawBorder: false
  79. },
  80. ticks: {
  81. display: false
  82. },
  83. }
  84. },
  85. },
  86. };
  87. const myChart = new Chart(ctx, config);
  88. </script>
  89. <!-- GRAFICO INGRESOS -->
  90. <script>
  91. const ctx2 = document.getElementById("myChart_second").getContext("2d");
  92. const labels2 = [
  93. '2012',
  94. '2013',
  95. '2014',
  96. '2015',
  97. '2016',
  98. '2017',
  99. '2018',
  100. '2019',
  101. '2020',
  102. ];
  103. const data2 = {
  104. labels2,
  105. datasets: [
  106. {
  107. data: [211, 326, 165, 350, 420, 370, 500, 376, 415],
  108. label: 'Title',
  109. fill: true,
  110. backgroundColor: gradient,
  111. borderColor: '#6935D3',
  112. pointBackgroundColor: '#4D1BB1',
  113. },
  114. ],
  115. };
  116. const config2 = {
  117. type: 'line',
  118. data: data2,
  119. options: {
  120. responsive: true,
  121. plugins: {legend:{display: false}},
  122. scales: {
  123. x: {
  124. grid: {
  125. display: false,
  126. drawBorder: false
  127. }
  128. },
  129. y: {
  130. grid: {
  131. display: false,
  132. drawBorder: false
  133. },
  134. ticks: {
  135. display: false
  136. },
  137. }
  138. },
  139. },
  140. };
  141. const myChart2 = new Chart(ctx2, config2);
  142. </script>
  143. </body>
  144. </html>
carvr3hs

carvr3hs1#

由于找不到labels2,因此未显示,请在下面进行更改

  1. const data2 = {
  2. labels : labels2, //change here
  3. datasets: [
  4. {
  5. data: [211, 326, 165, 350, 420, 370, 500, 376, 415],
  6. label: 'Title',
  7. fill: true,
  8. backgroundColor: gradient,
  9. borderColor: '#6935D3',
  10. pointBackgroundColor: '#4D1BB1',
  11. },
  12. ],
  13. };
pbpqsu0x

pbpqsu0x2#

您必须使用“标签:labels2”。如果只使用变量,则变量名称将自动用作属性名称。

  1. const data2 = {
  2. labels: labels2,
  3. datasets: [
  4. {
  5. data: [211, 326, 165, 350, 420, 370, 500, 376, 415],
  6. label: 'Title',
  7. fill: true,
  8. backgroundColor: gradient,
  9. borderColor: '#6935D3',
  10. pointBackgroundColor: '#4D1BB1',
  11. },
  12. ],
  13. };

相关问题