F6: bar labels fit inside the plot; clinical chart heights as before
- Cause: the percent axis range of 0-110 was only reached by the three percent bar builders (ageStack, ageShare, teaAlterations); every other bar chart with outside labels (count bars, sex bars) ran on autorange, where Plotly reserves no room for outside text, and the labels were only kept visible by cliponaxis false, so they spilled over the plot edge into the legend area. The headroom lived in each builder instead of in the shared plot code. - Headroom is now computed once in the shared plot builder (plot-headroom.ts, used by layoutOf): for bar traces with outside labels, the value axis (y for vertical, x for horizontal bars) gets a range of 15 % over the maximum (and under the minimum if negative) unless the figure sets its own range. Percent axes keep their explicit 0-110 range with ticks every 20, ending at 100. - cliponaxis false for bars is removed: with the headroom the labels are inside the plot area. - Indicadores clinicos: route chart 300, grid figures 320, figure-row figures 360 again; the clinicalHeight constant is gone. - Logic spec on the produced axis ranges: plot-headroom.spec.ts.
This commit is contained in:
@@ -90,7 +90,7 @@
|
|||||||
@if (reports.isLoading()) {
|
@if (reports.isLoading()) {
|
||||||
<app-loading what="los tiempos de la ruta" [figure]="true" />
|
<app-loading what="los tiempos de la ruta" [figure]="true" />
|
||||||
} @else if (routeOf(section); as fig) {
|
} @else if (routeOf(section); as fig) {
|
||||||
<app-plot [figure]="fig" [height]="clinicalHeight" />
|
<app-plot [figure]="fig" [height]="300" />
|
||||||
} @else {
|
} @else {
|
||||||
<p class="state">Sin tiempos publicados en el periodo.</p>
|
<p class="state">Sin tiempos publicados en el periodo.</p>
|
||||||
}
|
}
|
||||||
@@ -147,7 +147,7 @@
|
|||||||
@if (reports.isLoading()) {
|
@if (reports.isLoading()) {
|
||||||
<app-loading what="la figura" [figure]="true" />
|
<app-loading what="la figura" [figure]="true" />
|
||||||
} @else if (figureOf(f); as fig) {
|
} @else if (figureOf(f); as fig) {
|
||||||
<app-plot [figure]="fig" [height]="clinicalHeight" />
|
<app-plot [figure]="fig" [height]="320" />
|
||||||
} @else {
|
} @else {
|
||||||
<div class="empty-figure">Sin datos publicados</div>
|
<div class="empty-figure">Sin datos publicados</div>
|
||||||
}
|
}
|
||||||
@@ -219,7 +219,7 @@
|
|||||||
@if (reports.isLoading()) {
|
@if (reports.isLoading()) {
|
||||||
<app-loading what="la figura" [figure]="true" />
|
<app-loading what="la figura" [figure]="true" />
|
||||||
} @else if (figureOf(f); as fig) {
|
} @else if (figureOf(f); as fig) {
|
||||||
<app-plot [figure]="fig" [height]="clinicalHeight" />
|
<app-plot [figure]="fig" [height]="360" />
|
||||||
} @else {
|
} @else {
|
||||||
<p class="state">Sin datos en el periodo.</p>
|
<p class="state">Sin datos en el periodo.</p>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,7 +105,6 @@ export class TabIndicadores {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected readonly clinicalHeight = 430;
|
|
||||||
protected readonly pending = 'Indicador no publicado en el catálogo institucional.';
|
protected readonly pending = 'Indicador no publicado en el catálogo institucional.';
|
||||||
protected setType(type: PeriodType): void {
|
protected setType(type: PeriodType): void {
|
||||||
this.type.set(type);
|
this.type.set(type);
|
||||||
|
|||||||
@@ -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<string, unknown>) => [
|
||||||
|
{ 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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<Plotly.Data>[],
|
||||||
|
layout: Partial<Plotly.Layout>,
|
||||||
|
): Partial<Plotly.Layout> {
|
||||||
|
const labelled: Record<(typeof AXES)[number], number[]> = { xaxis: [], yaxis: [] };
|
||||||
|
for (const trace of data as Partial<Plotly.PlotData>[]) {
|
||||||
|
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] }]];
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
+9
-8
@@ -1,5 +1,6 @@
|
|||||||
import { Component, effect, ElementRef, input, OnDestroy, viewChild } from '@angular/core';
|
import { Component, effect, ElementRef, input, OnDestroy, viewChild } from '@angular/core';
|
||||||
import type * as Plotly from 'plotly.js';
|
import type * as Plotly from 'plotly.js';
|
||||||
|
import { headroom } from './plot-headroom';
|
||||||
import plotly from './plotly-bundle';
|
import plotly from './plotly-bundle';
|
||||||
|
|
||||||
export type Figure = { data: Partial<Plotly.Data>[]; layout?: Partial<Plotly.Layout> };
|
export type Figure = { data: Partial<Plotly.Data>[]; layout?: Partial<Plotly.Layout> };
|
||||||
@@ -29,7 +30,10 @@ const axisDefaults = (): Partial<Plotly.LayoutAxis> => ({
|
|||||||
ticklabelstandoff: 4,
|
ticklabelstandoff: 4,
|
||||||
});
|
});
|
||||||
|
|
||||||
function layoutOf(layout: Partial<Plotly.Layout> = {}): Partial<Plotly.Layout> {
|
function layoutOf(
|
||||||
|
layout: Partial<Plotly.Layout> = {},
|
||||||
|
data: Partial<Plotly.Data>[] = [],
|
||||||
|
): Partial<Plotly.Layout> {
|
||||||
const axes = Object.fromEntries(
|
const axes = Object.fromEntries(
|
||||||
Object.entries(layout)
|
Object.entries(layout)
|
||||||
.filter(([k]) => /^[xy]axis\d*$/.test(k))
|
.filter(([k]) => /^[xy]axis\d*$/.test(k))
|
||||||
@@ -40,7 +44,7 @@ function layoutOf(layout: Partial<Plotly.Layout> = {}): Partial<Plotly.Layout> {
|
|||||||
return [k, { ...base, ...axis, title: { ...base.title, ...title } }];
|
return [k, { ...base, ...axis, title: { ...base.title, ...title } }];
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
return {
|
const composed: Partial<Plotly.Layout> = {
|
||||||
...LAYOUT,
|
...LAYOUT,
|
||||||
xaxis: axisDefaults(),
|
xaxis: axisDefaults(),
|
||||||
yaxis: axisDefaults(),
|
yaxis: axisDefaults(),
|
||||||
@@ -49,14 +53,11 @@ function layoutOf(layout: Partial<Plotly.Layout> = {}): Partial<Plotly.Layout> {
|
|||||||
legend: { ...LAYOUT.legend, ...layout.legend },
|
legend: { ...LAYOUT.legend, ...layout.legend },
|
||||||
margin: { ...LAYOUT.margin, ...layout.margin },
|
margin: { ...LAYOUT.margin, ...layout.margin },
|
||||||
};
|
};
|
||||||
|
return { ...composed, ...headroom(data, composed) };
|
||||||
}
|
}
|
||||||
|
|
||||||
function withRoom(data: Partial<Plotly.Data>[]): Plotly.Data[] {
|
function withRoom(data: Partial<Plotly.Data>[]): Plotly.Data[] {
|
||||||
return data.map((d) => {
|
return data.map((d) => (d.type === 'pie' ? { automargin: true, ...d } : d)) as Plotly.Data[];
|
||||||
if (d.type === 'pie') return { automargin: true, ...d };
|
|
||||||
if (d.type === 'bar') return { cliponaxis: false, ...d };
|
|
||||||
return d;
|
|
||||||
}) as Plotly.Data[];
|
|
||||||
}
|
}
|
||||||
const CONFIG: Partial<Plotly.Config> = {
|
const CONFIG: Partial<Plotly.Config> = {
|
||||||
responsive: true,
|
responsive: true,
|
||||||
@@ -77,7 +78,7 @@ export class Plot implements OnDestroy {
|
|||||||
constructor() {
|
constructor() {
|
||||||
effect(() => {
|
effect(() => {
|
||||||
const { data, layout } = this.figure();
|
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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user