diff --git a/src/app/indicadores/bars.spec.ts b/src/app/indicadores/bars.spec.ts index cfcae1b..b1d9cc1 100644 --- a/src/app/indicadores/bars.spec.ts +++ b/src/app/indicadores/bars.spec.ts @@ -510,25 +510,51 @@ describe('independent bars by group', () => { }); describe('the subsequent round by last screening', () => { - it('draws each answer as its own group after the subsequent one', () => { - const cell = (n: number) => ({ 'initial-population': n }); + it('chains the steps of a time window, quartiles moving with their bar', () => { + const median = (score: number, low: number, high: number): Reading => ({ + populations: { 'measure-observation': 1 }, + strata: {}, + score, + low, + high, + }); + const steps = [ + { label: 'a', measure: 'A' }, + { label: 'b', measure: 'B' }, + ]; + const table = bars({ kind: 'days', label: 'x', steps, staggered: true }, (m) => + m === 'A' ? median(10, 5, 10) : median(20, 10, 30), + )!; + expect(table.rows.map((r) => [r.offset, r.width, r.marks])).toEqual([ + [0, 25, [12.5, 25]], + [25, 50, [50, 100]], + ]); + }); + it('splits the subsequent round by last screening, keeping those without an answer', () => { + const one = { 'initial-population': 1 }; const r: Reading = { populations: { 'initial-population': 3 }, strata: { - 'round-category': { 'subsequent|NILM': cell(3) }, - 'subsequent_history-category': { 'within_1_year|NILM': cell(2) }, + 'round-category': { 'initial|a': one, 'subsequent|a': { 'initial-population': 2 } }, + 'subsequent_history-category': { 'within_1_year|a': one, 'no_data|a': one }, }, }; - const catalog = [{ code: 'NILM', label: 'Normal', color: '#000' }]; - const found = grouped( - { kind: 'grouped', label: 'x', measure: 'M', catalog, unit: 'pacientes', stratum: 'round', order: ['subsequent'] }, - () => r, - )!; - expect(found.groups.map((g) => [g.code, g.total])).toEqual([ - ['subsequent', 3], - ['within_1_year', 2], + const mode: Mode = { + kind: 'grouped', + label: 'x', + measure: 'M', + stratum: 'round', + order: ['initial', 'subsequent'], + unit: 'pacientes', + catalog: [{ code: 'a', label: 'A', color: '#000' }], + }; + const { groups } = grouped(mode, () => r)!; + expect(groups.map((g) => [g.code, g.total])).toEqual([ + ['initial', 1], + ['within_1_year', 1], ['2_years', 0], ['3_or_more_years', 0], + ['subsequent_without_last_screening', 1], ]); }); }); diff --git a/src/app/indicadores/bars.ts b/src/app/indicadores/bars.ts index 9466559..1c6c41a 100644 --- a/src/app/indicadores/bars.ts +++ b/src/app/indicadores/bars.ts @@ -69,7 +69,13 @@ export type Mode = { ofAll?: boolean; hidden?: Hidden; } - | { kind: 'days'; steps: { label: string; measure: string }[]; tint?: Tint } + | { + kind: 'days'; + steps: { label: string; measure: string }[]; + tint?: Tint; + /** each bar starts where the previous one ends (a schematic sequence, as a Gantt chart) */ + staggered?: boolean; + } /** A proportion as one bar of 0 to 100 %, its 95 % interval as the two marks on it */ | { kind: 'ratio'; measure: string; colors?: Colors; percentLabel?: string } | { kind: 'days-by'; measure: string; stratum: string; tint?: Tint } @@ -156,6 +162,8 @@ export interface Bar { /** why the row has no figures: no patients in the group, or not available */ hint?: string; color?: string; + /** where the bar starts, in % of the scale */ + offset?: number; width: number; marks: number[]; cells: string[]; @@ -372,6 +380,7 @@ export function bars(mode: Mode, of: (measure: string) => Reading | undefined): return { label, n: r?.populations['measure-observation'] ?? 0, score: r }; }), mode.tint, + mode.staggered, ); case 'days-by': { const r = of(mode.measure); @@ -471,12 +480,17 @@ export function bars(mode: Mode, of: (measure: string) => Reading | undefined): } } -/** The subsequent round split by the form's last screening, drawn after that round's group. */ +/** The subsequent round split by the form's last screening, drawn in place of that round's group. */ const LAST_SCREENING: [string, string][] = [ ['within_1_year', 'Subsecuente: última detección hace un año o menos'], ['2_years', 'Subsecuente: última detección hace dos años'], ['3_or_more_years', 'Subsecuente: última detección hace tres años o más'], ]; +/** Subsequent records whose form gives none of those answers: drawn only when there are some. */ +const NO_LAST_SCREENING: [string, string] = [ + 'subsequent_without_last_screening', + 'Subsecuente sin dato de última detección', +]; /** The groups of a grouped mode: each group (an age group, a follow-up status, a screening round) with * one independent bar per category over the group's own records. A group the report leaves out is @@ -493,8 +507,16 @@ export function grouped( const history = mode.stratum === 'round' ? (r?.strata['subsequent_history-category'] ?? {}) : {}; const answers = LAST_SCREENING.map(([code]) => code); + const unanswered = Object.keys(history).filter((k) => !answers.includes(k.split('|')[0])); + /** the cells that make up a group, with their category */ + const cellsOf = (g: string): [string, Record][] => + g === NO_LAST_SCREENING[0] + ? unanswered.map((k) => [k.split('|')[1], history[k]]) + : Object.entries(answers.includes(g) ? history : cells) + .filter(([k]) => k.split('|')[0] === g) + .map(([k, c]) => [k.split('|')[1], c]); const n = (g: string, c: string) => - size((answers.includes(g) ? history : cells)[`${g}|${c}`]); + cellsOf(g).reduce((t, [category, cell]) => (category === c ? t + size(cell) : t), 0); const reconciled = reconciles( Object.values(cells).map((c) => c['initial-population']), patients(r), @@ -505,21 +527,24 @@ export function grouped( ]; const total = (g: string) => mode.ofAll - ? Object.entries(answers.includes(g) ? history : cells) - .filter(([k]) => k.split('|')[0] === g) - .reduce((t, [, c]) => t + size(c), 0) + ? cellsOf(g).reduce((t, [, c]) => t + size(c), 0) : mode.catalog.reduce((t, c) => t + n(g, c.code), 0); const at = codes.indexOf('subsequent'); const drawn = mode.stratum === 'round' && at >= 0 && r?.strata['subsequent_history-category'] - ? [...codes.slice(0, at + 1), ...answers, ...codes.slice(at + 1)] + ? [ + ...codes.slice(0, at), + ...answers, + ...(unanswered.length ? [NO_LAST_SCREENING[0]] : []), + ...codes.slice(at + 1), + ] : codes; const groups: Group[] = drawn.map((code) => { const den = total(code); return { code, label: - LAST_SCREENING.find(([answer]) => answer === code)?.[1] ?? + [...LAST_SCREENING, NO_LAST_SCREENING].find(([answer]) => answer === code)?.[1] ?? (mode.stratum === 'age_group' && code === '<25' && ['CACU_EU7', 'CACU_VPH_GENOTIPO'].includes(mode.measure) @@ -560,7 +585,7 @@ export function grouped( let note = hiddenNote(mode, of); if (mode.complete) { const unclassified = total('no_data'); - const classified = groups.filter((g) => g.code !== 'no_data' && !answers.includes(g.code)); + const classified = groups.filter((g) => g.code !== 'no_data'); const none = classified.every((g) => !g.total); const sentence = !reconciled ? 'Los conteos del informe no concilian con el total: los grupos ausentes no se muestran como cero.' @@ -639,21 +664,33 @@ type Score = { score?: number; low?: number; high?: number }; function medians( rows: { label: string; n: number; score?: Score }[], tint?: Tint, + staggered?: boolean, ): Bars | undefined { if (!rows.some((r) => r.score?.score !== undefined)) return undefined; - const top = Math.max(...rows.flatMap((r) => [r.score?.score ?? 0, r.score?.high ?? 0])) || 1; + // a step without a median moves nothing after it + let elapsed = 0; + const starts = rows.map((r) => { + const start = elapsed; + if (staggered) elapsed += r.score?.score ?? 0; + return start; + }); + const ends = rows.map( + (r, i) => starts[i] + Math.max(r.score?.score ?? 0, r.score?.high ?? 0), + ); + const top = Math.max(...ends) || 1; const at = (v: number) => (100 * v) / top; return { head: ['Mediana', 'Observaciones', 'Q1–Q3'], markColor: tint?.mark, - rows: rows.map(({ label, n, score: s }) => { + rows: rows.map(({ label, n, score: s }, i) => { if (s?.score === undefined) return { label, width: 0, marks: [], cells: ['—', `${n}`, '—'] }; const spread = s.low !== undefined && s.high !== undefined ? [s.low, s.high] : []; return { label, color: tint?.bar, + offset: staggered ? at(starts[i]) : undefined, width: at(s.score), - marks: spread.map(at), + marks: spread.map((v) => at(starts[i] + v)), cells: [`${days(s.score)} días`, `${n}`, spread.map(days).join('–') || '—'], }; }), diff --git a/src/app/indicadores/pages-cacu.ts b/src/app/indicadores/pages-cacu.ts index c02e594..080fc4f 100644 --- a/src/app/indicadores/pages-cacu.ts +++ b/src/app/indicadores/pages-cacu.ts @@ -227,7 +227,9 @@ export const CACU: Page = { kind: 'days', tint: TINT, label: 'Por ventana de tiempo', - caption: 'Cada paso mide a sus propias pacientes: las medianas no se suman.', + staggered: true, + caption: + 'Esquema: cada barra empieza donde termina la anterior. Cada paso mide a sus propias pacientes: las medianas no se suman.', steps: [ { label: 'Toma a resultado de citología', diff --git a/src/app/indicadores/readings.ts b/src/app/indicadores/readings.ts index d379a9c..cf0c56f 100644 --- a/src/app/indicadores/readings.ts +++ b/src/app/indicadores/readings.ts @@ -116,6 +116,7 @@ import { Reading } from './measures'; } @else {