import { PlotlyFigure } from '../../../features/indicadores/indicadores.models'; import { BaselineCurve, EegMetrics } from '../../api/tea-eeg.types'; const OUTSIDE = [ { flag: -1, label: 'Bajo', color: 'blue', symbol: 'triangle-down' }, { flag: 0, label: 'Dentro', color: 'purple', symbol: 'circle' }, { flag: 1, label: 'Alto', color: 'vermilion', symbol: 'triangle-up' }, { flag: null, label: 'Sin clasificación', color: 'axis', symbol: 'diamond-open' }, ] as const; export function overlayPoints(eegs: EegMetrics[], metric: string) { return eegs.flatMap((eeg) => eeg.age_months === null ? [] : eeg.metrics .filter((item) => item.name === metric && Number.isFinite(item.value)) .map((item) => ({ id: eeg.eeg_id, age: eeg.age_months!, value: item.value, outside: item.outside, label: OUTSIDE.find((group) => group.flag === item.outside)?.label, }))); } export function baselineFigure( curve: BaselineCurve, points: ReturnType, color: (token: string) => string, ink: string, ): PlotlyFigure { const x = curve.edad_meses; const band = (low: number[], high: number[], name: string, token: string) => ({ type: 'scatter', mode: 'lines', name, x: [...x, ...[...x].reverse()], y: [...high, ...[...low].reverse()], fill: 'toself', fillcolor: color(token), line: { width: 0 }, hoverinfo: 'skip', }); return { data: [ band(curve.p5, curve.p95, 'P5–P95', 'band-outer'), band(curve.p25, curve.p75, 'P25–P75', 'band-inner'), { type: 'scatter', mode: 'lines', name: 'Mediana', x, y: curve.mediana, line: { color: color('blue'), width: 2 } }, ...OUTSIDE.flatMap(({ flag, label, color: token, symbol }) => { const group = points.filter((point) => point.outside === flag); return group.length ? [{ type: 'scatter', mode: 'markers', name: label, x: group.map((point) => point.age), y: group.map((point) => point.value), text: group.map((point) => `EEG ${point.id}`), marker: { color: token === 'axis' ? ink : color(token), symbol, size: 10 }, }] : []; }), ], layout: { autosize: true, height: 400, margin: { l: 65, r: 20, t: 20, b: 100 }, paper_bgcolor: 'transparent', plot_bgcolor: 'transparent', font: { color: ink }, xaxis: { title: { text: 'Edad (meses)' }, showgrid: false, zeroline: false }, yaxis: { title: { text: curve.metric }, showgrid: false, zeroline: false }, legend: { orientation: 'h', y: -.22 }, }, }; }