feat(tea): botones para resaltar grupos y categorías en las figuras
- Leyenda con botones, como la distribución de resultados citológicos de cáncer cervicouterino: el grupo o la categoría elegida conserva su color, el resto se atenúa, «Volver» retrocede y un clic en la figura equivale al botón (indicadores-highlight.ts). - M21, M22 y M24 con botones por grupo de edad; el centro de las donas muestra la selección. - En TEA, la dona C2 de un periodo (M23) se dibuja con ambos anillos y botones, y sus barras también los tienen.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { C2Distribution } from './indicadores-c2-adapter';
|
||||
import { c2Color } 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';
|
||||
|
||||
// TEA (mock): C2 distributions with groups are drawn like CaCu's «Distribución de resultados
|
||||
// citológicos», both rings at once with highlight buttons, instead of the drill-down donut.
|
||||
const average = (colors: string[]) => '#' + [1, 3, 5].map(i => Math.round(colors.reduce((s, c) => s + parseInt(c.slice(i, i + 2), 16), 0) / colors.length)
|
||||
.toString(16).padStart(2, '0')).join('').toUpperCase();
|
||||
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 }));
|
||||
// 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 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: '' };
|
||||
const figure = dona(modelo, {}, tema);
|
||||
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 };
|
||||
return { figure, highlight: donaHighlight(modelo) };
|
||||
}
|
||||
|
||||
// Buttons for the C2 bars: one per category (bar), and its group highlights all of its bars.
|
||||
export function c2BarsHighlight(model: C2Distribution): FigureHighlight {
|
||||
const items = model.categories.map((c, i) => ({ id: c.id, label: c.label, color: hex(c.color, i),
|
||||
group: c.parentId ? 'group:' + c.parentId : undefined, points: [{ trace: 0, index: i }] }));
|
||||
const groups = model.parents.map(p => {
|
||||
const children = items.filter(c => c.group === p.id);
|
||||
return { id: p.id, label: p.label, color: average(children.map(c => c.color)), points: children.flatMap(c => c.points) };
|
||||
});
|
||||
return { groups, items, traces: [0], allLabel: 'Todas las categorías' };
|
||||
}
|
||||
Reference in New Issue
Block a user