Skip to content

Commit dc50932

Browse files
committed
docs: make sure script for chartjs is ready before init chart
1 parent 0bef2f4 commit dc50932

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

apps/kitchen-sink/src/app/soba/html-chart/experience.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ElementRef,
88
inject,
99
Injector,
10+
signal,
1011
viewChild,
1112
} from '@angular/core';
1213
import { NgtArgs, NgtHTML } from 'angular-three';
@@ -35,17 +36,20 @@ export class ChartContainer extends NgtHTML {
3536
// NOTE: I'm doing this dirty because I am lazy.
3637
const injector = inject(Injector);
3738
afterNextRender(() => {
38-
const chart = new Chart(this.chartContainer().nativeElement, {
39-
type: 'bar',
40-
data: {
41-
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
42-
datasets: [{ label: '# of Votes', data: this.data, borderWidth: 1 }],
43-
},
44-
options: { scales: { y: { beginAtZero: true } } },
45-
});
46-
4739
effect(
4840
(onCleanup) => {
41+
const chartReady = Experience.chartReady();
42+
if (!chartReady) return;
43+
44+
const chart = new Chart(this.chartContainer().nativeElement, {
45+
type: 'bar',
46+
data: {
47+
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
48+
datasets: [{ label: '# of Votes', data: this.data, borderWidth: 1 }],
49+
},
50+
options: { scales: { y: { beginAtZero: true } } },
51+
});
52+
4953
const id = setInterval(() => {
5054
// randomize the data
5155
this.data.forEach((_, index) => {
@@ -88,4 +92,6 @@ export class ChartContainer extends NgtHTML {
8892
})
8993
export class Experience {
9094
protected readonly Math = Math;
95+
96+
static chartReady = signal(false);
9197
}

apps/kitchen-sink/src/app/soba/html-chart/html-chart.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class HtmlChart {
2323
script = document.createElement('script');
2424
script.src = 'https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js';
2525
document.head.appendChild(script);
26+
Experience.chartReady.set(true);
2627
});
2728

2829
inject(DestroyRef).onDestroy(() => {

0 commit comments

Comments
 (0)