I'm writing an application which pulls data from a server into a wrapper component, formats the data, and passes it to multiple chart components. However, when I go to create the chart object, I get the error: TypeError: item is null.
Here's my code:
dashboard.component.html
<h1>A</h1>
<app-chart *ngIf="dataLoaded | async" [type]="'charta'" [data]="{labels: this.data.labels, readings: this.data.charta}"></app-chart>
<h1>B</h1>
<app-chart *ngIf="dataLoaded | async" [type]="'chartb'" [data]="{labels: this.data.labels, readings: this.data.chartb}"></app-chart>
chart.component.html
<div style="display: block;">
<canvas [attr.id]="type" width="400" height="400" >{{ chart }}</canvas>
</div>
chart.component.ts
....
export class ChartComponent implements OnInit {
@Input() data: any;
@Input() type: string;
chart: any;
createChart() {
const canvas = this.elementRef.nativeElement.querySelector(`#${this.type}`);
this.chart = new Chart(canvas, {....});
}
....
ngAfterContentInit() {
this.createChart();
}
}
Any ideas?