0c2e05d077
- 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.
89 lines
2.7 KiB
TypeScript
89 lines
2.7 KiB
TypeScript
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<Plotly.Data>[]; layout?: Partial<Plotly.Layout> };
|
|
|
|
export const PALETTE = [
|
|
'#A8C243',
|
|
'#42C29C',
|
|
'#C24269',
|
|
'#9C42C2',
|
|
'#4269C2',
|
|
'#C29C42',
|
|
'#7A7A7A',
|
|
];
|
|
|
|
const LAYOUT: Partial<Plotly.Layout> = {
|
|
font: { family: 'Roboto, Arial, sans-serif', size: 14, color: '#17324d' },
|
|
colorway: PALETTE,
|
|
margin: { t: 24, r: 16, b: 48, l: 56 },
|
|
paper_bgcolor: 'rgba(0,0,0,0)',
|
|
plot_bgcolor: 'rgba(0,0,0,0)',
|
|
legend: { orientation: 'h', x: 0, xanchor: 'left', y: 1.02, yanchor: 'bottom' },
|
|
hoverlabel: { font: { family: 'Roboto, Arial, sans-serif', size: 13 } },
|
|
};
|
|
const axisDefaults = (): Partial<Plotly.LayoutAxis> => ({
|
|
automargin: true,
|
|
title: { standoff: 12 },
|
|
ticklabelstandoff: 4,
|
|
});
|
|
|
|
function layoutOf(
|
|
layout: Partial<Plotly.Layout> = {},
|
|
data: Partial<Plotly.Data>[] = [],
|
|
): Partial<Plotly.Layout> {
|
|
const axes = Object.fromEntries(
|
|
Object.entries(layout)
|
|
.filter(([k]) => /^[xy]axis\d*$/.test(k))
|
|
.map(([k, v]) => {
|
|
const axis = (v ?? {}) as Partial<Plotly.LayoutAxis>;
|
|
const title = typeof axis.title === 'string' ? { text: axis.title } : axis.title;
|
|
const base = axisDefaults();
|
|
return [k, { ...base, ...axis, title: { ...base.title, ...title } }];
|
|
}),
|
|
);
|
|
const composed: Partial<Plotly.Layout> = {
|
|
...LAYOUT,
|
|
xaxis: axisDefaults(),
|
|
yaxis: axisDefaults(),
|
|
...layout,
|
|
...axes,
|
|
legend: { ...LAYOUT.legend, ...layout.legend },
|
|
margin: { ...LAYOUT.margin, ...layout.margin },
|
|
};
|
|
return { ...composed, ...headroom(data, composed) };
|
|
}
|
|
|
|
function withRoom(data: Partial<Plotly.Data>[]): Plotly.Data[] {
|
|
return data.map((d) => (d.type === 'pie' ? { automargin: true, ...d } : d)) as Plotly.Data[];
|
|
}
|
|
const CONFIG: Partial<Plotly.Config> = {
|
|
responsive: true,
|
|
displaylogo: false,
|
|
modeBarButtonsToRemove: ['lasso2d', 'select2d'],
|
|
};
|
|
|
|
@Component({
|
|
selector: 'app-plot',
|
|
template: '<div #host class="plot" [style.height.px]="height()"></div>',
|
|
styles: ':host { display: block; } .plot { width: 100%; }',
|
|
})
|
|
export class Plot implements OnDestroy {
|
|
readonly figure = input.required<Figure>();
|
|
readonly height = input(320);
|
|
private readonly host = viewChild.required<ElementRef<HTMLDivElement>>('host');
|
|
|
|
constructor() {
|
|
effect(() => {
|
|
const { data, layout } = this.figure();
|
|
void plotly.react(this.host().nativeElement, withRoom(data), layoutOf(layout, data), CONFIG);
|
|
});
|
|
}
|
|
|
|
ngOnDestroy(): void {
|
|
plotly.purge(this.host().nativeElement);
|
|
}
|
|
}
|