F1b: user fixes

Use the existing Plotly card and registered Sankey trace. Build transition totals from the remaining stages and omit zero-flow links. Build the prevalence donut from the report counts, with a grey slice for missing sex and controls to highlight each recorded sex. Use grouped bars for M23 and separate age and sex stacks for M24. Use the existing stacked history style for M31 and put the M30 figure behind one page flag. Cover the builders and the existing TEA page case.
This commit is contained in:
2026-12-01 11:33:06 -06:00
parent 7c98566579
commit c4920ed7eb
7 changed files with 451 additions and 57 deletions
+227 -20
View File
@@ -349,29 +349,37 @@ function ages(r: Reading): [string, Record<string, number>][] {
);
}
export function ageStack(r: Reading, names: [string, string]): Figure {
const rows = ages(r);
const x = rows.map(([a]) => ageLabel(a));
export function ageStack(
r: Reading,
names: [string, string],
stratum: 'age_group' | 'sex' = 'age_group',
): Figure {
const rows = stratum === 'sex' ? Object.entries(r.strata['sex'] ?? {}) : ages(r);
const x = rows.map(([value]) => (stratum === 'sex' ? name(value) : ageLabel(value)));
const numerators = rows.map(([, c]) => c['numerator'] ?? 0);
const denominators = rows.map(([, c]) => c['denominator'] ?? 0);
const colors = rows.map(
([code]) => TEA_SEXES.find((sex) => sex.code === code)?.color ?? '#C9CDD2',
);
return {
data: [
{
type: 'bar',
name: names[0],
x,
y: rows.map(([, c]) => c['numerator'] ?? 0),
marker: { color: AGE_COLORS[0] },
},
{
type: 'bar',
name: names[1],
x,
y: rows.map(([, c]) => (c['denominator'] ?? 0) - (c['numerator'] ?? 0)),
marker: { color: AGE_COLORS[1] },
},
] as Partial<Plotly.Data>[],
data: [numerators, denominators.map((n, i) => n - numerators[i])].map((y, i) => ({
type: 'bar',
name: names[i],
x,
y,
marker:
stratum === 'sex'
? { color: colors, pattern: { shape: i ? '/' : '' } }
: { color: AGE_COLORS[i] },
text: y.map((n, j) =>
denominators[j] ? `${n} (${percent((100 * n) / denominators[j])} %)` : '',
),
textposition: 'inside',
hovertemplate: '%{x}<br>%{fullData.name}: %{y} pacientes<extra></extra>',
})) as Partial<Plotly.Data>[],
layout: {
barmode: 'stack',
xaxis: { title: { text: 'Grupo de edad' }, type: 'category' },
xaxis: { title: { text: stratum === 'sex' ? 'Sexo' : 'Grupo de edad' }, type: 'category' },
yaxis: { title: { text: 'Pacientes' }, rangemode: 'tozero' },
},
};
@@ -458,3 +466,202 @@ export function historyFigure(
layout: layout(panel.kind === 'percent' ? 'Porcentaje' : 'Días'),
};
}
const TEA_SEXES = [
{ code: 'female', color: ROSE },
{ code: 'male', color: '#4269C2' },
{ code: 'no_data', color: '#C9CDD2' },
];
export function sexCountBars(r: Reading, catalog: Catalog, axis: string, period: string): Figure {
const base = countBars(r, catalog, axis, period);
const cells = r.strata['sex-category'] ?? {};
return {
data: TEA_SEXES.map(({ code }, i) => ({
...base.data[0],
name: name(code),
marker: {
color: catalog.categories.map((c) => c.color),
pattern: { shape: ['', '/', '\\'][i] },
},
x: catalog.categories.map((c) => cells[`${code}|${c.code}`]?.['initial-population'] ?? 0),
})) as Partial<Plotly.Data>[],
layout: { ...base.layout, barmode: 'group', showlegend: true },
};
}
export function teaSankey(r: Reading): Figure {
const [mchat, psychology, neuro, eeg] = [
'mchat_only',
'psychology',
'neuropediatrics',
'eeg',
].map((code) => counted(r, code));
const labels = [
'M-CHAT',
'Evaluación psicológica',
'Evaluación neuropediátrica',
'EEG de buena calidad',
'Se quedó en M-CHAT',
'Se quedó en evaluación psicológica',
'Se quedó en evaluación neuropediátrica',
];
const colors = ['#B8433A', '#D0B778', '#7184A2', OLIVE, '#888888', '#888888', '#888888'];
const links = [
{ source: 0, target: 1, value: psychology + neuro + eeg },
{ source: 0, target: 4, value: mchat },
{ source: 1, target: 2, value: neuro + eeg },
{ source: 1, target: 5, value: psychology },
{ source: 2, target: 3, value: eeg },
{ source: 2, target: 6, value: neuro },
].filter(({ value }) => value > 0);
const hovertemplate = '%{label}<br>%{value} pacientes<extra></extra>';
return {
data: [
{
type: 'sankey',
arrangement: 'fixed',
node: { label: labels, color: colors, align: 'left', pad: 24, hovertemplate },
link: {
source: links.map(({ source }) => source),
target: links.map(({ target }) => target),
value: links.map(({ value }) => value),
color: links.map(({ target }) => rgba(colors[target], 0.45)),
label: links.map(({ target }) => labels[target]),
hovertemplate,
},
} as Partial<Plotly.Data>,
],
layout: { margin: { l: 20, r: 20, t: 24, b: 24 } },
};
}
export function teaSexDonut(r: Reading, selectedSex?: string): Figure {
const confirmed = counted(r, 'confirmed');
const total = r.populations['initial-population'] ?? 0;
const share = (part: number, whole: number) => percent(whole ? (100 * part) / whole : 0);
const slices = TEA_SEXES.slice(0, 2).flatMap(({ code, color }) => {
const sexTotal = r.strata['sex']?.[code]?.['initial-population'] ?? 0;
const cases = r.strata['sex-category']?.[`${code}|confirmed`]?.['initial-population'] ?? 0;
return [
{
label: `${name(code)} · Diagnóstico confirmado`,
value: cases,
total: sexTotal,
color,
line: color,
code,
confirmed: true,
},
{
label: `${name(code)} · Sin diagnóstico confirmado`,
value: sexTotal - cases,
total: sexTotal,
color: rgba(color, 0.25),
line: color,
code,
confirmed: false,
},
];
});
const unknown = total - slices.reduce((n, { value }) => n + value, 0);
if (unknown) {
const { color } = TEA_SEXES[2];
slices.push({
label: 'Sin sexo registrado',
value: unknown,
total: unknown,
color,
line: color,
code: 'no_data',
confirmed: false,
});
}
const selected = slices.find((s) => s.code === selectedSex);
const centerCases = selected?.value ?? confirmed;
const centerTotal = selected?.total ?? total;
return {
data: [
{
type: 'pie',
hole: 0.58,
sort: false,
direction: 'clockwise',
labels: slices.map((s) => s.label),
values: slices.map((s) => s.value),
marker: {
colors: slices.map((s) => (!selected || s.code === selected.code ? s.color : '#E1E3E5')),
line: {
color: slices.map((s) => (!selected || s.code === selected.code ? s.line : '#C9CDD2')),
width: 1.5,
},
},
text: slices.map((s) =>
!total || s.value / total < 0.04
? ''
: s.confirmed
? `${s.value}<br>${share(s.value, s.total)} %`
: String(s.value),
),
textinfo: 'text',
textposition: 'inside',
insidetextorientation: 'horizontal',
customdata: slices.map((s) => [share(s.value, s.total), s.total]),
hovertemplate:
'%{label}<br>%{value} niños, %{customdata[0]} % de %{customdata[1]}<extra></extra>',
} as Partial<Plotly.Data>,
],
layout: {
showlegend: true,
legend: {
orientation: 'h',
y: -0.12,
yanchor: 'top',
itemclick: false,
itemdoubleclick: false,
},
margin: { l: 24, r: 24, t: 24, b: 96 },
annotations: [
{
x: 0.5,
y: 0.5,
xref: 'paper',
yref: 'paper',
showarrow: false,
text: `<b>${share(centerCases, centerTotal)} %</b><br>${centerCases} de ${centerTotal} ${selected ? `(${name(selected.code).toLowerCase()})` : 'niños'}`,
font: { size: 18 },
},
],
},
};
}
export function teaAlterations(r: Reading): Figure {
const rows = [['', r.populations] as const, ...Object.entries(r.strata['alteration'] ?? {})];
const customdata = rows.map(([, c]) => [c['numerator'], c['denominator']]);
const rates = customdata.map(([n, total]) => (total ? (100 * n) / total : 0));
return {
data: [
{
type: 'bar',
x: rows.map(([code]) => (code ? name(code) : 'Alguna alteración')),
y: rates,
marker: { color: rows.map((_, i) => PALETTE[(i + 1) % PALETTE.length]) },
text: rates.map((rate) => `${percent(rate)} %`),
textposition: 'outside',
cliponaxis: false,
customdata,
hovertemplate:
'%{x}<br>%{text} (%{customdata[0]} de %{customdata[1]} niños)<extra></extra>',
} as Partial<Plotly.Data>,
],
layout: {
xaxis: { type: 'category', title: { text: 'Alteración del EEG' } },
yaxis: {
range: [0, 100],
ticksuffix: ' %',
title: { text: '% de niños con EEG de buena calidad' },
},
},
};
}