diff --git a/src/app/indicadores/figures.ts b/src/app/indicadores/figures.ts index 0ff5e8a..fc2edd8 100644 --- a/src/app/indicadores/figures.ts +++ b/src/app/indicadores/figures.ts @@ -11,6 +11,8 @@ export const percentAxis = { ticksuffix: ' %', tickformat: '.0f', hoverformat: SHORT, + range: [0, 110], + dtick: 20, }; const count = (n: number) => n.toLocaleString('es-MX'); @@ -231,7 +233,7 @@ export function rateDots( categoryarray: [...y].reverse(), automargin: true, }, - xaxis: { range: [0, 100], ...percentAxis, zeroline: false, title: { text: spec.axis } }, + xaxis: { ...percentAxis, range: [0, 100], zeroline: false, title: { text: spec.axis } }, annotations: rows.map((x) => ({ x: 1, xref: 'paper', @@ -272,17 +274,6 @@ 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: [ { @@ -296,16 +287,11 @@ export function boxFigure(r: Reading, period: string): Figure { upperfence: [q3], marker: { color: '#4269C2' }, fillcolor: 'rgba(66,105,194,0.55)', - hoverinfo: 'skip', + hovertemplate: `Q3: %{q3:${SHORT}}
Mediana: %{median:${SHORT}}
Q1: %{q1:${SHORT}}`, } as Partial, ], 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]) }, }, }; @@ -399,32 +385,28 @@ export function ageStack(r: Reading, names: [string, string]): Figure { return { 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}
%{fullData.name}: %{y:d} pacientes', - })) as Partial[]; + const confirmed = rows.map((row, j) => + totals[j] ? (100 * (row?.['numerator'] ?? 0)) / totals[j] : 0, + ); + return [confirmed, totals.map((total, j) => (total ? 100 - confirmed[j] : 0))].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((share) => (share ? `${percent(share)} %` : '')), + textposition: 'inside', + hovertemplate: `%{x}
%{fullData.name}: %{y:${SHORT}} %`, + }), + ) as Partial[]; }), layout: { barmode: 'stack', xaxis: { title: { text: 'Grupo de edad' }, type: 'category' }, - yaxis: { - title: { text: 'Pacientes' }, - rangemode: 'tozero', - ...valueAxis( - bands.flatMap((band) => - TEA_SEXES.map(({ code }) => cells[`${band}|${code}`]?.['denominator'] ?? 0), - ), - ), - }, + yaxis: { title: { text: '% de pacientes' }, ...percentAxis }, }, }; } @@ -445,11 +427,7 @@ export function ageShare(r: Reading, axis: string): Figure { x, y: shares, marker: { color }, - text: shares.map((share, i) => - share === null - ? '' - : `${percent(share)} % (${rows[i]['numerator'] ?? 0}/${rows[i]['denominator']})`, - ), + text: shares.map((share) => (share === null ? '' : `${percent(share)} %`)), textposition: 'outside', hovertemplate: '%{x}: %{text}%{fullData.name}', }; @@ -457,7 +435,7 @@ export function ageShare(r: Reading, axis: string): Figure { layout: { barmode: 'group', xaxis: { title: { text: 'Grupo de edad' }, type: 'category' }, - yaxis: { title: { text: axis }, range: [0, 100], ...percentAxis }, + yaxis: { title: { text: axis }, ...percentAxis }, }, }; } @@ -674,17 +652,16 @@ function prevalenceCounts(r: Reading, sex?: string): { confirmed: number; total: }; } -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); +export function prevalenceHeadline(r: Reading) { + if (r.score === undefined) return undefined; + const { confirmed, total } = prevalenceCounts(r); return { - value: perTenThousand(found), + value: perTenThousand(r.score), detail: `${RATE_UNIT} · ${count(confirmed)} confirmados de ${count(total)} niños`, }; } -export function teaSexBars(r: Reading, selectedSex?: string): Figure { +export function teaSexBars(r: Reading): Figure { const x = TEA_SEXES.map(({ code }) => name(code)); const y = TEA_SEXES.map(({ code }) => r.rates?.['sex']?.[code] ?? 0); return { @@ -694,16 +671,9 @@ export function teaSexBars(r: Reading, selectedSex?: string): Figure { name: 'Prevalencia registrada', x, y, - marker: { - color: TEA_SEXES.map(({ color }) => color), - line: { - color: '#000000', - width: TEA_SEXES.map(({ code }) => (code === selectedSex ? 2 : 0)), - }, - }, + marker: { color: TEA_SEXES.map(({ color }) => color) }, text: y.map((value) => perTenThousand(value)), textposition: 'outside', - cliponaxis: false, customdata: TEA_SEXES.map(({ code }) => { const { confirmed, total } = prevalenceCounts(r, code); return [confirmed, total]; @@ -736,7 +706,6 @@ export function teaAlterations(r: Reading): Figure { marker: { color: rows.map((_, i) => PALETTE[(i + 1) % PALETTE.length]) }, text: rates.map((rate) => `${percent(rate)} %`), textposition: 'outside', - cliponaxis: false, customdata, hovertemplate: '%{x}
%{text} (%{customdata[0]} de %{customdata[1]} niños)', diff --git a/src/app/indicadores/tab-indicadores.html b/src/app/indicadores/tab-indicadores.html index 2e7c19e..6be39f0 100644 --- a/src/app/indicadores/tab-indicadores.html +++ b/src/app/indicadores/tab-indicadores.html @@ -90,7 +90,7 @@ @if (reports.isLoading()) { } @else if (routeOf(section); as fig) { - + } @else {

Sin tiempos publicados en el periodo.

} @@ -137,15 +137,6 @@

{{ f.title }}

Filtro general aplicado: {{ period }}

@if (f.kind === 'sex-bars') { - - Ambos sexos - Femenino - Masculino - @if (prevalenceOf(f); as p) {
{{ p.value }} @@ -156,7 +147,7 @@ @if (reports.isLoading()) { } @else if (figureOf(f); as fig) { - + } @else {
Sin datos publicados
} @@ -228,7 +219,7 @@ @if (reports.isLoading()) { } @else if (figureOf(f); as fig) { - + } @else {

Sin datos en el periodo.

} diff --git a/src/app/indicadores/tab-indicadores.ts b/src/app/indicadores/tab-indicadores.ts index f7dec71..c847c78 100644 --- a/src/app/indicadores/tab-indicadores.ts +++ b/src/app/indicadores/tab-indicadores.ts @@ -50,7 +50,6 @@ export class TabIndicadores { protected readonly type = signal('semester'); protected readonly options = computed(() => periods(this.type())); protected readonly chosen = signal(undefined); - protected readonly prevalenceSex = signal(undefined); protected readonly applied = signal(undefined); protected readonly page = computed(() => PAGES[this.domain()]); @@ -106,6 +105,7 @@ export class TabIndicadores { }); } + protected readonly clinicalHeight = 430; protected readonly pending = 'Indicador no publicado en el catálogo institucional.'; protected setType(type: PeriodType): void { this.type.set(type); @@ -140,7 +140,7 @@ export class TabIndicadores { } protected prevalenceOf(spec: FigureSpec) { const r = this.rd(spec.measure); - return r ? prevalenceHeadline(r, this.prevalenceSex()) : undefined; + return r ? prevalenceHeadline(r) : undefined; } protected readonly teaSummary = computed(() => { const rd = (c: string) => this.loaded()?.get(c); @@ -198,7 +198,7 @@ export class TabIndicadores { case 'sankey': return teaSankey(r); case 'sex-bars': - return teaSexBars(r, this.prevalenceSex()); + return teaSexBars(r); case 'alterations': return teaAlterations(r); case 'age-stack': diff --git a/src/app/indicadores/tea-indicators.spec.ts b/src/app/indicadores/tea-indicators.spec.ts index be53565..3a07991 100644 --- a/src/app/indicadores/tea-indicators.spec.ts +++ b/src/app/indicadores/tea-indicators.spec.ts @@ -399,14 +399,18 @@ describe('TEA clinical figures', () => { }); }); - it('annotates only the median and the quartiles on the box', () => { - const figure = boxFigure({ populations: {}, strata: {}, score: 12.5, low: 8, high: 20 }, '2025-S1'); - expect(figure.layout?.annotations?.map((a) => a.text)).toEqual([ - 'Q3 20', - 'Mediana 12.5', - 'Q1 8', - ]); - expect(figure.data[0]).toMatchObject({ hoverinfo: 'skip' }); + it('shows the quartiles and the median in the hover of the box instead of annotations', () => { + const figure = boxFigure( + { populations: {}, strata: {}, score: 12.5, low: 8, high: 20 }, + '2025-S1', + ); + expect(figure.layout?.annotations).toBeUndefined(); + expect(figure.data[0]).toMatchObject({ + q1: [8], + median: [12.5], + q3: [20], + hovertemplate: 'Q3: %{q3:.1~f}
Mediana: %{median:.1~f}
Q1: %{q1:.1~f}', + }); }); const prevalence = { @@ -432,7 +436,7 @@ describe('TEA clinical figures', () => { }; it('shows the prevalence rate per 10,000 as the bar text and the counts in the hover', () => { - const figure = teaSexBars(prevalence, 'female'); + const figure = teaSexBars(prevalence); expect(figure.layout?.barmode).toBeUndefined(); expect(figure.data).toHaveLength(1); expect(figure.data[0]).toMatchObject({ @@ -444,7 +448,7 @@ describe('TEA clinical figures', () => { [3, 6], [1, 4], ], - marker: { color: ['#4269C2', '#C24269'], line: { width: [0, 2] } }, + marker: { color: ['#4269C2', '#C24269'] }, }); expect(figure.data[0]).toMatchObject({ hovertemplate: expect.stringContaining( @@ -453,15 +457,11 @@ describe('TEA clinical figures', () => { }); }); - it('puts the total or the chosen sex rate in the headline', () => { + it('puts the total rate in the headline', () => { expect(prevalenceHeadline(prevalence)).toEqual({ value: '3,333.3', detail: 'por 10 000 niños · 4 confirmados de 12 niños', }); - expect(prevalenceHeadline(prevalence, 'male')).toEqual({ - value: '5,000', - detail: 'por 10 000 niños · 3 confirmados de 6 niños', - }); expect(prevalenceHeadline({ populations: {}, strata: {} })).toBeUndefined(); }); @@ -567,20 +567,36 @@ describe('TEA clinical figures', () => { }; const ageGroups = ['16 a 23 meses', '24 a 36 meses']; - it('stacks diagnostic status in one bar per sex in each age group', () => { + it('stacks diagnostic status as percentages in one bar per sex in each age group', () => { const figure = ageStack(byAgeAndSex, ['Confirmados', 'En proceso']); expect(figure.layout?.barmode).toBe('stack'); expect(figure.data).toMatchObject([ - { x: ageGroups, offsetgroup: 'male', y: [1, 1], marker: { color: '#4269C2' } }, - { x: ageGroups, offsetgroup: 'male', y: [3, 0], marker: { color: 'rgba(66,105,194,0.25)' } }, - { x: ageGroups, offsetgroup: 'female', y: [1, 0], marker: { color: '#C24269' } }, + { + x: ageGroups, + offsetgroup: 'male', + y: [25, 100], + text: ['25 %', '100 %'], + marker: { color: '#4269C2' }, + }, + { + x: ageGroups, + offsetgroup: 'male', + y: [75, 0], + text: ['75 %', ''], + marker: { color: 'rgba(66,105,194,0.25)' }, + }, + { x: ageGroups, offsetgroup: 'female', y: [50, 0], marker: { color: '#C24269' } }, { x: ageGroups, offsetgroup: 'female', - y: [1, 0], + y: [50, 0], marker: { color: 'rgba(194,66,105,0.25)' }, }, ]); + expect(figure.layout?.yaxis).toMatchObject({ + title: { text: '% de pacientes' }, + ticksuffix: ' %', + }); }); it('groups EEG percentages by age and sex and omits missing denominators', () => { @@ -591,14 +607,14 @@ describe('TEA clinical figures', () => { name: 'Masculino', x: ageGroups, y: [25, 100], - text: ['25 % (1/4)', '100 % (1/1)'], + text: ['25 %', '100 %'], marker: { color: '#4269C2' }, }, { name: 'Femenino', x: ageGroups, y: [50, null], - text: ['50 % (1/2)', ''], + text: ['50 %', ''], marker: { color: '#C24269' }, }, ]); diff --git a/src/app/plot.ts b/src/app/plot.ts index 22f38eb..b6592c8 100644 --- a/src/app/plot.ts +++ b/src/app/plot.ts @@ -52,7 +52,11 @@ function layoutOf(layout: Partial = {}): Partial { } function withRoom(data: Partial[]): Plotly.Data[] { - return data.map((d) => (d.type === 'pie' ? { automargin: true, ...d } : d)) as Plotly.Data[]; + return data.map((d) => { + if (d.type === 'pie') return { automargin: true, ...d }; + if (d.type === 'bar') return { cliponaxis: false, ...d }; + return d; + }) as Plotly.Data[]; } const CONFIG: Partial = { responsive: true,