#my charts.js chart not being displayed in a modal.
1 messages · Page 1 of 1 (latest)
A problem with your code. Please read #how-to-get-help
private initGraph() {
this.downtimeChart = new Chart('chart', {
type: 'doughnut',
data: {
labels: ['Porucha stroja', 'Problém s kvalitou', 'Nedostatok materiálu', 'Nastavenie stroja', 'Príprava', 'Iné', 'Produkcia'],
datasets: [
{
data: [3, 4, 3, 5, 2, 3, 75],
borderWidth: 0,
backgroundColor: [
'#E04958', '#5197FD', '#D3D8DE', '#FD9843', '#FFC721', 'black', 'green'
],
},
],
},
options: {
responsive: true,
plugins: {
tooltip: {
callbacks: {
label: function (tooltipItem) {
const label = tooltipItem.label || '';
const value = tooltipItem.raw;
return `${label}: ${value}`;
},
}
},
legend: {
display: false,
position: 'top',
},
},
},
});
}
<canvas id="graph">{{ downtimeChart }}</canvas>
hope this helps
Yeah, that can't work. Interpolation in Angular is used to display strings. So that would just transform your Chart object into a string by calling the toString() method, probably returning something like [object Object], and display that.
You need to read the documentation of chart.js to understand how it works. See https://www.chartjs.org/docs/latest/getting-started/. Note how the first argument of the Chart constructor is an HTMLCanvsElement whare you want to display that chart.
In Angular, you would use a viewChild to get it, and call afterNextRender to pass the canvas to your chart.
Something like
@Component({
template: '<canvas #chart></canvas>'
})
class MyComponent {
readonly canvas = viewChild.required<ElementRef<HTMLCanvasElement>>('chart');
constructor() {
afterNextRender(() => {
new Chart(this.canvas().nativeElement, { ... });
});
}
}
Open source HTML5 Charts for your website
thats weird because i have a same setup of charts in a different component. it works there. i thought maybe it wont work like this because now i am using it in a modal
You're passing the id of an element in your code. This ID is 'chart'. But there is no such ID in your template.
But even then, that would only work if the element with that ID already exists in the DOM. And using afterNextRender makes sure the canvas exists.
thx, will try
im sorry but this approach aint working, i used afterNextRender and still it aint working. here is the code:
constructor(
private _downtimeService: DowntimeService,
) {
afterNextRender(() => {
this.initGraph()
})
}
private initGraph() {
const canvas = document.getElementById('downtimeChart') as HTMLCanvasElement;
new Chart(canvas, {content of the graph}
}
<canvas id="downtimeChart"></canvas>
@ViewChild('chartCanvas', { static: false }) chartCanvas!: ElementRef<HTMLCanvasElement>;
I don't get why you're not using the code I suggested instead of using getElementById, but anyway, without a complete minimal reproduction, it's hard to help more.
@ViewChild('chartCanvas', { static: false }) chartCanvas!: ElementRef<HTMLCanvasElement>;
constructor(
private _downtimeService: DowntimeService,
) {
afterNextRender(() => {
this.initGraph()
})
}
private initGraph() {
const canvas = document.getElementById('downtimeChart') as HTMLCanvasElement;
new Chart(canvas, {
type: 'doughnut',
data: {
labels: ['Porucha stroja', 'Problém s kvalitou', 'Nedostatok materiálu', 'Nastavenie stroja', 'Príprava', 'Iné', 'Produkcia'],
datasets: [
{
data: [3, 4, 3, 5, 2, 3, 75],
borderWidth: 0,
backgroundColor: [
'#E04958', '#5197FD', '#D3D8DE', '#FD9843', '#FFC721', 'black', 'green'
],
},
],
},
options: {
responsive: true,
plugins: {
tooltip: {
callbacks: {
label: function (tooltipItem) {
const label = tooltipItem.label || '';
const value = tooltipItem.raw;
return `${label}: ${value}`;
},
}
},
legend: {
display: false,
position: 'top',
},
},
},
});
}
<canvas id="downtimeChart"></canvas>
this is the minimal reproduction
idk what more u need
Read #how-to-get-help to learn how to use stackblitz to provide an actual minimal reproduction.
i would spend a good 2 hours creating that stackblits
i provided the code for the graphs. i dont think anything other is needed the graph is being rendered in modal
And you find it more logical that I spend those two hours to do it, to help you fix your issue?