Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!