[Chartjs]-How to make bar total in chartjs same like user input?

1👍

This is because chart.js expects an array so you will need to make an array containing 12 labels first instead of passing the number 12 to the labels array:

function updateChart() {
  let val = $("#num-input").val()
  let valNum = parseInt(val)
  console.log(valNum)
  
  let labels = []; // this length = bar total

  for (let i = 0; i < $("#num-input").val(); i++) {
    labels.push(i) // or any other value you want your label to have
  }

  const data = {
    labels: labels,
    datasets: [{
      backgroundColor: 'rgb(255, 99, 132)',
      borderColor: 'rgb(255, 99, 132)',
      data: [10, 20, 5, 2, 20, 30, 45], // = value for each bar
    }]
  };

  const config = {
    type: 'bar',
    data: data,
    options: {}
  };

  const myChart = new Chart(
    document.getElementById('myChart'),
    config
  );
}

Leave a comment