feat(tea): botones y rótulos en la dona C2 de un solo anillo
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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<br>88.9 %', 'En proceso<br>11.1 %']);
|
||||
expect((figure.layout['annotations'] as { text: string }[])[0].text).toBe('<b>380</b><br>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('<b>42</b><br>En proceso');
|
||||
});
|
||||
it('sin pacientes no dibuja nada', () => {
|
||||
expect(teaC2Donut(modelo({ total: 0 }), 'claro', {})).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -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}<br>%{customdata[0]} ' + unit + ', %{customdata[1]} % de ' + model.total + '<extra></extra>';
|
||||
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: '<b>' + model.total.toLocaleString('es-MX') + '</b><br>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: '<b>' + celdas[c.id].n!.toLocaleString('es-MX') + '</b><br>' + c.etiqueta })) } };
|
||||
}
|
||||
return { figure, highlight: donaHighlight(modelo) };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user