feat(indicadores): completa metadatos y navegación multiperiodo

This commit is contained in:
Angel Capetillo Zavala
2026-10-23 01:18:24 -06:00
parent 1fe88dfb45
commit 9639423a32
25 changed files with 864 additions and 70 deletions
@@ -6,7 +6,8 @@ export interface DonutHierarchy {
categoryTrace: number;
parentTrace?: number;
}
export interface DonutSelection { parent?: string; category?: string; }
export interface DonutSelection { parent?: string; category?: string; period?: string; }
export interface DonutLegendEntry { label: string; color?: string; }
function record(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
@@ -37,6 +38,39 @@ function centerCountAt(value: unknown, x: number, y: number): value is Record<st
&& typeof value['text'] === 'string' && /^<b>\d+<\/b>$/.test(value['text']);
}
export function donutLegend(figure: PlotlyFigure): readonly DonutLegendEntry[] | undefined {
if (figure.data.length !== 1) return;
const trace = figure.data[0];
if (trace['type'] !== 'pie' || !labels(trace)) return;
const marker = record(trace['marker']) ? trace['marker'] : undefined;
const colors = marker && Array.isArray(marker['colors']) && marker['colors'].length === trace.labels.length
? marker['colors'] : [];
return trace.labels.map((label, index) => ({
label,
color: typeof colors[index] === 'string' && /^#[0-9a-f]{6}$/i.test(colors[index] as string)
? colors[index] as string : undefined,
}));
}
function donutCanvas(source: PlotlyFigure): PlotlyFigure {
const figure = structuredClone(source);
figure.layout['showlegend'] = false;
figure.layout['height'] = 440;
figure.layout['margin'] = { ...(record(figure.layout['margin']) ? figure.layout['margin'] : {}),
l: 16, r: 16, t: 16, b: 16, pad: 0, autoexpand: false };
const annotations = figure.layout['annotations'];
if (Array.isArray(annotations)) figure.layout['annotations'] = annotations.map(a => centerCount(a)
? { ...a, x: 0.5, y: 0.5, xanchor: 'center', yanchor: 'middle' } : a);
return figure;
}
export function donutSimpleView(source: PlotlyFigure): PlotlyFigure {
const figure = donutCanvas(source);
figure.data = figure.data.map(trace => trace['type'] === 'pie'
? { ...trace, domain: { x: [0, 1], y: [0, 1] }, showlegend: false } : trace);
return figure;
}
// Only a structural candidate: /datos supplies the semantics. No indicator ID,
// category count, legendgroup, trace order or exact domain decides eligibility.
// C2 metadata describes category -> parent, not further levels or facets.
@@ -117,7 +151,7 @@ export function donutHierarchy(figure: PlotlyFigure, data: IndicadorDatos): Donu
// The inner ring retains ALL parents so another can be selected directly. Its
// denominator is the full verified partition; the outer ring is within-parent.
export function donutView(source: PlotlyFigure, hierarchy: DonutHierarchy, parent?: string, _availableWidth?: number): PlotlyFigure {
const figure = structuredClone(source);
const figure = donutCanvas(source);
const meta = figure.layout['meta'];
const unit = record(meta) && typeof meta['unit'] === 'string' && !/[<>&]/.test(meta['unit']) ? ' ' + meta['unit'] : '';
const outer = figure.data[hierarchy.categoryTrace];
@@ -139,10 +173,6 @@ export function donutView(source: PlotlyFigure, hierarchy: DonutHierarchy, paren
x: (plotDomain.x[0] + plotDomain.x[1]) / 2,
y: (plotDomain.y[0] + plotDomain.y[1]) / 2,
};
figure.layout['showlegend'] = false;
figure.layout['height'] = 440;
figure.layout['margin'] = { ...(record(figure.layout['margin']) ? figure.layout['margin'] : {}),
l: 16, r: 16, t: 16, b: 16, pad: 0, autoexpand: false };
const annotations = figure.layout['annotations'];
if (Array.isArray(annotations)) figure.layout['annotations'] = annotations.map(a => centerCount(a)
? { ...a, x: center.x, y: center.y, xanchor: 'center', yanchor: 'middle' } : a);