|
| 1 | +import { |
| 2 | + afterNextRender, |
| 3 | + ChangeDetectionStrategy, |
| 4 | + Component, |
| 5 | + CUSTOM_ELEMENTS_SCHEMA, |
| 6 | + effect, |
| 7 | + ElementRef, |
| 8 | + inject, |
| 9 | + Injector, |
| 10 | + viewChild, |
| 11 | +} from '@angular/core'; |
| 12 | +import { NgtArgs, NgtHTML } from 'angular-three'; |
| 13 | +import { NgtsOrbitControls } from 'angular-three-soba/controls'; |
| 14 | +import { NgtsHTML, NgtsHTMLContent } from 'angular-three-soba/misc'; |
| 15 | + |
| 16 | +declare const Chart: any; |
| 17 | + |
| 18 | +@Component({ |
| 19 | + selector: 'app-chart-container', |
| 20 | + standalone: true, |
| 21 | + template: ` |
| 22 | + <canvas #chartContainer></canvas> |
| 23 | + `, |
| 24 | + host: { |
| 25 | + class: 'block w-[320px]', |
| 26 | + }, |
| 27 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 28 | +}) |
| 29 | +export class ChartContainer extends NgtHTML { |
| 30 | + chartContainer = viewChild.required<ElementRef<HTMLCanvasElement>>('chartContainer'); |
| 31 | + data = Array.from({ length: 6 }, () => Math.random() * 100); |
| 32 | + |
| 33 | + constructor() { |
| 34 | + super(); |
| 35 | + // NOTE: I'm doing this dirty because I am lazy. |
| 36 | + const injector = inject(Injector); |
| 37 | + 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 | + |
| 47 | + effect( |
| 48 | + (onCleanup) => { |
| 49 | + const id = setInterval(() => { |
| 50 | + // randomize the data |
| 51 | + this.data.forEach((_, index) => { |
| 52 | + this.data[index] = Math.random() * 100; |
| 53 | + }); |
| 54 | + chart.update(); |
| 55 | + }, 1000); |
| 56 | + onCleanup(() => clearInterval(id)); |
| 57 | + }, |
| 58 | + { injector }, |
| 59 | + ); |
| 60 | + }); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +@Component({ |
| 65 | + standalone: true, |
| 66 | + template: ` |
| 67 | + <ngt-color attach="background" *args="['#f78b3d']" /> |
| 68 | +
|
| 69 | + <ngt-ambient-light /> |
| 70 | + <ngt-directional-light [intensity]="2 * Math.PI" [position]="3" /> |
| 71 | +
|
| 72 | + <ngt-mesh> |
| 73 | + <ngt-box-geometry *args="[8.5, 4, 0.5]" /> |
| 74 | + <ngt-mesh-toon-material color="#fbb03b" /> |
| 75 | +
|
| 76 | + <ngts-html [options]="{ occlude: true, transform: true, position: [0, 0, 0.3] }"> |
| 77 | + <div [ngts-html-content]="{ distanceFactor: 0 }"> |
| 78 | + <app-chart-container /> |
| 79 | + </div> |
| 80 | + </ngts-html> |
| 81 | + </ngt-mesh> |
| 82 | +
|
| 83 | + <ngts-orbit-controls [options]="{ autoRotate: true }" /> |
| 84 | + `, |
| 85 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 86 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 87 | + imports: [NgtArgs, NgtsOrbitControls, NgtsHTML, NgtsHTMLContent, ChartContainer], |
| 88 | +}) |
| 89 | +export class Experience { |
| 90 | + protected readonly Math = Math; |
| 91 | +} |
0 commit comments