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') { } @if (status() === 'error') {

No se pudo representar la figura. Puedes reintentar.

}
@if (showNotes && status() === 'ready' && notes.length) {

Notas de la figura

@for (note of notes; track $index) {

}
} `, 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; readonly renderError = output(); readonly widthChanged = output(); readonly plotClick = output(); 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(); } }