From ebb8db00176671bf4d440a3dd12e366da9937bfb Mon Sep 17 00:00:00 2001 From: alberto Date: Thu, 3 Dec 2026 17:12:54 -0600 Subject: [PATCH] Headroom reads bar traces through a local shape; Plotly types export no PlotData here --- src/app/plot-headroom.ts | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/app/plot-headroom.ts b/src/app/plot-headroom.ts index b012df0..aa33a2b 100644 --- a/src/app/plot-headroom.ts +++ b/src/app/plot-headroom.ts @@ -4,12 +4,20 @@ export const HEADROOM = 1.15; const AXES = ['xaxis', 'yaxis'] as const; +interface BarTrace { + type?: string; + textposition?: unknown; + orientation?: string; + x?: unknown; + y?: unknown; +} + export function headroom( data: Partial[], layout: Partial, ): Partial { const labelled: Record<(typeof AXES)[number], number[]> = { xaxis: [], yaxis: [] }; - for (const trace of data as Partial[]) { + for (const trace of data as BarTrace[]) { if (trace.type !== 'bar' || trace.textposition !== 'outside') continue; const horizontal = trace.orientation === 'h'; const values = ((horizontal ? trace.x : trace.y) ?? []) as unknown[]; @@ -17,15 +25,15 @@ export function headroom( ...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] }]]; - }), - ); + const room: Partial = {}; + for (const key of AXES) { + const values = labelled[key]; + const axis = layout[key]; + if (!values.length || axis?.range) continue; + const low = Math.min(0, ...values); + const high = Math.max(0, ...values); + if (low === high) continue; + room[key] = { ...axis, range: [low * HEADROOM, high * HEADROOM] }; + } + return room; }