Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

204
Visualizações
how to make bar total in chartjs same like user input?

I want to make the total bars 12 when i input 12. how to make the length of variable labels same like my input???

this is my code:

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

  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
  );
}

this is my result

result

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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
  );
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda