Usar un solo selector de curvas para riesgo y supervivencia

This commit is contained in:
2026-12-03 07:31:12 -06:00
parent 5a82dc780e
commit 4ec071332d
7 changed files with 138 additions and 143 deletions
+37 -59
View File
@@ -1,7 +1,6 @@
import { Component, computed, input, signal } from '@angular/core';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { Component, computed, input } from '@angular/core';
import type { Parameters, ParametersParameter } from 'fhir/r4';
import { Plot } from '../plot';
import { CurveChooser } from './curve-chooser';
import { curveFigure } from './risk-curve';
export interface SeriesPoint {
@@ -64,23 +63,12 @@ export function parseSeries(answer: Parameters): Series[] {
/** The chosen series as curves. Series are shown side by side; no difference is computed. */
@Component({
selector: 'app-risk-series',
imports: [MatCheckboxModule, Plot],
imports: [CurveChooser],
template: `
<p class="muted">Cada serie tiene su propio desenlace y población: no se restan entre sí.</p>
<div class="choices">
@for (s of available(); track s.id) {
<mat-checkbox [checked]="chosen().has(s.id)" (change)="toggle(s.id)">
{{ name(s) }}
</mat-checkbox>
}
</div>
@if (selected().length) {
@for (assumption of assumptions(); track assumption) {
<p class="muted">{{ assumption }}</p>
}
<app-plot [figure]="figure()" [height]="340" [toolbar]="false" />
}
@if (!available().length) {
@if (available().length) {
<app-curve-chooser [choices]="choices()" [draw]="draw()" [notes]="assumptions()" />
} @else {
<p class="muted">No disponible: datos insuficientes.</p>
}
`,
@@ -88,52 +76,42 @@ export function parseSeries(answer: Parameters): Series[] {
:host {
display: block;
}
.choices {
display: flex;
flex-wrap: wrap;
gap: 0 16px;
margin-bottom: 8px;
}
`,
})
export class RiskSeries {
readonly series = input.required<Series[]>();
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<string>());
protected readonly chosen = computed(
() =>
new Set(
this.available()
.filter((s) => (s.source === 'Institucional') !== this.toggled().has(s.id))
.map((s) => s.id),
),
/** the platform's own series are drawn until the user says otherwise */
protected readonly choices = computed(() =>
this.available().map((s) => ({
id: s.id,
name: seriesName(s),
on: s.source === 'Institucional',
})),
);
protected readonly selected = computed(() =>
this.available().filter((s) => this.chosen().has(s.id)),
);
/** what the chosen series assume, each said once */
protected readonly assumptions = computed(() => [
...new Set(this.selected().flatMap((s) => s.assumptions)),
]);
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;
private readonly among = computed(() => {
const all = this.available();
return (ids: Set<string>) => all.filter((s) => ids.has(s.id));
});
/** what the chosen series assume, each said once */
protected readonly assumptions = computed(() => {
const chosen = this.among();
return (ids: Set<string>) => [...new Set(chosen(ids).flatMap((s) => s.assumptions))];
});
protected readonly draw = computed(() => {
const chosen = this.among();
return (ids: Set<string>) => {
const shown = chosen(ids).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);
}
}