diff --git a/src/app/indicadores/tab-indicadores.html b/src/app/indicadores/tab-indicadores.html
index 6be39f0..4eb4c9e 100644
--- a/src/app/indicadores/tab-indicadores.html
+++ b/src/app/indicadores/tab-indicadores.html
@@ -90,7 +90,7 @@
@if (reports.isLoading()) {
} @else if (routeOf(section); as fig) {
-
+
} @else {
Sin tiempos publicados en el periodo.
}
@@ -147,7 +147,7 @@
@if (reports.isLoading()) {
} @else if (figureOf(f); as fig) {
-
+
} @else {
Sin datos publicados
}
@@ -219,7 +219,7 @@
@if (reports.isLoading()) {
} @else if (figureOf(f); as fig) {
-
+
} @else {
Sin datos en el periodo.
}
diff --git a/src/app/indicadores/tab-indicadores.ts b/src/app/indicadores/tab-indicadores.ts
index c847c78..de55fb9 100644
--- a/src/app/indicadores/tab-indicadores.ts
+++ b/src/app/indicadores/tab-indicadores.ts
@@ -105,7 +105,6 @@ export class TabIndicadores {
});
}
- protected readonly clinicalHeight = 430;
protected readonly pending = 'Indicador no publicado en el catálogo institucional.';
protected setType(type: PeriodType): void {
this.type.set(type);
diff --git a/src/app/plot-headroom.spec.ts b/src/app/plot-headroom.spec.ts
new file mode 100644
index 0000000..d2d27b1
--- /dev/null
+++ b/src/app/plot-headroom.spec.ts
@@ -0,0 +1,51 @@
+import { describe, expect, it } from 'vitest';
+import { percentAxis } from './indicadores/figures';
+import { HEADROOM, headroom } from './plot-headroom';
+
+const bars = (extra: Record) => [
+ { type: 'bar', textposition: 'outside', ...extra },
+];
+
+describe('headroom', () => {
+ it('lifts the value axis of vertical bars with outside labels above the maximum', () => {
+ expect(headroom(bars({ x: ['a', 'b'], y: [4, 20] }) as never, {})).toEqual({
+ yaxis: { range: [0, 20 * HEADROOM] },
+ });
+ });
+
+ it('lifts the value axis of horizontal bars with outside labels above the maximum', () => {
+ expect(headroom(bars({ orientation: 'h', y: ['a', 'b'], x: [3, 40] }) as never, {})).toEqual({
+ xaxis: { range: [0, 40 * HEADROOM] },
+ });
+ });
+
+ it('pads below zero for negative values and ignores missing ones', () => {
+ expect(headroom(bars({ y: [-10, null, 5] }) as never, {})).toEqual({
+ yaxis: { range: [-10 * HEADROOM, 5 * HEADROOM] },
+ });
+ });
+
+ it('keeps the other axis properties and leaves explicit ranges alone', () => {
+ const data = bars({ y: [8] }) as never;
+ expect(headroom(data, { yaxis: { title: { text: 'n' } } })).toEqual({
+ yaxis: { title: { text: 'n' }, range: [0, 8 * HEADROOM] },
+ });
+ expect(headroom(data, { yaxis: { range: [0, 9] } })).toEqual({});
+ });
+
+ it('leaves bars without outside labels and all-zero bars on autorange', () => {
+ expect(headroom([{ type: 'bar', textposition: 'inside', y: [5] }] as never, {})).toEqual({});
+ expect(headroom([{ type: 'bar', y: [5] }] as never, {})).toEqual({});
+ expect(headroom(bars({ y: [0, 0] }) as never, {})).toEqual({});
+ });
+});
+
+describe('percentAxis', () => {
+ it('leaves room above 100 while the ticks stop at 100', () => {
+ const [low, high] = percentAxis.range;
+ expect(low).toBe(0);
+ expect(high).toBeGreaterThanOrEqual(110);
+ expect(percentAxis.dtick).toBe(20);
+ expect(Math.floor(high / percentAxis.dtick) * percentAxis.dtick).toBe(100);
+ });
+});
diff --git a/src/app/plot-headroom.ts b/src/app/plot-headroom.ts
new file mode 100644
index 0000000..b012df0
--- /dev/null
+++ b/src/app/plot-headroom.ts
@@ -0,0 +1,31 @@
+import type * as Plotly from 'plotly.js';
+
+export const HEADROOM = 1.15;
+
+const AXES = ['xaxis', 'yaxis'] as const;
+
+export function headroom(
+ data: Partial[],
+ layout: Partial,
+): Partial {
+ const labelled: Record<(typeof AXES)[number], number[]> = { xaxis: [], yaxis: [] };
+ for (const trace of data as Partial[]) {
+ if (trace.type !== 'bar' || trace.textposition !== 'outside') continue;
+ const horizontal = trace.orientation === 'h';
+ const values = ((horizontal ? trace.x : trace.y) ?? []) as unknown[];
+ labelled[horizontal ? 'xaxis' : 'yaxis'].push(
+ ...values.filter((v): v is number => typeof v === 'number' && Number.isFinite(v)),
+ );
+ }
+ return Object.fromEntries(
+ AXES.flatMap((key) => {
+ const values = labelled[key];
+ const axis = layout[key];
+ if (!values.length || axis?.range) return [];
+ const low = Math.min(0, ...values);
+ const high = Math.max(0, ...values);
+ if (low === high) return [];
+ return [[key, { ...axis, range: [low * HEADROOM, high * HEADROOM] as [number, number] }]];
+ }),
+ );
+}
diff --git a/src/app/plot.ts b/src/app/plot.ts
index b6592c8..15be4f3 100644
--- a/src/app/plot.ts
+++ b/src/app/plot.ts
@@ -1,5 +1,6 @@
import { Component, effect, ElementRef, input, OnDestroy, viewChild } from '@angular/core';
import type * as Plotly from 'plotly.js';
+import { headroom } from './plot-headroom';
import plotly from './plotly-bundle';
export type Figure = { data: Partial[]; layout?: Partial };
@@ -29,7 +30,10 @@ const axisDefaults = (): Partial => ({
ticklabelstandoff: 4,
});
-function layoutOf(layout: Partial = {}): Partial {
+function layoutOf(
+ layout: Partial = {},
+ data: Partial[] = [],
+): Partial {
const axes = Object.fromEntries(
Object.entries(layout)
.filter(([k]) => /^[xy]axis\d*$/.test(k))
@@ -40,7 +44,7 @@ function layoutOf(layout: Partial = {}): Partial {
return [k, { ...base, ...axis, title: { ...base.title, ...title } }];
}),
);
- return {
+ const composed: Partial = {
...LAYOUT,
xaxis: axisDefaults(),
yaxis: axisDefaults(),
@@ -49,14 +53,11 @@ function layoutOf(layout: Partial = {}): Partial {
legend: { ...LAYOUT.legend, ...layout.legend },
margin: { ...LAYOUT.margin, ...layout.margin },
};
+ return { ...composed, ...headroom(data, composed) };
}
function withRoom(data: Partial[]): Plotly.Data[] {
- return data.map((d) => {
- if (d.type === 'pie') return { automargin: true, ...d };
- if (d.type === 'bar') return { cliponaxis: false, ...d };
- return d;
- }) as Plotly.Data[];
+ return data.map((d) => (d.type === 'pie' ? { automargin: true, ...d } : d)) as Plotly.Data[];
}
const CONFIG: Partial = {
responsive: true,
@@ -77,7 +78,7 @@ export class Plot implements OnDestroy {
constructor() {
effect(() => {
const { data, layout } = this.figure();
- void plotly.react(this.host().nativeElement, withRoom(data), layoutOf(layout), CONFIG);
+ void plotly.react(this.host().nativeElement, withRoom(data), layoutOf(layout, data), CONFIG);
});
}