Merge parity/F1g

This commit is contained in:
2026-12-01 13:45:25 -06:00
8 changed files with 203 additions and 137 deletions
+42 -13
View File
@@ -410,6 +410,28 @@ export function ageShare(r: Reading, axis: string): Figure {
};
}
export function wholeNumberAxis(values: Iterable<number | null | undefined>) {
let min = Infinity;
let max = -Infinity;
for (const value of values) {
if (value == null || !Number.isFinite(value)) continue;
min = Math.min(min, value);
max = Math.max(max, value);
}
const target = Math.max(1, (max - min) / 4);
const magnitude = 10 ** Math.floor(Math.log10(target));
const step = [1, 2, 5, 10].find((step) => step * magnitude >= target) ?? 1;
return {
tickmode: 'linear' as const,
tick0: 0,
dtick: step * magnitude,
tickformat: '.0f',
...(Math.ceil(min) > Math.floor(max) && min <= max
? { range: [Math.floor(min), Math.ceil(max)] }
: {}),
};
}
export function historyFigure(
panel: HistoryPanel,
series: { label: string; r: Reading | undefined }[],
@@ -417,9 +439,9 @@ export function historyFigure(
xTitle: string,
): Figure {
const x = series.map((s) => s.label);
const layout = (y: string) => ({
const layout = (y: string, values: (number | null)[]) => ({
xaxis: { title: { text: xTitle }, type: 'category' as const },
yaxis: { title: { text: y } },
yaxis: { title: { text: y }, ...wholeNumberAxis(values) },
});
if (panel.kind === 'stack') {
const found = [...new Set(series.flatMap((s) => Object.keys(s.r?.strata['category'] ?? {})))];
@@ -430,24 +452,31 @@ export function historyFigure(
.filter((c) => !declared.some((d) => d.code === c))
.map((c, i) => ({ code: c, label: name(c), color: PALETTE[i % PALETTE.length] })),
];
const data = named.map((c) => ({
type: 'bar' as const,
name: c.label,
x,
y: series.map((s) => s.r?.strata['category']?.[c.code]?.['initial-population'] ?? 0),
marker: { color: c.color },
}));
const totals = x.map((_, i) => data.reduce((sum, trace) => sum + trace.y[i], 0));
return {
data: named.map((c) => ({
type: 'bar',
name: c.label,
x,
y: series.map((s) => s.r?.strata['category']?.[c.code]?.['initial-population'] ?? 0),
marker: { color: c.color },
})),
layout: { ...layout(panel.axis ?? 'Pacientes'), barmode: 'stack' },
data,
layout: { ...layout(panel.axis ?? 'Pacientes', [0, ...totals]), barmode: 'stack' },
};
}
const k = panel.kind === 'percent' ? 100 : 1;
const at = (v?: number) => (v === undefined ? null : v * k);
const band = [
...series.map((s) => at(s.r?.high)),
...[...series].reverse().map((s) => at(s.r?.low)),
];
const scores = series.map((s) => at(s.r?.score));
return {
data: [
{
x: [...x, ...[...x].reverse()],
y: [...series.map((s) => at(s.r?.high)), ...[...series].reverse().map((s) => at(s.r?.low))],
y: band,
mode: 'lines',
fill: 'toself',
fillcolor: line + '33',
@@ -457,14 +486,14 @@ export function historyFigure(
},
{
x,
y: series.map((s) => at(s.r?.score)),
y: scores,
mode: 'lines+markers',
name: panel.kind === 'percent' ? 'Porcentaje' : 'Mediana',
line: { color: line },
marker: { color: line },
},
],
layout: layout(panel.kind === 'percent' ? 'Porcentaje' : 'Días'),
layout: layout(panel.kind === 'percent' ? 'Porcentaje' : 'Días', [...band, ...scores]),
};
}