32 lines
1.6 KiB
TypeScript
32 lines
1.6 KiB
TypeScript
import { FigureSelection, PlotlyFigure } from './indicadores.models';
|
|
|
|
export function figureTitle(figure: PlotlyFigure, fallback: string): string {
|
|
const title = figure.layout['title'];
|
|
if (typeof title === 'string' && title.trim()) return title;
|
|
if (title && typeof title === 'object' && 'text' in title
|
|
&& typeof title.text === 'string' && title.text.trim()) return title.text;
|
|
return fallback;
|
|
}
|
|
|
|
// Adapt only the dashboard copy; HTTP figures and all observations stay intact.
|
|
export function presentationFigure(original: PlotlyFigure, selection: FigureSelection): PlotlyFigure {
|
|
const figure = structuredClone(original);
|
|
const title = figure.layout['title'];
|
|
figure.layout['title'] = title && typeof title === 'object' ? { ...title, text: '' } : { text: '' };
|
|
if ((selection.dominio === 'cacu' || selection.dominio === 'cama')
|
|
&& selection.figura === 'fig_c1_puntos' && selection.tipoPeriodo === 'mes'
|
|
&& figure.data.length > 0
|
|
&& figure.data.every(trace => trace['type'] === 'scatter' && trace['mode'] === 'markers'
|
|
&& Array.isArray(trace['x']) && trace['x'].length > 0
|
|
&& trace['x'].every(x => typeof x === 'string' && /^[0-9]{4}-(0[1-9]|1[0-2])$/.test(x)))) {
|
|
const periods = [...new Set(figure.data.flatMap(trace => trace['x'] as string[]))].sort();
|
|
figure.layout['xaxis'] = {
|
|
...(figure.layout['xaxis'] as Record<string, unknown> | undefined),
|
|
type: 'category', categoryorder: 'array', categoryarray: periods,
|
|
tickmode: 'array', tickvals: periods, ticktext: periods,
|
|
tickangle: 0, automargin: true,
|
|
};
|
|
}
|
|
return figure;
|
|
}
|