import { C2Distribution } from './indicadores-c2-adapter'; 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'; // 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.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 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: '' }; 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}
%{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) }; } // 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' }; }