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:
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!