From 3bb9c2121d1bb0ad030f94c9527d40c6f11a952f Mon Sep 17 00:00:00 2001 From: edgar Date: Wed, 4 Nov 2026 23:56:54 -0600 Subject: [PATCH] =?UTF-8?q?feat(tea):=20botones=20y=20r=C3=B3tulos=20en=20?= =?UTF-8?q?la=20dona=20C2=20de=20un=20solo=20anillo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sin grupos de categorías (M23), la dona de TEA se dibuja como la de M21: un anillo con rótulos, el total al centro y un botón por categoría. Co-Authored-By: Claude Opus 5.5 --- .../indicadores/indicadores-tea-c2.spec.ts | 28 +++++++++++++++++++ .../indicadores/indicadores-tea-c2.ts | 19 ++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/app/features/indicadores/indicadores-tea-c2.spec.ts diff --git a/src/app/features/indicadores/indicadores-tea-c2.spec.ts b/src/app/features/indicadores/indicadores-tea-c2.spec.ts new file mode 100644 index 0000000..fadcd89 --- /dev/null +++ b/src/app/features/indicadores/indicadores-tea-c2.spec.ts @@ -0,0 +1,28 @@ +import { C2Distribution } from './indicadores-c2-adapter'; +import { highlightFigure } from './indicadores-highlight'; +import { teaC2Donut } from './indicadores-tea-c2'; + +const categoria = (id: string, label: string, value: number, color: string, parentId?: string) => ({ id, label, value, color, parentId }); +const modelo = (extra: object = {}) => ({ period: '2025-S2', unit: 'paciente', title: 'T', total: 380, notes: [], nodes: [], parents: [], + categories: [categoria('CONFIRMADO', 'Diagnóstico confirmado', 338, '#A8C243'), categoria('EN_PROCESO', 'En proceso', 42, '#B8433A')], ...extra }) as unknown as C2Distribution; + +describe('Dona C2 de TEA', () => { + it('sin grupos dibuja un anillo con rótulos, el total al centro y un botón por categoría', () => { + const { figure, highlight } = teaC2Donut(modelo(), 'claro', { text: 'T' })!; + expect(figure.data).toHaveLength(1); + expect(figure.data[0]).toMatchObject({ type: 'pie', values: [338, 42], labels: ['Diagnóstico confirmado', 'En proceso'] }); + expect(figure.data[0]['text']).toEqual(['Diagnóstico confirmado
88.9 %', 'En proceso
11.1 %']); + expect((figure.layout['annotations'] as { text: string }[])[0].text).toBe('380
TOTAL'); + expect(highlight.groups).toEqual([]); + expect(highlight.items.map(i => [i.label, i.color])).toEqual([['Diagnóstico confirmado', '#A8C243'], ['En proceso', '#B8433A']]); + }); + it('al elegir una categoría atenúa la otra y muestra su número al centro', () => { + const { figure, highlight } = teaC2Donut(modelo(), 'claro', { text: 'T' })!; + const elegida = highlightFigure(figure, highlight, { item: 'EN_PROCESO' }); + expect((elegida.data[0]['marker'] as { colors: string[] }).colors).toEqual(['rgba(168, 194, 67, 0.3)', 'rgba(184,67,58,1)']); + expect((elegida.layout['annotations'] as { text: string }[])[0].text).toBe('42
En proceso'); + }); + it('sin pacientes no dibuja nada', () => { + expect(teaC2Donut(modelo({ total: 0 }), 'claro', {})).toBeUndefined(); + }); +}); diff --git a/src/app/features/indicadores/indicadores-tea-c2.ts b/src/app/features/indicadores/indicadores-tea-c2.ts index b5c2aa6..c64312d 100644 --- a/src/app/features/indicadores/indicadores-tea-c2.ts +++ b/src/app/features/indicadores/indicadores-tea-c2.ts @@ -1,5 +1,5 @@ import { C2Distribution } from './indicadores-c2-adapter'; -import { c2Color } from './indicadores-c2-palette'; +import { c2Color, c2Theme } from './indicadores-c2-palette'; import { Categoria, dona, Grupo, Modelo } from './indicadores-eu7-escenario'; import { donaHighlight, FigureHighlight } from './indicadores-highlight'; import { PlotlyFigure, Tema } from './indicadores.models'; @@ -11,10 +11,12 @@ const average = (colors: string[]) => '#' + [1, 3, 5].map(i => Math.round(colors const hex = (c: string | undefined, i: number) => { const v = c2Color(c, i); return /^#[0-9a-f]{6}$/i.test(v) ? v : c2Color(undefined, i); }; export function teaC2Donut(model: C2Distribution, tema: Tema, title: unknown): { figure: PlotlyFigure; highlight: FigureHighlight } | undefined { - if (!model.parents.length || model.total === 0) return undefined; - const categorias: Categoria[] = model.categories.map((c, i) => ({ id: c.id, etiqueta: c.label, color: hex(c.color, i), grupo: 'group:' + c.parentId })); + if (model.total === 0) return undefined; + const unAnillo = !model.parents.length; + const categorias: Categoria[] = model.categories.map((c, i) => ({ id: c.id, etiqueta: c.label, color: hex(c.color, i), grupo: unAnillo ? 'total' : 'group:' + c.parentId })); // A group takes the average colour of its categories, as in CaCu. - const grupos: Grupo[] = model.parents.map(p => ({ id: p.id, etiqueta: p.label, color: average(categorias.filter(c => c.grupo === p.id).map(c => c.color)) })); + const padres = unAnillo ? [{ id: 'total', label: 'TOTAL' }] : model.parents; + const grupos: Grupo[] = padres.map(p => ({ id: p.id, etiqueta: p.label, color: average(categorias.filter(c => c.grupo === p.id).map(c => c.color)) })); const celdas: Modelo['celdas'] = Object.fromEntries(model.categories.map(c => [c.id, { n: c.value, estado: 'Publicado' as const }])); const modelo: Modelo = { periodo: model.period, categorias, grupos, celdas, total: model.total, sinDato: { n: 0, estado: 'Publicado' }, escenarios: [], vacio: false, notas: [...model.notes], sinDatoEtiqueta: '' }; @@ -22,6 +24,15 @@ export function teaC2Donut(model: C2Distribution, tema: Tema, title: unknown): { const unit = model.unit.endsWith('s') ? model.unit : model.unit + 's'; for (const trace of figure.data.slice(1)) trace['hovertemplate'] = '%{label}
%{customdata[0]} ' + unit + ', %{customdata[1]} % de ' + model.total + ''; figure.layout = { ...figure.layout, title }; + if (unAnillo) { + // Without groups, only the categories' ring around the total, like M21's donut. + figure.data = [{ ...figure.data[2], hole: .6 }]; + figure.layout = { ...figure.layout, annotations: [{ x: .5, y: .5, xref: 'paper', yref: 'paper', showarrow: false, + text: '' + model.total.toLocaleString('es-MX') + '
TOTAL', font: { size: 20, color: c2Theme(tema).text } }] }; + return { figure, highlight: { groups: [], traces: [0], center: { annotation: 0 }, allLabel: 'TOTAL', + items: categorias.map((c, i) => ({ id: c.id, label: c.etiqueta, color: c.color, points: [{ trace: 0, index: i }], + center: '' + celdas[c.id].n!.toLocaleString('es-MX') + '
' + c.etiqueta })) } }; + } return { figure, highlight: donaHighlight(modelo) }; }