Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda