import { Modelo } from './indicadores-eu7-escenario'; import { PlotlyFigure } from './indicadores.models'; // Legend buttons that highlight one part of a figure, like CaCu's «Distribución de resultados // citológicos»: the chosen group or category keeps its colour and everything else fades. export interface HighlightPoint { trace: number; index: number; } export interface HighlightEntry { id: string; label: string; color: string; // Shorter label for the button when it sits next to its group's button. short?: string; // Only for categories: the group they belong to (its button narrows the categories on offer). group?: string; points: readonly HighlightPoint[]; // Text for the centre of a donut while this entry is chosen. center?: string; } export interface FigureHighlight { groups: readonly HighlightEntry[]; items: readonly HighlightEntry[]; // Traces whose points fade when they are not part of the choice. traces: readonly number[]; // Where a donut shows its centre text: a pie trace's single text, or a layout annotation. center?: { trace: number } | { annotation: number }; // Name of the whole, for the status line. allLabel: string; } export interface HighlightSelection { group?: string; item?: string; } const FADED = .3; // Accepts #RRGGBB and rgb()/rgba() (dona() already writes its colours as rgba). function rgba(color: string, alpha: number): string { const hex = /^#([0-9a-f]{6})$/i.exec(color); if (hex) { const [r, g, b] = [0, 2, 4].map(i => parseInt(hex[1].slice(i, i + 2), 16)); return `rgba(${r}, ${g}, ${b}, ${alpha})`; } const rgb = /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/i.exec(color); return rgb ? `rgba(${rgb[1]}, ${rgb[2]}, ${rgb[3]}, ${alpha})` : color; } export function highlightEntry(h: FigureHighlight, sel: HighlightSelection): HighlightEntry | undefined { return sel.item ? h.items.find(e => e.id === sel.item) : sel.group ? h.groups.find(e => e.id === sel.group) : undefined; } export function highlightLabel(h: FigureHighlight, sel: HighlightSelection): string { return highlightEntry(h, sel)?.label ?? h.allLabel; } // Same state machine as CaCu: choosing the chosen entry again goes back one step. export function toggleHighlight(h: FigureHighlight, sel: HighlightSelection, kind: 'group' | 'item', id: string): HighlightSelection { if (kind === 'group') return !h.groups.some(g => g.id === id) || sel.group === id ? {} : { group: id }; const item = h.items.find(e => e.id === id); if (!item || (sel.group && item.group !== sel.group)) return sel; return sel.item === id ? (sel.group ? { group: sel.group } : {}) : { ...(sel.group ? { group: sel.group } : {}), item: id }; } // The entry a click on the figure means: a category first, otherwise a group. export function highlightAt(h: FigureHighlight, point: HighlightPoint): { kind: 'group' | 'item'; id: string } | undefined { const hit = (e: HighlightEntry) => e.points.some(p => p.trace === point.trace && p.index === point.index); // A group also covers its categories' slices, so an outer slice hits both: the category wins. const item = h.items.find(hit); if (item) return { kind: 'item', id: item.id }; const group = h.groups.find(hit); return group ? { kind: 'group', id: group.id } : undefined; } export function highlightFigure(figure: PlotlyFigure, h: FigureHighlight, sel: HighlightSelection): PlotlyFigure { const entry = highlightEntry(h, sel); if (!entry) return figure; const keep = new Set(entry.points.map(p => p.trace + ':' + p.index)); // A chosen category also keeps its group's slice of the inner ring (not its sibling categories). const group = entry.group ? h.groups.find(g => g.id === entry.group) : undefined; const own = new Set(entry.points.map(p => p.trace)); group?.points.filter(p => !own.has(p.trace)).forEach(p => keep.add(p.trace + ':' + p.index)); const data = figure.data.map((trace, t) => { if (!h.traces.includes(t)) return trace; const marker = (trace['marker'] ?? {}) as Record; if (trace['type'] === 'pie') { const colors = (marker['colors'] as unknown[] | undefined) ?? []; return { ...trace, marker: { ...marker, colors: colors.map((c, i) => typeof c === 'string' && !keep.has(t + ':' + i) ? rgba(c, FADED) : c) } }; } const count = Array.isArray(trace['x']) ? (trace['x'] as unknown[]).length : Array.isArray(trace['y']) ? (trace['y'] as unknown[]).length : 0; return { ...trace, marker: { ...marker, opacity: Array.from({ length: count }, (_, i) => keep.has(t + ':' + i) ? 1 : FADED) } }; }); let layout = figure.layout; if (entry.center !== undefined && h.center) { if ('trace' in h.center) { const t = h.center.trace; data[t] = { ...data[t], text: [entry.center] }; } else { const annotations = [...((layout['annotations'] as Record[] | undefined) ?? [])]; if (annotations[h.center.annotation]) annotations[h.center.annotation] = { ...annotations[h.center.annotation], text: entry.center }; layout = { ...layout, annotations }; } } return { ...figure, data, layout }; } const escape = (v: string) => v.replace(/&/g, '&').replace(//g, '>'); // Buttons for a figure drawn by dona() (traces: 0 centre, 1 groups, 2 categories). A group also // covers its categories' slices; the centre shows the count of the choice. `short` gives each // category's button label next to its group (by default, its full label). export function donaHighlight(m: Modelo, short: (id: string) => string | undefined = () => undefined): FigureHighlight { const centre = (n: number, label: string) => '' + n.toLocaleString('es-MX') + '
' + escape(label); return { traces: [1, 2], center: { trace: 0 }, allLabel: 'TOTAL', groups: m.grupos.map((g, gi) => { const children = m.categorias.map((c, i) => ({ c, i })).filter(({ c }) => c.grupo === g.id); return { id: g.id, label: g.etiqueta, color: g.color, points: [{ trace: 1, index: gi }, ...children.map(({ i }) => ({ trace: 2, index: i }))], center: centre(children.reduce((n, { c }) => n + (m.celdas[c.id].n ?? 0), 0), g.etiqueta) }; }), items: m.categorias.map((c, i) => ({ id: c.id, label: c.etiqueta, short: short(c.id), color: c.color, group: c.grupo, points: [{ trace: 2, index: i }], center: centre(m.celdas[c.id].n ?? 0, c.etiqueta) })) }; } // Rows of buttons: each group followed by its categories; categories without a group share one row. export function highlightRows(h: FigureHighlight): { group?: HighlightEntry; items: HighlightEntry[] }[] { const rows = h.groups.map(group => ({ group, items: h.items.filter(i => i.group === group.id) })); const loose = h.items.filter(i => !i.group || !h.groups.some(g => g.id === i.group)); return loose.length ? [...rows, { items: loose }] : rows; }