diff --git a/src/app/indicadores/figures.spec.ts b/src/app/indicadores/figures.spec.ts index 2096b50..fd56f51 100644 --- a/src/app/indicadores/figures.spec.ts +++ b/src/app/indicadores/figures.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { logAxis } from './figures'; +import { bandRuns, logAxis } from './figures'; describe('logAxis', () => { 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: [] }); }); }); + +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], + ]); + }); +}); diff --git a/src/app/indicadores/figures.ts b/src/app/indicadores/figures.ts index b1dcbe9..825cc7a 100644 --- a/src/app/indicadores/figures.ts +++ b/src/app/indicadores/figures.ts @@ -544,23 +544,24 @@ export function historyFigure( } const k = panel.kind === 'percent' ? 100 : 1; const at = (v?: number) => (v === undefined ? null : v * k); - const band = [ - ...series.map((s) => at(s.r?.high)), - ...[...series].reverse().map((s) => at(s.r?.low)), - ]; + const highs = series.map((s) => at(s.r?.high)); + const lows = series.map((s) => at(s.r?.low)); const scores = series.map((s) => at(s.r?.score)); + const bandName = panel.kind === 'percent' ? 'Intervalo de confianza' : 'Primer a tercer cuartil'; return { data: [ - { - x: [...x, ...[...x].reverse()], - y: band, - mode: 'lines', - fill: 'toself', + ...bandRuns(highs, lows).map((run, i) => ({ + x: [...run.map((j) => x[j]), ...[...run].reverse().map((j) => x[j])], + y: [...run.map((j) => highs[j]), ...[...run].reverse().map((j) => lows[j])], + mode: 'lines' as const, + fill: 'toself' as const, fillcolor: line + '33', line: { width: 0 }, - name: panel.kind === 'percent' ? 'Intervalo de confianza' : 'Primer a tercer cuartil', - hoverinfo: 'skip', - }, + name: bandName, + legendgroup: 'band', + showlegend: i === 0, + hoverinfo: 'skip' as const, + })), { x, y: scores, @@ -570,10 +571,28 @@ export function historyFigure( 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 { const base = countBars(r, catalog, axis, period); const cells = r.strata['sex-category'] ?? {};