【问题标题】:How to add multiple chart js script in html如何在html中添加多个图表js脚本
【发布时间】:2023-01-27 12:01:45
【问题描述】:

我想从图表 js 库创建图表。条形图的脚本代码是用一些 .js 文件编写的,这意味着它现在不在 html 中,我想在我这样做时将该脚本添加到我的 html 中,然后图表没有准备好,然后我尝试了window.onload=function(){Here is the code snippet of chartjs dataset}。这个工作正常的一张图表准备好了,但我想要它 4 次相同的图表但不同的 id 然后它不起作用它只在最后一个 div 中打印图表

window.onload = function () {
  window.myBar = new Chart(document.getElementById("bars1"), {
    type: "bar",
    data: {
      labels: ["January", "February", "March", "April", "May", "June", "July"],
      datasets: [
        {
          label: "Bags",
          backgroundColor: "#7e3af2",
          // borderColor: window.chartColors.blue,
          borderWidth: 1,
          data: [66, 33, 43, 12, 54, 62, 84, 100],
        },
      ],
    },
    options: {
      responsive: true,
      legend: {
        display: false,
      },
    },
  });
};
<div class="min-w-0 p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
<h4 class="mb-4 font-semibold text-gray-800 dark:text-gray-300">
    Bars
</h4>
<canvas id="bars1"></canvas>
<div class="flex justify-center mt-4 space-x-3 text-sm text-gray-600 dark:text-gray-400">

</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

相同的图表我想要 4 次如果我在 canavs 标签和 getelementbyid 中使用不同的 id 将这个相同的 div 粘贴到这个 div 下面然后它不会工作只有最后一个 div 图表被打印

解释

【问题讨论】:

  • 请edit你的问题来纠正你的语法。正如所写,您的问题包括令人困惑的连续句子。

标签: javascript html


【解决方案1】:

这能解决您的问题吗?如果不是请提供更多详情

let ids = ['bars1', 'bars2', 'bars3', 'bars4']

ids.forEach(id => {
  window.myBar = new Chart(document.getElementById(id), {
      type: "bar",
      data: {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [
          {
            label: "Bags",
            backgroundColor: "#7e3af2",
            // borderColor: window.chartColors.blue,
            borderWidth: 1,
            data: [66, 33, 43, 12, 54, 62, 84, 100],
          },
        ],
      },
      options: {
        responsive: true,
        legend: {
          display: false,
        },
      },
    });
})
<canvas id="bars1"></canvas>
<canvas id="bars2"></canvas>
<canvas id="bars3"></canvas>
<canvas id="bars4"></canvas>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

【讨论】:

  • 在条形图中,我得到一个带有小紫色的袋子标签,我想删除它那个颜色框和标签是的,我可以从代码中删除标签名称“bags”,但是那个颜色框如何
【解决方案2】:

您可以只存储所有画布元素的 id,并在该数组上循环,同时将图表添加到具有 id 的相应元素

<canvas id="bars1"></canvas>
<canvas id="bars2"></canvas>
<canvas id="bars3"></canvas>
<canvas id="bars4"></canvas>

<script>
window.onload = function(){
    const ids = ['bars1', 'bars2', 'bars3', 'bars4']
    ids.forEach((id) => {
        window.myBar = new Chart(document.getElementById(id),
                  {
                    type: 'bar',
                    data: {
                      labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
                      datasets: [
                      
                        {
                          label: 'Bags',
                          backgroundColor: '#7e3af2',
                          // borderColor: window.chartColors.blue,
                          borderWidth: 1,
                          data: [66, 33, 43, 12, 54, 62, 84,100],
                        },
                        
                      ],
                    },
                    options: {
                      responsive: true,
                      legend: {
                        display: false,
                      },
                    },
                  }
                )
              }
    })
                  
</script>

【讨论】:

    猜你喜欢
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 2020-09-19
    • 2018-10-01
    • 1970-01-01
    相关资源
    最近更新 更多