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

313
Views
Chart showing just after resizing window - Using chart.js and Firebase database

I'm trying to make a chart using chart.js and for that I'm retrieving data from Firebase Realtime Database. I'm able to get the data and put on the chart, the problem is the chart is showing on the screen just after resize the window or alt+tab. I'm using this code for that:

var rent_data = [];

auth.onAuthStateChanged(function(user) {
      if (user != null) {
        const db1 = ...;
        db1.once('value').then(function (snapshot) {
            snapshot.forEach(function(childSnaphot){
                rent_data.push(childSnaphot.val())
            })
         })

        var ctx = document.getElementById('chart');
        var labelms = ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez']
        var config = {   
            type: 'line',
            data: {
                labels: labelms,
                datasets: [{
                    label: '# of Votes',
                    data: rent_data,
                    backgroundColor: [
                        'rgba(255, 99, 132, 0.2)',
                        'rgba(54, 162, 235, 0.2)',
                        'rgba(255, 206, 86, 0.2)',
                        'rgba(75, 192, 192, 0.2)',
                        'rgba(153, 102, 255, 0.2)',
                        'rgba(255, 159, 64, 0.2)'
                    ],
                    borderColor: [
                        'rgba(255, 99, 132, 1)',
                        'rgba(54, 162, 235, 1)',
                        'rgba(255, 206, 86, 1)',
                        'rgba(75, 192, 192, 1)',
                        'rgba(153, 102, 255, 1)',
                        'rgba(255, 159, 64, 1)'
                    ],
                    borderWidth: 1
                }]
            }
           }

        var chart2 = new Chart (ctx, config);

}})

The code is missing some parts, like authentication in firebase but I don't think it's crucial since it's working after resize.

I tried to put chart.update() after var chart but it doesn't do nothing.

Some images just to show the output:

First chart

Chart after resize

I'm new on the javascript and maybe I missing something simple that I don't know yet. On another questions I saw something related to async functions but I didn't figure it out where do I need to use then.

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
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!