From 5cd601651c53b926f41c57c37680fd7354663d16 Mon Sep 17 00:00:00 2001 From: alberto Date: Tue, 1 Dec 2026 17:15:35 -0600 Subject: [PATCH] Count axes are linear with whole-number or one-decimal tick labels from one shared builder The EEG quality chart's count axis was typed as a category axis, which put 0 on the right; every count and value axis now takes its type and tick format from valueAxis: linear, whole numbers for counts (one step when the range is narrow), one decimal otherwise, with Plotly choosing the number of ticks. --- src/app/indicadores/figures.ts | 33 +++++++++------------- src/app/indicadores/tea-indicators.spec.ts | 10 ++----- src/app/paciente/tea-figures.ts | 11 +++----- 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/app/indicadores/figures.ts b/src/app/indicadores/figures.ts index 1ba212f..208d068 100644 --- a/src/app/indicadores/figures.ts +++ b/src/app/indicadores/figures.ts @@ -334,7 +334,11 @@ export function countBars(r: Reading, catalog: Catalog, axis: string, period: st ], layout: { showlegend: false, - xaxis: { title: { text: axis }, rangemode: 'tozero' }, + xaxis: { + title: { text: axis }, + rangemode: 'tozero', + ...valueAxis(catalog.categories.map((c) => counted(r, c.code))), + }, yaxis: { type: 'category', autorange: 'reversed', automargin: true }, }, }; @@ -410,25 +414,14 @@ export function ageShare(r: Reading, axis: string): Figure { }; } -export function wholeNumberAxis(values: Iterable) { - let min = Infinity; - let max = -Infinity; - for (const value of values) { - if (value == null || !Number.isFinite(value)) continue; - min = Math.min(min, value); - max = Math.max(max, value); - } - const target = Math.max(1, (max - min) / 4); - const magnitude = 10 ** Math.floor(Math.log10(target)); - const step = [1, 2, 5, 10].find((step) => step * magnitude >= target) ?? 1; +export function valueAxis(values: Iterable) { + const found = [...values].filter((v): v is number => v != null && Number.isFinite(v)); + const counts = found.every(Number.isInteger); + const narrow = found.length > 0 && Math.max(...found) - Math.min(...found) <= 5; return { - tickmode: 'linear' as const, - tick0: 0, - dtick: step * magnitude, - tickformat: '.0f', - ...(Math.ceil(min) > Math.floor(max) && min <= max - ? { range: [Math.floor(min), Math.ceil(max)] as [number, number] } - : {}), + type: 'linear' as const, + tickformat: counts ? 'd' : '.1~f', + ...(counts && narrow ? { dtick: 1 } : {}), }; } @@ -441,7 +434,7 @@ export function historyFigure( const x = series.map((s) => s.label); const layout = (y: string, values: (number | null)[]) => ({ xaxis: { title: { text: xTitle }, type: 'category' as const }, - yaxis: { title: { text: y }, ...wholeNumberAxis(values) }, + yaxis: { title: { text: y }, ...valueAxis(values) }, }); if (panel.kind === 'stack') { const found = [...new Set(series.flatMap((s) => Object.keys(s.r?.strata['category'] ?? {})))]; diff --git a/src/app/indicadores/tea-indicators.spec.ts b/src/app/indicadores/tea-indicators.spec.ts index 228deeb..ca44285 100644 --- a/src/app/indicadores/tea-indicators.spec.ts +++ b/src/app/indicadores/tea-indicators.spec.ts @@ -154,13 +154,9 @@ describe('TEA indicators', () => { 'Mes', ); - expect(figure(1).layout?.yaxis).toMatchObject({ - tickmode: 'linear', - tick0: 0, - dtick: 1, - tickformat: '.0f', - }); - expect(figure(100).layout?.yaxis?.dtick).toBe(50); + expect(figure(1).layout?.yaxis).toMatchObject({ type: 'linear', tickformat: 'd', dtick: 1 }); + expect(figure(100).layout?.yaxis).toMatchObject({ type: 'linear', tickformat: 'd' }); + expect(figure(100).layout?.yaxis).not.toHaveProperty('dtick'); }); it('builds the category and group rings from the supplied counts', () => { diff --git a/src/app/paciente/tea-figures.ts b/src/app/paciente/tea-figures.ts index 817552e..b375751 100644 --- a/src/app/paciente/tea-figures.ts +++ b/src/app/paciente/tea-figures.ts @@ -1,5 +1,5 @@ import { banded } from '../gobernanza/figures'; -import { wholeNumberAxis } from '../indicadores/figures'; +import { valueAxis } from '../indicadores/figures'; import { Figure, PALETTE } from '../plot'; import { Metric, Pair, Site, Study, TeaStep, zoneLabel } from './tea-data'; @@ -252,10 +252,7 @@ export function ageNorm(rows: Centile[], metric: Metric, age: number): Figure { xaxis: { title: { text: 'Edad (meses)' } }, yaxis: { title: { text: metric.unit ?? 'Valor' }, - ...wholeNumberAxis([ - metric.value, - ...rows.flatMap(({ p5, median, p95 }) => [p5, median, p95]), - ]), + ...valueAxis([metric.value, ...rows.flatMap(({ p5, median, p95 }) => [p5, median, p95])]), }, }, }; @@ -333,7 +330,7 @@ export function zBars(eeg: Study): SingleTrace { layout: { xaxis: { title: { text: 'Puntuación z' }, - ...wholeNumberAxis([-limit, limit]), + ...valueAxis([-limit, limit]), range: [-limit, limit], }, yaxis: { autorange: 'reversed', automargin: true }, @@ -394,7 +391,7 @@ export function probabilityCurve( data, layout: { xaxis: { title: { text: 'Puntuación z' } }, - yaxis: { title: { text: 'Probabilidad (%)' }, ...wholeNumberAxis([0, 100]), range: [0, 100] }, + yaxis: { title: { text: 'Probabilidad (%)' }, ...valueAxis([0, 100]), range: [0, 100] }, }, }; }