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:
@@ -334,7 +334,11 @@ export function countBars(r: Reading, catalog: Catalog, axis: string, period: st
|
|||||||
],
|
],
|
||||||
layout: {
|
layout: {
|
||||||
showlegend: false,
|
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 },
|
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>) {
|
export function valueAxis(values: Iterable<number | null | undefined>) {
|
||||||
let min = Infinity;
|
const found = [...values].filter((v): v is number => v != null && Number.isFinite(v));
|
||||||
let max = -Infinity;
|
const counts = found.every(Number.isInteger);
|
||||||
for (const value of values) {
|
const narrow = found.length > 0 && Math.max(...found) - Math.min(...found) <= 5;
|
||||||
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;
|
|
||||||
return {
|
return {
|
||||||
tickmode: 'linear' as const,
|
type: 'linear' as const,
|
||||||
tick0: 0,
|
tickformat: counts ? 'd' : '.1~f',
|
||||||
dtick: step * magnitude,
|
...(counts && narrow ? { dtick: 1 } : {}),
|
||||||
tickformat: '.0f',
|
|
||||||
...(Math.ceil(min) > Math.floor(max) && min <= max
|
|
||||||
? { range: [Math.floor(min), Math.ceil(max)] as [number, number] }
|
|
||||||
: {}),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -441,7 +434,7 @@ export function historyFigure(
|
|||||||
const x = series.map((s) => s.label);
|
const x = series.map((s) => s.label);
|
||||||
const layout = (y: string, values: (number | null)[]) => ({
|
const layout = (y: string, values: (number | null)[]) => ({
|
||||||
xaxis: { title: { text: xTitle }, type: 'category' as const },
|
xaxis: { title: { text: xTitle }, type: 'category' as const },
|
||||||
yaxis: { title: { text: y }, ...wholeNumberAxis(values) },
|
yaxis: { title: { text: y }, ...valueAxis(values) },
|
||||||
});
|
});
|
||||||
if (panel.kind === 'stack') {
|
if (panel.kind === 'stack') {
|
||||||
const found = [...new Set(series.flatMap((s) => Object.keys(s.r?.strata['category'] ?? {})))];
|
const found = [...new Set(series.flatMap((s) => Object.keys(s.r?.strata['category'] ?? {})))];
|
||||||
|
|||||||
@@ -154,13 +154,9 @@ describe('TEA indicators', () => {
|
|||||||
'Mes',
|
'Mes',
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(figure(1).layout?.yaxis).toMatchObject({
|
expect(figure(1).layout?.yaxis).toMatchObject({ type: 'linear', tickformat: 'd', dtick: 1 });
|
||||||
tickmode: 'linear',
|
expect(figure(100).layout?.yaxis).toMatchObject({ type: 'linear', tickformat: 'd' });
|
||||||
tick0: 0,
|
expect(figure(100).layout?.yaxis).not.toHaveProperty('dtick');
|
||||||
dtick: 1,
|
|
||||||
tickformat: '.0f',
|
|
||||||
});
|
|
||||||
expect(figure(100).layout?.yaxis?.dtick).toBe(50);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('builds the category and group rings from the supplied counts', () => {
|
it('builds the category and group rings from the supplied counts', () => {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { banded } from '../gobernanza/figures';
|
import { banded } from '../gobernanza/figures';
|
||||||
import { wholeNumberAxis } from '../indicadores/figures';
|
import { valueAxis } from '../indicadores/figures';
|
||||||
import { Figure, PALETTE } from '../plot';
|
import { Figure, PALETTE } from '../plot';
|
||||||
import { Metric, Pair, Site, Study, TeaStep, zoneLabel } from './tea-data';
|
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)' } },
|
xaxis: { title: { text: 'Edad (meses)' } },
|
||||||
yaxis: {
|
yaxis: {
|
||||||
title: { text: metric.unit ?? 'Valor' },
|
title: { text: metric.unit ?? 'Valor' },
|
||||||
...wholeNumberAxis([
|
...valueAxis([metric.value, ...rows.flatMap(({ p5, median, p95 }) => [p5, median, p95])]),
|
||||||
metric.value,
|
|
||||||
...rows.flatMap(({ p5, median, p95 }) => [p5, median, p95]),
|
|
||||||
]),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -333,7 +330,7 @@ export function zBars(eeg: Study): SingleTrace {
|
|||||||
layout: {
|
layout: {
|
||||||
xaxis: {
|
xaxis: {
|
||||||
title: { text: 'Puntuación z' },
|
title: { text: 'Puntuación z' },
|
||||||
...wholeNumberAxis([-limit, limit]),
|
...valueAxis([-limit, limit]),
|
||||||
range: [-limit, limit],
|
range: [-limit, limit],
|
||||||
},
|
},
|
||||||
yaxis: { autorange: 'reversed', automargin: true },
|
yaxis: { autorange: 'reversed', automargin: true },
|
||||||
@@ -394,7 +391,7 @@ export function probabilityCurve(
|
|||||||
data,
|
data,
|
||||||
layout: {
|
layout: {
|
||||||
xaxis: { title: { text: 'Puntuación z' } },
|
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] },
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user