import { Component, DestroyRef, inject, Input, OnInit, signal } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { PlotlyChart } from '../../shared/plotly/plotly-chart'; import { PlotlyFigure } from './indicadores.models'; import { TeaAdos2, TeaEeg, TeaPacienteDetalle, TeaSeguimientoService } from './tea-seguimiento.service'; const CLASIFICACION: Record = { autismo: 'Autismo', espectro_autista: 'Espectro autista', no_espectro: 'No espectro' }; // ADOS-2 calibrated severity score ranges. const NIVEL_CSS: Record = { minimo: 'Evidencia mínima o nula', bajo: 'Nivel bajo', moderado: 'Nivel moderado', alto: 'Nivel alto' }; const CALIDAD: Record = { buena: 'Buena', aceptable: 'Aceptable', deficiente: 'Deficiente' }; const INK = '#081839'; // Shape paths have no arcs: the hole is a polygon, wound against the frame so it stays empty under either fill rule. const HOLE = Array.from({ length: 96 }, (_, i) => -2 * Math.PI * i / 96) .map((a, i) => `${i ? 'L' : 'M'} ${Math.cos(a).toFixed(4)} ${Math.sin(a).toFixed(4)}`).join(' '); // A white frame with a round hole hides the map outside the head; then the head outline, nose and ears. const HEAD_SHAPES = [ { type: 'path', path: `M -2 -2 L 2 -2 L 2 2 L -2 2 Z ${HOLE} Z`, fillrule: 'evenodd', fillcolor: '#FFFFFF', line: { width: 0 } }, { type: 'circle', xref: 'x', yref: 'y', x0: -1, y0: -1, x1: 1, y1: 1, line: { color: INK, width: 2 } }, { type: 'path', path: 'M -0.13 0.99 L 0 1.13 L 0.13 0.99', line: { color: INK, width: 2 } }, { type: 'path', path: 'M -0.99 0.14 Q -1.1 0.12 -1.08 0 Q -1.1 -0.12 -0.99 -0.14', line: { color: INK, width: 2 } }, { type: 'path', path: 'M 0.99 0.14 Q 1.1 0.12 1.08 0 Q 1.1 -0.12 0.99 -0.14', line: { color: INK, width: 2 } }, ]; export function mapaFigure(eeg: TeaEeg): PlotlyFigure { const m = eeg.mapa; return { data: [ { type: 'contour', x: m.x, y: m.y, z: m.z, zmin: -3, zmax: 3, zmid: 0, colorscale: 'RdBu', connectgaps: false, contours: { coloring: 'heatmap', showlines: true }, line: { width: .5, color: 'rgba(8,24,57,.3)' }, colorbar: { title: { text: 'z', side: 'top' }, thickness: 12, len: .8 }, hovertemplate: 'z = %{z:.2f}' }, { type: 'scatter', mode: 'markers+text', x: m.electrodos.map(e => e.x), y: m.electrodos.map(e => e.y), text: m.electrodos.map(e => e.nombre), textposition: 'top center', textfont: { size: 9, color: INK }, marker: { size: 5, color: INK }, hoverinfo: 'text', showlegend: false }, ], layout: { margin: { l: 8, r: 8, t: 8, b: 8 }, showlegend: false, shapes: HEAD_SHAPES, paper_bgcolor: 'rgba(0,0,0,0)', plot_bgcolor: 'rgba(0,0,0,0)', xaxis: { visible: false, range: [-1.2, 1.2], fixedrange: true }, yaxis: { visible: false, range: [-1.15, 1.2], scaleanchor: 'x', fixedrange: true }, }, config: { displayModeBar: false }, }; } @Component({ selector: 'app-tea-paciente-resumen', imports: [PlotlyChart], templateUrl: './tea-paciente-resumen.html', styleUrl: './tea-paciente-resumen.scss', }) export class TeaPacienteResumen implements OnInit { @Input({ required: true }) id = ''; private readonly service = inject(TeaSeguimientoService); private readonly destroy = inject(DestroyRef); readonly status = signal<'loading' | 'ready' | 'error'>('loading'); readonly detalle = signal(undefined); figura?: PlotlyFigure; readonly clasificacion = CLASIFICACION; readonly nivelCss = NIVEL_CSS; readonly calidad = CALIDAD; ngOnInit() { this.load(); } load() { this.status.set('loading'); this.service.detalle(this.id).pipe(takeUntilDestroyed(this.destroy)).subscribe({ next: d => { this.figura = d.eeg ? mapaFigure(d.eeg) : undefined; this.detalle.set(d); this.status.set('ready'); }, error: () => this.status.set('error'), }); } pct(value: number, max: number) { return max > 0 ? Math.round(100 * value / max) : 0; } }