Actualiza seguimiento y ficha CaCu

This commit is contained in:
Angel Capetillo Zavala
2026-11-25 16:33:22 -06:00
parent 860c66ba5c
commit bd3f63b20a
24 changed files with 465 additions and 84 deletions
@@ -215,36 +215,35 @@ export function dona(m: Modelo, sel: Seleccion, tema: Tema): PlotlyFigure {
], layout: { autosize: true, height: 440, margin: { t: 10, l: 10, r: 10, b: 10 }, paper_bgcolor: p.background, font: { color: p.text } },
config: { displayModeBar: false } };
}
/** Selection never changes the original denominator or renormalizes a subset. */
/** Absolute counts, one independent bar per category inside each clinical scenario. The selection only chooses which
* categories are drawn; it never renormalizes anything and every scenario shares one horizontal scale. */
export function barras(m: Modelo, sel: Seleccion, tema: Tema): PlotlyFigure {
const p = c2Theme(tema);
const visibles = sel.categoria ? [sel.categoria] : sel.grupo ? hijas(m, sel.grupo).map(c => c.id) : m.categorias.map(c => c.id);
const y = m.escenarios.map(s => escapar(s.etiqueta));
const sumas = m.escenarios.map(s => s.den === null || s.den === 0 ? null : visibles.reduce((n, c) => n + porcentaje(s, c)!, 0));
// Every category keeps its trace (same uid and count) so a selection animates;
// an unselected category is a zero-width bar without text or hover, never data.
return { data: m.categorias.map(c => {
const visible = visibles.includes(c.id);
const x = m.escenarios.map(s => {
const value = porcentaje(s, c.id);
return value === null ? null : visible ? value : 0;
});
const visibles = m.categorias.filter(c => sel.categoria ? c.id === sel.categoria : sel.grupo ? c.grupo === sel.grupo : true);
// The valid total of each scenario travels with its name; an unknown total is said, never drawn as zero.
const y = m.escenarios.map(s => `${escapar(s.etiqueta)}${s.den === null ? `<br>Total ${s.estado === 'Suprimido' ? 'suprimido' : 'no disponible'}`
: s.den === 0 ? '<br>Sin pacientes con resultado' : ''}`);
const traces = visibles.map(c => {
const celdas = m.escenarios.map(s => s.celdas[c.id]);
// An unknown cell draws no bar (width 0) and says its state instead of a number; a published zero says «0».
const x = celdas.map(v => v.n ?? 0);
return { type: 'bar', orientation: 'h', uid: c.id, name: escapar(c.etiqueta), y, x,
marker: { color: c.color, line: { color: p.background, width: visible ? 1.5 : 0 } },
text: x.map(v => visible && v !== null && v >= 6 ? `${percentNumber(v)} %` : ''), textposition: 'inside', insidetextanchor: 'middle',
customdata: visible ? m.escenarios.map(s => s.den === null ? [s.estado, 'No disponible'] : [s.celdas[c.id].n, s.den]) : [],
...(visible ? { hovertemplate: `%{y}<br>${escapar(c.etiqueta)}: %{x:.1f} % (%{customdata[0]}/%{customdata[1]})<extra></extra>` }
: { hoverinfo: 'skip' }) };
}), layout: { barmode: 'stack', autosize: true, height: Math.max(440, 130 + m.escenarios.length * 65),
marker: { color: c.color, line: { color: p.background, width: 1 } },
text: celdas.map(v => v.n === null ? v.estado : miles(v.n)), textposition: 'outside', cliponaxis: false,
textfont: { size: 12, color: p.text },
hovertext: celdas.map((v, i) => `${escapar(m.escenarios[i].etiqueta)}<br>${escapar(c.etiqueta)}: ${v.n === null
? `${v.estado} (no es cero)` : `${miles(v.n)} ${v.n === 1 ? 'paciente' : 'pacientes'}`}`),
hovertemplate: '%{hovertext}<extra></extra>' };
});
const mayor = Math.max(1, ...traces.flatMap(t => t.x));
return { data: traces, layout: { barmode: 'group', bargap: .45, bargroupgap: .08, autosize: true,
height: Math.max(360, 110 + m.escenarios.length * (visibles.length * 22 + 36)),
paper_bgcolor: p.background, plot_bgcolor: p.background, font: { color: p.text },
margin: { t: 24, l: 150, r: 72, b: 64 }, showlegend: false,
margin: { t: 16, l: 150, r: 24, b: 64 }, showlegend: false,
// A thin rule between scenarios so each group reads as one unit.
shapes: m.escenarios.slice(1).map((_, i) => ({ type: 'line', xref: 'paper', x0: 0, x1: 1, yref: 'y', y0: i + .5, y1: i + .5, line: { color: p.outline, width: 1, dash: 'dot' } })),
yaxis: { type: 'category', autorange: 'reversed', categoryorder: 'array', categoryarray: y, automargin: true },
xaxis: { title: { text: '% de pacientes del escenario con resultado' }, ticksuffix: ' %', gridcolor: p.grid,
range: sel.grupo || sel.categoria ? [0, Math.max(...sumas.filter((v): v is number => v !== null), 1) * 1.35] : [0, 112],
tickvals: sel.grupo || sel.categoria ? undefined : [0, 20, 40, 60, 80, 100] },
annotations: m.escenarios.map((s, i) => ({ y: y[i], x: sumas[i] ?? 0, xanchor: 'left', xshift: 6, showarrow: false,
font: { size: 12, color: p.text }, text: s.den === null ? s.estado : s.den === 0 ? 'Sin pacientes con resultado'
: sel.grupo || sel.categoria ? `<b>${percentNumber(sumas[i]!)} %</b> (${visibles.reduce((n, c) => n + s.celdas[c].n!, 0)}/${miles(s.den)})`
: miles(s.den) })),
xaxis: { title: { text: 'Número de pacientes' }, tickformat: ',d', gridcolor: p.grid, zeroline: false,
range: [0, mayor * 1.2], ...(mayor <= 8 ? { dtick: 1 } : {}) },
}, config: { displayModeBar: false } };
}