58ff3870be
Cambio de los colores de las graficas para tener una mejor armonía de colores y evitar el uso de la unión de colores que interpreten ideas falsas
72 lines
5.2 KiB
TypeScript
72 lines
5.2 KiB
TypeScript
import { percentNumber } from './indicadores-format';
|
|
import { FigureHighlight } from './indicadores-highlight';
|
|
import { C2_PALETTE, c2Theme } from './indicadores-c2-palette';
|
|
import { FigureSelection, IndicadorDatos, PlotlyFigure } from './indicadores.models';
|
|
|
|
// fig_c1_barras_alteracion: a C1 proportion whose groups are the alterations of the EEG. One bar per alteration with
|
|
// the share of the period's children that have it, after a first bar for the children with any of them (the TOTAL
|
|
// group). A child counts in every alteration it has, so the bars don't add up. Several periods draw side by side.
|
|
export const C1_ALTERATION_BARS = 'fig_c1_barras_alteracion';
|
|
const TOTAL = 'TOTAL';
|
|
const ANY_LABEL = 'Alguna alteración';
|
|
|
|
export type C1AlterationResult = { status: 'unsupported'; reason: string }
|
|
| { status: 'supported'; figure: PlotlyFigure; highlight: FigureHighlight; total: number; notes: string[] };
|
|
export function c1AlterationCapability(contract: string, representation: string): boolean {
|
|
return contract === 'C1' && representation === C1_ALTERATION_BARS;
|
|
}
|
|
|
|
const count = (v: unknown): v is number => typeof v === 'number' && Number.isSafeInteger(v) && v >= 0;
|
|
const escape = (v: string) => v.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
// The any-alteration bar uses the second series colour; each alteration takes its own colour.
|
|
const COLORS = [C2_PALETTE.series[1], ...C2_PALETTE.edades, C2_PALETTE.series[2], C2_PALETTE.series[3], C2_PALETTE.series[0]];
|
|
const color = (i: number) => COLORS[i % COLORS.length];
|
|
|
|
export function c1AlterationFigure(data: IndicadorDatos, selection: FigureSelection): C1AlterationResult {
|
|
const unsupported = (reason: string): C1AlterationResult => ({ status: 'unsupported', reason });
|
|
if (!c1AlterationCapability(data.contrato, selection.figura)) return unsupported('Representación no soportada');
|
|
if (data['estrato'] != null) return unsupported('Esta figura no admite una dimensión');
|
|
const periods = selection.periodos ? data.periodos.filter(p => selection.periodos!.includes(p)) : data.periodos;
|
|
if (!periods.length) return unsupported('No hay periodos publicados para esta selección');
|
|
const published = data['grupos'];
|
|
const groups = Object.entries(published && typeof published === 'object' ? published : {}).filter(([id]) => id !== TOTAL);
|
|
if (!groups.length) return unsupported('El indicador no publica grupos por alteración');
|
|
const bars = [{ id: TOTAL, label: ANY_LABEL }, ...groups.map(([id, label]) => ({ id, label: String(label) }))];
|
|
|
|
const cells = periods.map(period => bars.map(bar =>
|
|
data.filas.filter(r => r['periodo'] === period && r['grupo'] === bar.id && r['estrato'] == null)));
|
|
if (cells.some(row => row.some(found => found.length !== 1))) return unsupported('Grupos por alteración incompletos');
|
|
const rows = cells.map(row => row.map(([r]) => r));
|
|
if (rows.some(row => row.some(r => (r['suprimido'] !== 0 && r['suprimido'] !== false)
|
|
|| !count(r['numerador']) || !count(r['denominador']) || r['numerador'] > r['denominador'])))
|
|
return unsupported('Conteos por alteración suprimidos o no verificables');
|
|
|
|
const theme = c2Theme(selection.tema);
|
|
const x = bars.map(b => escape(b.label));
|
|
const single = periods.length === 1;
|
|
const traces = rows.map((row, p) => {
|
|
const y = row.map(r => (r['denominador'] as number) ? 100 * (r['numerador'] as number) / (r['denominador'] as number) : null);
|
|
return { type: 'bar', name: periods[p], x, y,
|
|
marker: { color: single ? bars.map((_, i) => color(i)) : color(p), line: { color: theme.background, width: 1 } },
|
|
text: y.map(v => v === null ? '' : percentNumber(v) + ' %'), textposition: 'outside', cliponaxis: false,
|
|
customdata: row.map(r => [r['numerador'], r['denominador']]),
|
|
hovertemplate: '%{x}' + (single ? '' : ', %{fullData.name}') + '<br>%{y:.1f} % (%{customdata[0]} de %{customdata[1]} niños)<extra></extra>' };
|
|
});
|
|
const figure: PlotlyFigure = {
|
|
data: traces,
|
|
layout: { autosize: true, height: 400, barmode: 'group', margin: { t: 56, l: 72, r: 24, b: single ? 64 : 96 }, showlegend: !single,
|
|
legend: { orientation: 'h', y: -0.3, itemclick: false, itemdoubleclick: false },
|
|
title: { text: escape(String(data['titulo'] ?? data.id)) + (single ? ', ' + periods[0] : '') },
|
|
paper_bgcolor: theme.background, plot_bgcolor: theme.background, font: { color: theme.text },
|
|
xaxis: { type: 'category', title: { text: 'Alteración del EEG' }, automargin: true },
|
|
yaxis: { title: { text: '% de niños con EEG de buena calidad' }, range: [0, 100], ticksuffix: ' %', gridcolor: theme.grid, zeroline: false } },
|
|
config: { displayModeBar: false },
|
|
};
|
|
const highlight: FigureHighlight = { groups: [], traces: traces.map((_, t) => t), allLabel: 'Todas las alteraciones',
|
|
items: bars.map((b, i) => ({ id: b.id, label: b.label, color: single ? color(i) : theme.line,
|
|
points: traces.map((_, t) => ({ trace: t, index: i })) })) };
|
|
// Children with a first good EEG over the periods drawn: zero means there is nothing to draw a share of.
|
|
const total = rows.reduce((sum, row) => sum + (row[0]['denominador'] as number), 0);
|
|
return { status: 'supported', figure, highlight, total, notes: Array.isArray(data.notas) ? [...data.notas] : [] };
|
|
}
|