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'] ?? {})))];