integrating latest dashboard changes
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { c2Figure } from './indicadores-c2-adapter';
|
||||
import { DonutSelection } from './indicadores-donut-drilldown';
|
||||
import { periodDatos } from './indicadores-donut-periods';
|
||||
import { FigureSelection, IndicadorDatos, PlotlyFigure } from './indicadores.models';
|
||||
|
||||
const record = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v);
|
||||
// Only known per-point arrays may be sliced. Unknown array semantics require
|
||||
// the complete presentation, rather than risking misaligned metadata.
|
||||
const pointArrays = new Set([
|
||||
'x', 'y', 'text', 'hovertext', 'customdata', 'ids', 'hovertemplate', 'texttemplate', 'textposition',
|
||||
'base', 'width', 'offset', 'marker.color', 'marker.opacity', 'marker.line.color', 'marker.line.width',
|
||||
'marker.pattern.shape', 'marker.pattern.size', 'marker.pattern.solidity', 'marker.pattern.fgcolor', 'marker.pattern.bgcolor',
|
||||
'error_x.array', 'error_x.arrayminus', 'error_y.array', 'error_y.arrayminus',
|
||||
'textfont.color', 'textfont.size', 'textfont.family', 'insidetextfont.color', 'insidetextfont.size', 'insidetextfont.family',
|
||||
'outsidetextfont.color', 'outsidetextfont.size', 'outsidetextfont.family',
|
||||
]);
|
||||
|
||||
export interface C2GeneralSelection { figure: PlotlyFigure; context: string; }
|
||||
// Select published observations, never recompute a denominator or interpret a
|
||||
// visual parent as filas[].grupo (the latter is the statistical population).
|
||||
export function c2GeneralSelection(source: PlotlyFigure, data: IndicadorDatos,
|
||||
selection: FigureSelection, local: DonutSelection): C2GeneralSelection | undefined {
|
||||
const period = local.period;
|
||||
if (selection.figura !== 'fig_c2_barras' || (!local.parent && !local.category) || !period
|
||||
|| !data.periodos.includes(period) || (selection.periodos && !selection.periodos.includes(period))
|
||||
|| source.frames?.length || source.layout['sliders'] !== undefined || source.layout['updatemenus'] !== undefined
|
||||
|| !!source.layout['barnorm'] || Object.keys(source.layout).some(key => /^[xy]axis\d+$/.test(key))) return;
|
||||
const adapted = c2Figure(periodDatos(data, period), { ...selection, periodos: [period] });
|
||||
if (adapted.status !== 'supported' || !adapted.model.parents.length || !adapted.model.categories.length) return;
|
||||
const { categories, parents } = adapted.model;
|
||||
const meta = source.layout['meta'];
|
||||
if (record(meta) && ((meta['period'] !== undefined && meta['period'] !== period)
|
||||
|| (meta['denominator'] !== undefined && meta['denominator'] !== adapted.model.total))) return;
|
||||
const parent = local.parent ? parents.find(p => p.label === local.parent) : undefined;
|
||||
const category = local.category ? categories.find(c => c.label === local.category) : undefined;
|
||||
if ((local.parent && !parent) || (local.category && !category)
|
||||
|| (parent && category && 'group:' + category.parentId !== parent.id)) return;
|
||||
const selected = categories.filter(c => (!parent || 'group:' + c.parentId === parent.id) && (!category || c.id === category.id));
|
||||
if (!selected.length) return;
|
||||
// A trace must explicitly identify its period and match the entire verified
|
||||
// partition before any categories are hidden. Never filter a partial trace.
|
||||
const names = source.data.map(t => t['name']);
|
||||
if (new Set(names).size !== names.length || source.data.some(t => t['type'] !== 'bar' || t['orientation'] !== 'h'
|
||||
|| typeof t['name'] !== 'string' || !data.periodos.includes(t['name']))) return;
|
||||
const trace = source.data.find(t => t['name'] === period);
|
||||
if (!trace || !Array.isArray(trace['x']) || !Array.isArray(trace['y'])
|
||||
|| ['transforms', 'xsrc', 'ysrc'].some(key => trace[key] !== undefined)
|
||||
|| (trace['xaxis'] !== undefined && trace['xaxis'] !== 'x') || (trace['yaxis'] !== undefined && trace['yaxis'] !== 'y')
|
||||
|| trace['x'].length !== categories.length || trace['y'].length !== categories.length
|
||||
|| new Set(trace['y']).size !== categories.length) return;
|
||||
const labels = trace['y'] as unknown[], values = trace['x'] as unknown[];
|
||||
if (categories.some(c => { const i = labels.indexOf(c.label); return i < 0 || values[i] !== c.value; })) return;
|
||||
const indices = labels.flatMap((label, i) => selected.some(c => c.label === label) ? [i] : []);
|
||||
let compatible = true;
|
||||
const slice = (value: unknown, path: string): unknown => {
|
||||
if (Array.isArray(value)) {
|
||||
if (!pointArrays.has(path) || value.length !== categories.length) { compatible = false; return value; }
|
||||
return indices.map(i => structuredClone(value[i]));
|
||||
}
|
||||
if (record(value)) return Object.fromEntries(Object.entries(value).map(([key, v]) => [key, slice(v, path ? path + '.' + key : key)]));
|
||||
return value;
|
||||
};
|
||||
const filtered = slice(trace, '') as Record<string, unknown>;
|
||||
if (!compatible) return;
|
||||
const axis = source.layout['yaxis'];
|
||||
if (record(axis) && (axis['tickvals'] !== undefined || axis['ticktext'] !== undefined
|
||||
|| (axis['type'] !== undefined && axis['type'] !== 'category'))) return;
|
||||
const figure = structuredClone(source);
|
||||
figure.data = [filtered];
|
||||
if (record(figure.layout['yaxis']) && Array.isArray(figure.layout['yaxis']['categoryarray'])) {
|
||||
figure.layout['yaxis']['categoryarray'] = figure.layout['yaxis']['categoryarray'].filter(v => selected.some(c => c.label === v));
|
||||
}
|
||||
return { figure, context: 'Vista local de la dona: ' + [local.parent, local.category, period].filter(Boolean).join(' · ')
|
||||
+ '. Sólo se muestran las categorías seleccionadas de este periodo. Los conteos y porcentajes conservan el denominador de la distribución completa del periodo.' };
|
||||
}
|
||||
Reference in New Issue
Block a user