I need to display a graph in my Angular project. A simple doughnut chart with 3 datasets. When I try to run the project, the console throws the following error:
"Failed to create chart: can't acquire context from the given item"
I've read the documentation, used the templates and even checked on few questions of StackOverflow :
- chart.js Failed to create chart: can't acquire context from the given item
- Using Chart.js on Angular 4
- Angular/Chart.js error: Failed to create chart: can't acquire context from the given item
None of them worked and I think that it owes to the typescript doesn't "find" the ID of the < canvas >, but I sincerely don't know what am I doing wrong
Here's the code:
HTML
<canvas #grafico id="grafico">{{ grafico }}</canvas>
TypeScript
import Chart = require('chart.js');
grafico: any;
this.grafico = new Chart('grafico',{
type:"doughnut",
data:{
labels: ['Expirados', 'Sem exames', 'Proximo de expirar'],
datasets:[
{ // Exames expirados
data:res[0].num_funcion_expir, // Data from API
backgroundColor:'#EA3100'
},
{ // Exames expirados
data:res[0].num_funcion_sem_exam_ini, // Data from API
backgroundColor:'#ff6384'
},
{ // Exames expirados
data:res[0].num_funcion_expir_prox_exam, // Data from API
backgroundColor:'#EAA900'
},
]
},
options:{
legend:{
display:true
}
}
});
Any help is appreciated
