85 lines
4.5 KiB
TypeScript
85 lines
4.5 KiB
TypeScript
import { AfterViewInit, Component, inject, ElementRef, Input, OnChanges, OnDestroy, output, signal, ViewChild } from '@angular/core';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
|
import { PlotlyFigure } from '../../features/indicadores/indicadores.models';
|
|
import { recolorFigure } from '../../features/indicadores/indicadores-c2-palette';
|
|
import { PlotlyController } from './plotly-controller';
|
|
import { PLOTLY_LOADER } from './plotly-runtime';
|
|
|
|
function prefersReducedMotion(): boolean {
|
|
return typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
}
|
|
|
|
@Component({
|
|
selector: 'app-plotly-chart',
|
|
imports: [MatButtonModule, MatProgressBarModule],
|
|
template: `
|
|
@if (status() === 'loading') { <mat-progress-bar mode="indeterminate" aria-label="Representando figura" /> }
|
|
@if (status() === 'error') {
|
|
<div role="alert"><p>No se pudo representar la figura. Puedes reintentar.</p>
|
|
<button mat-stroked-button (click)="retry()">Reintentar renderizado</button></div>
|
|
}
|
|
<div class="plot-viewport" tabindex="0" role="region" [attr.aria-label]="label + ', área de figura'"><div #host class="plot" role="img" [attr.aria-label]="label" [attr.aria-hidden]="status() !== 'ready'"></div></div>
|
|
@if (showNotes && status() === 'ready' && notes.length) {
|
|
<section class="notes" aria-label="Notas completas de la figura">
|
|
<h2>Notas de la figura</h2>
|
|
@for (note of notes; track $index) { <p [innerHTML]="note"></p> }
|
|
</section>
|
|
}
|
|
`,
|
|
styles: `:host { display: block; min-width: 0; } .plot-viewport { min-width: 0; overflow-x: auto; } .plot-viewport:focus-visible { outline: 2px solid var(--issste-text); outline-offset: 2px; } .plot { width: 100%; min-width: var(--plot-min-width, 0px); min-height: var(--plot-height, 620px); visibility: hidden; } [role=alert] { padding: 1rem; } .notes { padding: 1rem; overflow-wrap: anywhere; } .notes h2 { font-size: 1rem; } .notes p { line-height: 1.5; }`,
|
|
})
|
|
export class PlotlyChart implements AfterViewInit, OnChanges, OnDestroy {
|
|
@Input() showNotes = true;
|
|
@Input() label = 'Figura estadística';
|
|
// Opt-in: updates of the same visible figure move smoothly instead of redrawing.
|
|
@Input() animated = false;
|
|
@Input({ required: true }) figure!: PlotlyFigure;
|
|
@ViewChild('host', { static: true }) host!: ElementRef<HTMLElement>;
|
|
readonly renderError = output<void>();
|
|
readonly widthChanged = output<number>();
|
|
readonly plotClick = output<unknown>();
|
|
readonly status = signal<'loading' | 'ready' | 'error'>('loading');
|
|
get notes(): string[] {
|
|
const annotations = this.figure?.layout['annotations'];
|
|
if (!Array.isArray(annotations)) return [];
|
|
return annotations.flatMap(annotation =>
|
|
annotation && typeof annotation.text === 'string' ? [annotation.text] : []);
|
|
}
|
|
private readonly loadPlotly = inject(PLOTLY_LOADER);
|
|
private controller?: PlotlyController;
|
|
private observer?: ResizeObserver;
|
|
private lastWidth = 0;
|
|
private widthFrame?: number;
|
|
private readonly windowResize = () => {
|
|
this.controller?.resize();
|
|
if (this.widthFrame !== undefined) return;
|
|
this.widthFrame = requestAnimationFrame(() => {
|
|
this.widthFrame = undefined;
|
|
const width = Math.floor(this.host.nativeElement.getBoundingClientRect().width);
|
|
if (width > 0 && width !== this.lastWidth) { this.lastWidth = width; this.widthChanged.emit(width); }
|
|
});
|
|
};
|
|
ngAfterViewInit() {
|
|
this.controller = new PlotlyController(this.host.nativeElement, this.loadPlotly, state => {
|
|
this.status.set(state);
|
|
if (state === 'error') this.renderError.emit();
|
|
}, event => this.plotClick.emit(event), { animate: () => this.animated && !prefersReducedMotion() });
|
|
if (typeof ResizeObserver !== 'undefined') {
|
|
this.observer = new ResizeObserver(this.windowResize);
|
|
this.observer.observe(this.host.nativeElement);
|
|
} else window.addEventListener('resize', this.windowResize);
|
|
this.controller.setFigure(recolorFigure(this.figure));
|
|
// Also supply the initial width in browsers without ResizeObserver.
|
|
this.windowResize();
|
|
}
|
|
ngOnChanges() { if (this.figure) this.controller?.setFigure(recolorFigure(this.figure)); }
|
|
retry() { this.controller?.setFigure(recolorFigure(this.figure)); }
|
|
ngOnDestroy() {
|
|
this.observer?.disconnect();
|
|
if (this.widthFrame !== undefined) cancelAnimationFrame(this.widthFrame);
|
|
window.removeEventListener('resize', this.windowResize);
|
|
this.controller?.destroy();
|
|
}
|
|
}
|