Merge parity/F2a

This commit is contained in:
2026-12-03 13:55:41 -06:00
7 changed files with 292 additions and 76 deletions
+103 -38
View File
@@ -272,6 +272,17 @@ export function fill(template: string, r: Reading): string {
export function boxFigure(r: Reading, period: string): Figure {
const q1 = r.low ?? r.score!;
const q3 = r.high ?? r.score!;
const label = (text: string, y: number, yanchor: 'top' | 'middle' | 'bottom') => ({
x: period,
y,
text: `${text} ${decimal(y)}`,
xref: 'x' as const,
yref: 'y' as const,
xanchor: 'left' as const,
yanchor,
xshift: 56,
showarrow: false,
});
return {
data: [
{
@@ -285,11 +296,16 @@ export function boxFigure(r: Reading, period: string): Figure {
upperfence: [q3],
marker: { color: '#4269C2' },
fillcolor: 'rgba(66,105,194,0.55)',
hovertemplate: `Q1 ${decimal(q1)} · mediana ${decimal(r.score!)} · Q3 ${decimal(q3)} días<extra></extra>`,
hoverinfo: 'skip',
} as Partial<Plotly.Data>,
],
layout: {
showlegend: false,
annotations: [
label('Q3', q3, 'bottom'),
label('Mediana', r.score!, 'middle'),
label('Q1', q1, 'top'),
],
yaxis: { title: { text: 'días' }, rangemode: 'tozero', ...valueAxis([q1, r.score!, q3]) },
},
};
@@ -418,19 +434,26 @@ export function ageShare(r: Reading, axis: string): Figure {
const bands = ageBands(cells);
const x = bands.map(ageLabel);
return {
data: TEA_SEXES.map(({ code, color }) => ({
type: 'bar',
name: name(code),
x,
y: bands.map((band) => {
const row = cells[`${band}|${code}`];
return row?.['denominator'] ? (100 * (row['numerator'] ?? 0)) / row['denominator'] : null;
}),
marker: { color },
texttemplate: '%{y:.1~f} %',
textposition: 'outside',
hovertemplate: '%{x}: %{y:.1~f} %<extra>%{fullData.name}</extra>',
})),
data: TEA_SEXES.map(({ code, color }) => {
const rows = bands.map((band) => cells[`${band}|${code}`]);
const shares = rows.map((row) =>
row?.['denominator'] ? (100 * (row['numerator'] ?? 0)) / row['denominator'] : null,
);
return {
type: 'bar',
name: name(code),
x,
y: shares,
marker: { color },
text: shares.map((share, i) =>
share === null
? ''
: `${percent(share)} % (${rows[i]['numerator'] ?? 0}/${rows[i]['denominator']})`,
),
textposition: 'outside',
hovertemplate: '%{x}: %{text}<extra>%{fullData.name}</extra>',
};
}),
layout: {
barmode: 'group',
xaxis: { title: { text: 'Grupo de edad' }, type: 'category' },
@@ -507,6 +530,31 @@ export function historyFigure(
layout: { ...layout(panel.axis ?? 'Pacientes', [0, ...totals]), barmode: 'stack' },
};
}
if (panel.kind === 'rate') {
const lines = [
{ name: 'Total', color: line, at: (r?: Reading) => r?.score },
...TEA_SEXES.map(({ code, color }) => ({
name: name(code),
color,
at: (r?: Reading) => r?.rates?.['sex']?.[code],
})),
];
const data = lines.map(({ name: label, color, at }) => ({
x,
y: series.map((s) => at(s.r) ?? null),
mode: 'lines+markers' as const,
name: label,
line: { color },
marker: { color },
}));
return {
data,
layout: layout(
panel.axis ?? '',
data.flatMap((d) => d.y),
),
};
}
const k = panel.kind === 'percent' ? 100 : 1;
const at = (v?: number) => (v === undefined ? null : v * k);
const band = [
@@ -594,7 +642,7 @@ export function teaSankey(r: Reading): Figure {
type: 'sankey',
arrangement: 'fixed',
node: {
label: shown.map((index) => nodes[index].label),
label: shown.map((index) => `${nodes[index].label} · ${count(nodes[index].value)}`),
color: shown.map((index) => nodes[index].color),
pad: 36,
hovertemplate,
@@ -613,46 +661,63 @@ export function teaSankey(r: Reading): Figure {
};
}
const RATE_UNIT = 'por 10 000 niños';
const perTenThousand = (n: number) => n.toLocaleString('es-MX', { maximumFractionDigits: 1 });
function prevalenceCounts(r: Reading, sex?: string): { confirmed: number; total: number } {
if (!sex) {
return { confirmed: counted(r, 'confirmed'), total: r.populations['initial-population'] ?? 0 };
}
return {
confirmed: r.strata['sex-category']?.[`${sex}|confirmed`]?.['initial-population'] ?? 0,
total: r.strata['sex']?.[sex]?.['initial-population'] ?? 0,
};
}
export function prevalenceHeadline(r: Reading, sex?: string) {
const found = sex ? r.rates?.['sex']?.[sex] : r.score;
if (found === undefined) return undefined;
const { confirmed, total } = prevalenceCounts(r, sex);
return {
value: perTenThousand(found),
detail: `${RATE_UNIT} · ${count(confirmed)} confirmados de ${count(total)} niños`,
};
}
export function teaSexBars(r: Reading, selectedSex?: string): Figure {
const cells = r.strata['sex-category'] ?? {};
const x = TEA_SEXES.map(({ code }) => name(code));
const y = TEA_SEXES.map(({ code }) => r.rates?.['sex']?.[code] ?? 0);
return {
data: [
{ code: 'confirmed', label: 'Diagnóstico confirmado' },
{ code: 'in_population', label: 'Sin diagnóstico confirmado' },
].map(({ code: category, label }, i) => {
const y = TEA_SEXES.map(
({ code }) => cells[`${code}|${category}`]?.['initial-population'] ?? 0,
);
return {
{
type: 'bar',
name: label,
name: 'Prevalencia registrada',
x,
y,
marker: {
color: TEA_SEXES.map(({ color }) => (i ? rgba(color, 0.25) : color)),
color: TEA_SEXES.map(({ color }) => color),
line: {
color: TEA_SEXES.map(({ color }) => color),
color: '#000000',
width: TEA_SEXES.map(({ code }) => (code === selectedSex ? 2 : 0)),
},
},
customdata: TEA_SEXES.map(({ code }, j) => {
const total = r.strata['sex']?.[code]?.['initial-population'] ?? 0;
return [percent(total ? (100 * y[j]) / total : 0), total];
text: y.map((value) => perTenThousand(value)),
textposition: 'outside',
cliponaxis: false,
customdata: TEA_SEXES.map(({ code }) => {
const { confirmed, total } = prevalenceCounts(r, code);
return [confirmed, total];
}),
hovertemplate:
'%{x} · %{fullData.name}<br>%{y:d} niños, %{customdata[0]} % de %{customdata[1]}<extra></extra>',
};
}),
hovertemplate: `%{x}<br>%{text} ${RATE_UNIT}<br>%{customdata[0]} confirmados de %{customdata[1]} niños<extra></extra>`,
},
],
layout: {
barmode: 'group',
showlegend: false,
xaxis: { title: { text: 'Sexo' }, type: 'category' },
yaxis: {
title: { text: 'Niños' },
title: { text: 'Casos por 10 000 niños' },
rangemode: 'tozero',
...valueAxis(
TEA_SEXES.map(({ code }) => r.strata['sex']?.[code]?.['initial-population'] ?? 0),
),
...valueAxis(y),
},
},
};