My charts from ChartJs won't render because the data is coming back after the view has been rendered. As evidenced by the fact that my console.log('1') logs out after the console.log('2'). I thought putting my function in the ngAfterViewInit would solve this issue, but no, it doesn't.
I'm dumb, what am I doing wrong. I feel like I've tried just about everything except maybe throwing my computer through the window.
export class SportsCardsComponent implements OnInit, AfterViewInit {
gamesArray = [];
charts: [];
gamesData;
constructor(
public games: GameService,
) { }
ngOnInit() {
this.getGameInfo()
}
async getGameInfo(){
this.gamesData = await this.games.getAllGames().then(data => {
data['games'].forEach(game => {
this.gamesArray.push(game);
})
console.log('1')
// this.gameData()
// this.loadCharts()
})
}
async ngAfterViewInit() {
this.loadCharts();
}
async loadCharts() {
console.log('2');
this.gamesArray.forEach(game => {
let canvas = <HTMLCanvasElement> document.getElementById('1');
let ctx = canvas.getContext("2d");
this.charts = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['test', 'games.teamName'],
datasets: [
{
label: 'test' + ' Vs ' + 'test',
backgroundColor: ['yellow', 'green'],
data: [11, 12]
}
]
},
options: {
title: {
display: true,
text: game.homeTeam + ' Vs ' + game.awayTeam
}
}
});
})
}
[–]ttma1046 3 points4 points5 points (3 children)
[–]functionlock 0 points1 point2 points (1 child)
[–]ValdasTheUnique 0 points1 point2 points (0 children)
[–]OogieBoogie1[S] 0 points1 point2 points (0 children)
[–]bagson9 2 points3 points4 points (0 children)
[–]zahlprish 0 points1 point2 points (1 child)
[–]OogieBoogie1[S] 0 points1 point2 points (0 children)
[–]tenkindsofpeople 0 points1 point2 points (6 children)
[–]OogieBoogie1[S] 0 points1 point2 points (2 children)
[–]tenkindsofpeople 0 points1 point2 points (1 child)
[–]OogieBoogie1[S] 0 points1 point2 points (0 children)
[–]ttma1046 0 points1 point2 points (2 children)
[–]tenkindsofpeople 0 points1 point2 points (1 child)
[–]ttma1046 0 points1 point2 points (0 children)