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

314
Vistas
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
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