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

110
Views
Chart.js doesn't show anything

I'm trying to periodically update a chart. The data is making into the dataset but I can't get it to draw the chart. What am I doing wrong?

const ctx = document.querySelector('#apichart canvas').getContext('2d');
const apichart = new Chart(ctx, {
  type: 'line',
  data: {datasets:[{
    label:'Pressure',
    data: [1025,1000],
    fill:false,
    borderColor: 'rgb(75, 192, 192)',
    tension:0.1
  }]},
  options: {
    scales: {
      y: {
        beginAtZero: true
      }
    }
  }
});

setInterval(function run(){
  apichart.data.datasets[0].data.push(1025 * Math.random());
  apichart.update();
}, 10000);
#apichart{background:#777; position:absolute; top:0; right:0; bottom:0; left:0;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.2/chart.min.js"></script>
<div id="apichart">
    <canvas></canvas>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think this problem related to labels, so I have added labels array and update them in setInterval each time and it works.

const ctx = document.querySelector('#apichart canvas').getContext('2d');
const apichart = new Chart(ctx, {
  type: 'line',
  data: {labels: [1, 2],
datasets:[{
    label:'Pressure',
    data: [1025,1000],
    fill:false,
    borderColor: 'rgb(75, 192, 192)',
    tension:0.1
  }]},
  options: {
    scales: {
      y: {
        beginAtZero: true
      }
    }
  }
});

setInterval(function run(){
apichart.data.labels.push(Math.random());
  apichart.data.datasets[0].data.push(1025 * Math.random());
  apichart.update();
}, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.2/chart.min.js"></script>
<div id="apichart">
    <canvas></canvas>
</div>

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!