Files
WebApp/src/app/features/indicadores/indicadores-donut-periods.ts
T
2026-10-26 01:42:32 -06:00

59 lines
3.8 KiB
TypeScript

import { FigureSelection, IndicadorDatos, parsePeriodos, PlotlyFigure } from './indicadores.models';
const record = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v);
// Published C2 trace identity, shared by simple pies and the two concentric rings.
export function tracePeriod(trace: Record<string, unknown>): string | undefined {
return typeof trace['name'] === 'string'
? /^(\d{4}-(?:S[12]|0[1-9]|1[0-2]))(?:$|, )/.exec(trace['name'])?.[1] : undefined;
}
export function periodDatos(data: IndicadorDatos, period: string): IndicadorDatos {
return { ...data, periodos: [period], filas: data.filas.filter(row => row['periodo'] === period) };
}
export interface DonutPeriodFigure { period: string; figure: PlotlyFigure; }
// Select traces before Plotly receives them. Never change counts, per-point
// attributes, suppression, or the original response. Other chart families and
// figures with their own animation/navigation retain their existing renderer.
export function donutPeriodFigures(source: PlotlyFigure, selection: FigureSelection,
reportedPeriods: readonly string[] = []): DonutPeriodFigure[] | undefined {
if ((!source.data.length && selection.figura !== 'fig_c2_donas') || source.frames?.length
|| source.layout['sliders'] !== undefined || source.layout['updatemenus'] !== undefined
|| source.data.some(t => t['type'] !== 'pie' || typeof t['hole'] !== 'number' || t['hole'] <= 0 || t['hole'] >= 1 || !tracePeriod(t))) return;
const received = [...new Set(source.data.map(t => tracePeriod(t)!))];
const periods = [...new Set(selection.periodos ?? [...reportedPeriods, ...received])];
if (!periods.length || periods.some(p => !selection.tipoPeriodo || parsePeriodos(p, selection.tipoPeriodo)?.[0] !== p)) return;
// A single-period figure keeps its exact published geometry and annotations.
if (periods.length === 1 && received.every(p => p === periods[0])) return;
return periods.map(period => {
const figure = structuredClone(source);
figure.data = figure.data.filter(t => tracePeriod(t) === period);
const domains = figure.data.map(t => t['domain']).filter(record);
const xs = domains.flatMap(d => Array.isArray(d['x']) ? d['x'] as number[] : [0, 1]);
const left = Math.min(...xs), right = Math.max(...xs);
const normalize = (x: number) => (x - left) / (right - left);
// Remove only period-labelled center annotations belonging to other pages.
// Documentary notes and all unrecognized annotations remain untouched.
if (Array.isArray(figure.layout['annotations'])) figure.layout['annotations'] = figure.layout['annotations'].flatMap(a => {
if (!record(a) || a['xref'] !== 'paper' || a['yref'] !== 'paper' || a['y'] !== 0.5
|| a['showarrow'] !== false || typeof a['text'] !== 'string') return [a];
const owner = /(?:^|[^\d])(\d{4}-(?:S[12]|0[1-9]|1[0-2]))(?=$|[^\d])/.exec(a['text'])?.[1];
if (!owner || ![...periods, ...reportedPeriods, ...received].includes(owner)) return [a];
return owner === period ? [{ ...a, x: typeof a['x'] === 'number' && right > left ? normalize(a['x']) : 0.5 }] : [];
});
if (Number.isFinite(left) && Number.isFinite(right) && right > left) {
for (const trace of figure.data) {
const domain = trace['domain'];
if (record(domain) && Array.isArray(domain['x']) && domain['x'].every(x => typeof x === 'number')) {
trace['domain'] = { ...domain, x: domain['x'].map(normalize) };
}
}
}
if (!figure.data.length) {
// An empty donut page has no Cartesian observations. Keep the active
// suppression/availability annotation, without Plotly's default axes.
for (const axis of ['xaxis', 'yaxis']) figure.layout[axis] = { ...(record(figure.layout[axis]) ? figure.layout[axis] : {}), visible: false };
}
return { period, figure };
});
}