F1: Sankey laid out by Plotly's own columns, with no hand-set x/y

Root cause of the crossing bands and overlap: with explicit node x/y Plotly keeps the nodes where they are set but orders the links leaving a node on its own, so a stopped band could leave the top of a node while its target sat at the bottom (bands crossed), zero-flow nodes stayed in the figure and tiny nodes landed on one another, and long one-line labels ran into the next column's labels. Now the stopped nodes are plain sinks that Plotly justifies to the right column, ordered neuropediátrica, psicológica, M-CHAT from top to bottom so bands never cross; nodes without flow are dropped; labels break in two lines; the padding keeps two-line labels apart. The card spans the whole row. Checked by screenshots of the live page across monthly and semester periods.
This commit is contained in:
2026-12-01 14:36:30 -06:00
parent 2404a3a66b
commit 4118b050b8
3 changed files with 39 additions and 29 deletions
+34 -24
View File
@@ -512,8 +512,7 @@ export function sexCountBars(r: Reading, catalog: Catalog, axis: string, period:
};
}
const SANKEY_X = [0.001, 0.25, 0.5, 0.75, 0.999, 0.999, 0.999];
const SANKEY_Y = [0.2, 0.2, 0.2, 0.2, 0.95, 0.7, 0.45];
const STOPPED = '#888888';
export function teaSankey(r: Reading): Figure {
const [mchat, psychology, neuro, eeg] = [
@@ -522,42 +521,53 @@ export function teaSankey(r: Reading): Figure {
'neuropediatrics',
'eeg',
].map((code) => counted(r, code));
const labels = [
'M-CHAT',
'Evaluación psicológica',
'Evaluación neuropediátrica',
'EEG de buena calidad',
'Se quedó en M-CHAT',
'Se quedó en evaluación psicológica',
'Se quedó en evaluación neuropediátrica',
const nodes = [
{ label: 'M-CHAT', color: '#B8433A', value: mchat + psychology + neuro + eeg },
{ label: 'Evaluación<br>psicológica', color: '#D0B778', value: psychology + neuro + eeg },
{ label: 'Evaluación<br>neuropediátrica', color: '#7184A2', value: neuro + eeg },
{ label: 'EEG de<br>buena calidad', color: OLIVE, value: eeg },
{ label: 'Se quedó en evaluación<br>neuropediátrica', color: STOPPED, value: neuro },
{ label: 'Se quedó en evaluación<br>psicológica', color: STOPPED, value: psychology },
{ label: 'Se quedó en M-CHAT', color: STOPPED, value: mchat },
];
const colors = ['#B8433A', '#D0B778', '#7184A2', OLIVE, '#888888', '#888888', '#888888'];
const links = [
{ source: 0, target: 1, value: psychology + neuro + eeg },
{ source: 0, target: 4, value: mchat },
{ source: 1, target: 2, value: neuro + eeg },
{ source: 1, target: 5, value: psychology },
{ source: 2, target: 3, value: eeg },
{ source: 2, target: 6, value: neuro },
].filter(({ value }) => value > 0);
const shown = nodes.flatMap((node, index) => (node.value > 0 ? [index] : []));
const position = new Map(shown.map((index, place) => [index, place]));
const flows: [number, number][] = [
[0, 1],
[0, 6],
[1, 2],
[1, 5],
[2, 3],
[2, 4],
];
const links = flows.flatMap(([source, target]) =>
nodes[target].value > 0
? [{ source: position.get(source)!, target: position.get(target)!, index: target }]
: [],
);
const hovertemplate = '%{label}<br>%{value} pacientes<extra></extra>';
return {
data: [
{
type: 'sankey',
arrangement: 'fixed',
node: { label: labels, color: colors, x: SANKEY_X, y: SANKEY_Y, pad: 24, hovertemplate },
node: {
label: shown.map((index) => nodes[index].label),
color: shown.map((index) => nodes[index].color),
pad: 36,
hovertemplate,
},
link: {
source: links.map(({ source }) => source),
target: links.map(({ target }) => target),
value: links.map(({ value }) => value),
color: links.map(({ target }) => rgba(colors[target], 0.45)),
label: links.map(({ target }) => labels[target]),
value: links.map(({ index }) => nodes[index].value),
color: links.map(({ index }) => rgba(nodes[index].color, 0.45)),
label: links.map(({ index }) => nodes[index].label.replace('<br>', ' ')),
hovertemplate,
},
} as Partial<Plotly.Data>,
],
layout: { margin: { l: 20, r: 20, t: 24, b: 24 } },
layout: { font: { size: 12 }, margin: { l: 20, r: 20, t: 24, b: 24 } },
};
}
+4 -4
View File
@@ -129,10 +129,6 @@ h2.figure-card-title {
}
}
.figure[data-figure='sankey'] {
grid-column: 1 / -1;
}
.figure h3 {
font-size: 16px;
font-weight: 700;
@@ -234,6 +230,10 @@ h2.box-subtitle {
grid-template-columns: 1fr;
}
.grid-figures > [data-figure='sankey'] {
grid-column: 1 / -1;
}
@media (min-width: 1100px) {
.grid-figures {
grid-template-columns: 1fr 1fr;
+1 -1
View File
@@ -369,7 +369,7 @@ describe('TEA clinical figures', () => {
});
expect(figure.data[0]).toMatchObject({
type: 'sankey',
link: { source: [0, 0, 1, 2, 2], target: [1, 4, 2, 3, 6], value: [7, 2, 7, 4, 3] },
link: { source: [0, 0, 1, 2, 2], target: [1, 5, 2, 3, 4], value: [7, 2, 7, 4, 3] },
});
});