diff --git a/src/app/paciente/paciente.html b/src/app/paciente/paciente.html index 8a247a7..5f7abed 100644 --- a/src/app/paciente/paciente.html +++ b/src/app/paciente/paciente.html @@ -616,7 +616,7 @@ Greenwood; n = {{ c.n }}, fallecidos = {{ c.fallecidos }}).

- + } @else {

{{ c.missing }}

} diff --git a/src/app/paciente/paciente.ts b/src/app/paciente/paciente.ts index 8049064..d9cb261 100644 --- a/src/app/paciente/paciente.ts +++ b/src/app/paciente/paciente.ts @@ -50,6 +50,7 @@ import type { Relation } from '../api/models/relation'; import { Figure, PALETTE, Plot } from '../plot'; import { Loading } from '../loading'; import { Factores, ScenarioFactors } from './factores'; +import { SurvivalCurves } from './survival-curves'; /** An Observation's value as the screens say it, whatever its type. */ function shown(o: Observation): string { @@ -103,6 +104,7 @@ function modelInputs(value: string | undefined): Record | undef selector: 'app-paciente', imports: [ Factores, + SurvivalCurves, Loading, DecimalPipe, MatButtonModule, diff --git a/src/app/paciente/survival-curves.spec.ts b/src/app/paciente/survival-curves.spec.ts new file mode 100644 index 0000000..eb33f0f --- /dev/null +++ b/src/app/paciente/survival-curves.spec.ts @@ -0,0 +1,12 @@ +import { curveGroups } from './survival-curves'; + +describe('curveGroups', () => { + it('keeps each band with its line, one choice per curve', () => { + const data = [{ showlegend: false }, { name: 'Kaplan-Meier' }, { name: 'Weibull' }]; + const groups = curveGroups({ data: data as never }); + expect(groups.map((g) => [g.name, g.traces.length])).toEqual([ + ['Kaplan-Meier', 2], + ['Weibull', 1], + ]); + }); +}); diff --git a/src/app/paciente/survival-curves.ts b/src/app/paciente/survival-curves.ts new file mode 100644 index 0000000..5577e44 --- /dev/null +++ b/src/app/paciente/survival-curves.ts @@ -0,0 +1,67 @@ +import { Component, computed, input, signal } from '@angular/core'; +import { MatCheckboxModule } from '@angular/material/checkbox'; +import { Figure, Plot } from '../plot'; + +type Trace = Figure['data'][number]; + +/** The figure's curves by name; a band has no name of its own and goes with the line after it. */ +export function curveGroups(figure: Figure): { name: string; traces: Trace[] }[] { + const groups: { name: string; traces: Trace[] }[] = []; + let pending: Trace[] = []; + for (const trace of figure.data) { + pending.push(trace); + const { name, showlegend } = trace as { name?: string; showlegend?: boolean }; + if (name && showlegend !== false) { + groups.push({ name, traces: pending }); + pending = []; + } + } + return groups; +} + +/** The survival fits, each one chosen or not, to compare them on one plot. */ +@Component({ + selector: 'app-survival-curves', + imports: [MatCheckboxModule, Plot], + template: ` +
+ @for (g of groups(); track g.name) { + + {{ g.name }} + + } +
+ @if (shown().data.length) { + + } + `, + styles: ` + :host { + display: block; + } + .choices { + display: flex; + flex-wrap: wrap; + gap: 0 16px; + margin-bottom: 8px; + } + `, +}) +export class SurvivalCurves { + readonly figure = input.required
(); + protected readonly groups = computed(() => curveGroups(this.figure())); + private readonly hidden_ = signal(new Set()); + protected readonly hidden = this.hidden_.asReadonly(); + protected readonly shown = computed
(() => ({ + ...this.figure(), + data: this.groups() + .filter((g) => !this.hidden().has(g.name)) + .flatMap((g) => g.traces), + })); + + protected toggle(name: string) { + const next = new Set(this.hidden()); + if (!next.delete(name)) next.add(name); + this.hidden_.set(next); + } +}