import { Component, computed, input, signal } from '@angular/core'; import { MatCheckboxModule } from '@angular/material/checkbox'; import type { Parameters, ParametersParameter } from 'fhir/r4'; import { Plot } from '../plot'; import { curveFigure } from './risk-curve'; export interface SeriesPoint { years: number; p: number; low?: number; high?: number; } /** One series of `$risk-series`: a source, an outcome and her variables. */ export interface Series { id: string; source: string; outcome: string; label: string; resolved: boolean; reasons: string[]; assumptions: string[]; points: SeriesPoint[]; } const OUTCOME_TEXT: Record = { 'CIN2+': 'LIEAG+ (CIN2+)', 'CIN3+': 'CIN3+', cancer: 'Cáncer', }; export const seriesName = (s: Series) => `${s.source} · ${OUTCOME_TEXT[s.outcome] ?? s.outcome}`; /** The series of the answer. A value absent from it stays absent. */ export function parseSeries(answer: Parameters): Series[] { const all = (parts: ParametersParameter[] | undefined, name: string) => (parts ?? []).filter((p) => p.name === name); const one = (parts: ParametersParameter[] | undefined, name: string) => all(parts, name)[0]; return all(answer.parameter, 'series').map(({ part }) => ({ id: one(part, 'id')?.valueString ?? '', source: one(part, 'source')?.valueString ?? '', outcome: one(part, 'outcome')?.valueString ?? '', label: one(part, 'label')?.valueString ?? '', resolved: one(part, 'status')?.valueCode === 'resolved', reasons: all(part, 'reason').map((r) => r.valueCode ?? ''), assumptions: all(part, 'assumption').map((a) => a.valueString ?? ''), points: all(part, 'point').map(({ part: at }) => ({ years: one(at, 'years')?.valueInteger ?? 0, p: one(at, 'probability')?.valueDecimal ?? 0, low: one(at, 'low')?.valueDecimal, high: one(at, 'high')?.valueDecimal, })), })); } /** The chosen series as curves. Series are shown side by side; no difference is computed. */ @Component({ selector: 'app-risk-series', imports: [MatCheckboxModule, Plot], template: `

Cada serie tiene su propio desenlace y población: no se restan entre sí.

@for (s of available(); track s.id) { {{ name(s) }} }
@if (selected().length) { @for (s of selected(); track s.id) { @for (assumption of s.assumptions; track assumption) {

{{ name(s) }}: {{ assumption }}

} } } @if (!available().length) {

No disponible: datos insuficientes.

} `, styles: ` :host { display: block; } .choices { display: flex; flex-wrap: wrap; gap: 0 16px; margin-bottom: 8px; } `, }) export class RiskSeries { readonly series = input.required(); protected readonly name = seriesName; protected readonly available = computed(() => this.series().filter((s) => s.points.length)); /** what the user toggled away from the default (the institutional series) */ private readonly toggled = signal(new Set()); protected readonly chosen = computed( () => new Set( this.available() .filter((s) => (s.source === 'Institucional') !== this.toggled().has(s.id)) .map((s) => s.id), ), ); protected readonly selected = computed(() => this.available().filter((s) => this.chosen().has(s.id)), ); protected readonly figure = computed(() => { const shown = this.selected().map((s) => ({ model: s.id, text: seriesName(s), index: '', predictions: s.points, })); const top = Math.max(...shown.flatMap((m) => m.predictions.map((p) => p.high ?? p.p))); const figure = curveFigure(shown, top); // sources do not share an index date: the axis says only the horizon figure.layout!.xaxis!.title = { text: 'Horizonte (años)' }; return figure; }); protected toggle(id: string) { const next = new Set(this.toggled()); if (!next.delete(id)) next.add(id); this.toggled.set(next); } }