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.
This commit is contained in:
2026-12-01 17:15:35 -06:00
parent 8ca3b053bd
commit 5cd601651c
3 changed files with 20 additions and 34 deletions
+13 -20
View File
@@ -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<number | null | undefined>) {
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<number | null | undefined>) {
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'] ?? {})))];
+3 -7
View File
@@ -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', () => {
+4 -7
View File
@@ -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] },
},
};
}