F1f: TEA indicator chart formats (sex by age group, men blue, women magenta)

Reuse the existing male blue and female magenta, in that order. Read the published sex-by-result and age-by-sex cells and omit unknown sex. Keep the report age labels and sort by the starting month. Use zero for absent count cells and omit rates without denominators.

Keep M24 count heights and percentage labels. Stack the two outcomes within each sex using Plotly offset groups. Use the previous prevalence alpha of 0.25 for both transparent outcomes. For M31, use the published outcome counts and keep the sex focus control by outlining the selected bars while preserving both fill colors. Use the standard chart card height and identify changed figures by measure in the page checks.

Prettier, targeted figure type checking, and the 23 indicator logic tests passed. Full application type checking requires the unavailable pdfmake dependencies in this checkout. Angular and Playwright runs are reserved for the test machines.
This commit is contained in:
2026-12-01 13:36:44 -06:00
parent 5bf022b36f
commit 789145b93c
6 changed files with 174 additions and 206 deletions
+79 -147
View File
@@ -340,69 +340,70 @@ export function countBars(r: Reading, catalog: Catalog, axis: string, period: st
};
}
const AGE_COLORS = ['#A8C243', '#42C29C', '#9C42C2', '#4269C2'];
const TEA_SEXES = [
{ code: 'male', color: '#4269C2' },
{ code: 'female', color: ROSE },
];
const ageLabel = (band: string) => band.replace(/^(\d+)-(\d+)$/, '$1 a $2 meses');
function ages(r: Reading): [string, Record<string, number>][] {
return Object.entries(r.strata['age_group'] ?? {}).sort(
([a], [b]) => parseInt(a, 10) - parseInt(b, 10),
);
function ageBands(cells: Reading['strata'][string]): string[] {
const bands = Object.keys(cells)
.map((key) => key.split('|'))
.filter(([, code]) => TEA_SEXES.some((sex) => sex.code === code))
.map(([band]) => band);
return [...new Set(bands)].sort((a, b) => parseInt(a, 10) - parseInt(b, 10));
}
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',
);
export function ageStack(r: Reading, names: [string, string]): Figure {
const cells = r.strata['age_sex'] ?? {};
const bands = ageBands(cells);
const x = bands.map(ageLabel);
return {
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>[],
data: TEA_SEXES.flatMap(({ code, color }) => {
const rows = bands.map((band) => cells[`${band}|${code}`]);
const confirmed = rows.map((row) => row?.['numerator'] ?? 0);
const totals = rows.map((row) => row?.['denominator'] ?? 0);
return [confirmed, totals.map((n, i) => n - confirmed[i])].map((y, i) => ({
type: 'bar',
name: `${name(code)} · ${names[i]}`,
offsetgroup: code,
x,
y,
marker: { color: i ? rgba(color, 0.25) : color },
text: y.map((n, j) => (n ? `${n} (${percent((100 * n) / totals[j])} %)` : '')),
textposition: 'inside',
hovertemplate: '%{x}<br>%{fullData.name}: %{y} pacientes<extra></extra>',
})) as Partial<Plotly.Data>[];
}),
layout: {
barmode: 'stack',
xaxis: { title: { text: stratum === 'sex' ? 'Sexo' : 'Grupo de edad' }, type: 'category' },
xaxis: { title: { text: 'Grupo de edad' }, type: 'category' },
yaxis: { title: { text: 'Pacientes' }, rangemode: 'tozero' },
},
};
}
export function ageShare(r: Reading, axis: string): Figure {
const rows = ages(r);
const cells = r.strata['age_sex'] ?? {};
const bands = ageBands(cells);
const x = bands.map(ageLabel);
return {
data: [
{
type: 'bar',
x: rows.map(([a]) => ageLabel(a)),
y: rows.map(([, c]) =>
c['denominator'] ? (100 * (c['numerator'] ?? 0)) / c['denominator'] : 0,
),
marker: { color: rows.map((_, i) => AGE_COLORS[i % AGE_COLORS.length]) },
texttemplate: '%{y:.1f} %',
textposition: 'outside',
hovertemplate: '%{x}: %{y:.1f} %<extra></extra>',
} as Partial<Plotly.Data>,
],
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:.1f} %',
textposition: 'outside',
hovertemplate: '%{x}: %{y:.1f} %<extra>%{fullData.name}</extra>',
})),
layout: {
showlegend: false,
barmode: 'group',
xaxis: { title: { text: 'Grupo de edad' }, type: 'category' },
yaxis: { title: { text: axis }, range: [0, 100], ticksuffix: ' %' },
},
@@ -467,24 +468,16 @@ export function historyFigure(
};
}
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) => ({
data: TEA_SEXES.map(({ code, color }) => ({
...base.data[0],
name: name(code),
marker: {
color: catalog.categories.map((c) => c.color),
pattern: { shape: ['', '/', '\\'][i] },
},
marker: { color },
x: catalog.categories.map((c) => cells[`${code}|${c.code}`]?.['initial-population'] ?? 0),
hovertemplate: '%{y}: %{x}<extra>%{fullData.name}</extra>',
})) as Partial<Plotly.Data>[],
layout: { ...base.layout, barmode: 'group', showlegend: true },
};
@@ -539,102 +532,41 @@ export function teaSankey(r: Reading): Figure {
};
}
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;
export function teaSexBars(r: Reading, selectedSex?: string): Figure {
const cells = r.strata['sex-category'] ?? {};
const x = TEA_SEXES.map(({ code }) => name(code));
return {
data: [
{
type: 'pie',
hole: 0.58,
sort: false,
direction: 'clockwise',
labels: slices.map((s) => s.label),
values: slices.map((s) => s.value),
{ 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,
x,
y,
marker: {
colors: slices.map((s) => (!selected || s.code === selected.code ? s.color : '#E1E3E5')),
color: TEA_SEXES.map(({ color }) => (i ? rgba(color, 0.25) : color)),
line: {
color: slices.map((s) => (!selected || s.code === selected.code ? s.line : '#C9CDD2')),
width: 1.5,
color: TEA_SEXES.map(({ color }) => color),
width: TEA_SEXES.map(({ code }) => (code === selectedSex ? 2 : 0)),
},
},
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]),
customdata: TEA_SEXES.map(({ code }, j) => {
const total = r.strata['sex']?.[code]?.['initial-population'] ?? 0;
return [percent(total ? (100 * y[j]) / total : 0), total];
}),
hovertemplate:
'%{label}<br>%{value} niños, %{customdata[0]} % de %{customdata[1]}<extra></extra>',
} as Partial<Plotly.Data>,
],
'%{x} · %{fullData.name}<br>%{y} niños, %{customdata[0]} % de %{customdata[1]}<extra></extra>',
};
}),
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 },
},
],
barmode: 'group',
xaxis: { title: { text: 'Sexo' }, type: 'category' },
yaxis: { title: { text: 'Niños' }, rangemode: 'tozero' },
},
};
}