History band is one filled shape per run of periods with both bounds, so gaps no longer split it into triangles

This commit is contained in:
2026-12-03 17:40:54 -06:00
parent 599e239cd0
commit 2da269a6a8
2 changed files with 42 additions and 14 deletions
+10 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'; import { describe, expect, it } from 'vitest';
import { logAxis } from './figures'; import { bandRuns, logAxis } from './figures';
describe('logAxis', () => { describe('logAxis', () => {
it('uses the 1-2-3-5-7 sequence across decades with decimal labels', () => { it('uses the 1-2-3-5-7 sequence across decades with decimal labels', () => {
@@ -42,3 +42,12 @@ describe('logAxis', () => {
expect(logAxis([])).toMatchObject({ tickvals: [], ticktext: [] }); expect(logAxis([])).toMatchObject({ tickvals: [], ticktext: [] });
}); });
}); });
describe('bandRuns', () => {
it('splits the band into runs of periods that have both bounds', () => {
expect(bandRuns([null, 60, 70, null, 80, 90], [null, 10, 20, 30, null, 40])).toEqual([
[1, 2],
[5],
]);
});
});
+32 -13
View File
@@ -544,23 +544,24 @@ export function historyFigure(
} }
const k = panel.kind === 'percent' ? 100 : 1; const k = panel.kind === 'percent' ? 100 : 1;
const at = (v?: number) => (v === undefined ? null : v * k); const at = (v?: number) => (v === undefined ? null : v * k);
const band = [ const highs = series.map((s) => at(s.r?.high));
...series.map((s) => at(s.r?.high)), const lows = series.map((s) => at(s.r?.low));
...[...series].reverse().map((s) => at(s.r?.low)),
];
const scores = series.map((s) => at(s.r?.score)); const scores = series.map((s) => at(s.r?.score));
const bandName = panel.kind === 'percent' ? 'Intervalo de confianza' : 'Primer a tercer cuartil';
return { return {
data: [ data: [
{ ...bandRuns(highs, lows).map((run, i) => ({
x: [...x, ...[...x].reverse()], x: [...run.map((j) => x[j]), ...[...run].reverse().map((j) => x[j])],
y: band, y: [...run.map((j) => highs[j]), ...[...run].reverse().map((j) => lows[j])],
mode: 'lines', mode: 'lines' as const,
fill: 'toself', fill: 'toself' as const,
fillcolor: line + '33', fillcolor: line + '33',
line: { width: 0 }, line: { width: 0 },
name: panel.kind === 'percent' ? 'Intervalo de confianza' : 'Primer a tercer cuartil', name: bandName,
hoverinfo: 'skip', legendgroup: 'band',
}, showlegend: i === 0,
hoverinfo: 'skip' as const,
})),
{ {
x, x,
y: scores, y: scores,
@@ -570,10 +571,28 @@ export function historyFigure(
marker: { color: line }, marker: { color: line },
}, },
], ],
layout: layout(panel.kind === 'percent' ? 'Porcentaje' : 'Días', [...band, ...scores]), layout: layout(panel.kind === 'percent' ? 'Porcentaje' : 'Días', [
...highs,
...lows,
...scores,
]),
}; };
} }
export function bandRuns(highs: (number | null)[], lows: (number | null)[]): number[][] {
const runs: number[][] = [];
let run: number[] = [];
highs.forEach((high, i) => {
if (high != null && lows[i] != null) run.push(i);
else if (run.length) {
runs.push(run);
run = [];
}
});
if (run.length) runs.push(run);
return runs;
}
export function sexCountBars(r: Reading, catalog: Catalog, axis: string, period: string): Figure { export function sexCountBars(r: Reading, catalog: Catalog, axis: string, period: string): Figure {
const base = countBars(r, catalog, axis, period); const base = countBars(r, catalog, axis, period);
const cells = r.strata['sex-category'] ?? {}; const cells = r.strata['sex-category'] ?? {};