Chartjs-Generate multi chart with forloop

0👍

It looks like you’re getting an error as it’s trying to create multiple charts with the same id. Can you try modifying your loop by using the index to create unique id for each chart:

    for(let i = 0; i <= ctxs.length;i++){
      console.log(ctxs[i].id)
      for(let z = 0; z < allxValue.length; z++){
        for(let y = 0; y < allyValue.length; y++){
          var idiv = document.querySelector('#myChart' + (i + 1)).getContext('2d');
          const chart = new Chart('myChart' + (i + 1), {
            type: "doughnut",
            data: {
              labels: allxValue[z],
              datasets: [{   
                data: allyValue[y]
              }]
            },
            options: {}
          });     
        }
      }
    }

This will create charts with IDs myChart1, myChart2, and myChart3, which should fix the error you are seeing.

I hope this helps!

Leave a comment