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:
@@ -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 STOPPED = '#888888';
|
||||||
const SANKEY_Y = [0.2, 0.2, 0.2, 0.2, 0.95, 0.7, 0.45];
|
|
||||||
|
|
||||||
export function teaSankey(r: Reading): Figure {
|
export function teaSankey(r: Reading): Figure {
|
||||||
const [mchat, psychology, neuro, eeg] = [
|
const [mchat, psychology, neuro, eeg] = [
|
||||||
@@ -522,42 +521,53 @@ export function teaSankey(r: Reading): Figure {
|
|||||||
'neuropediatrics',
|
'neuropediatrics',
|
||||||
'eeg',
|
'eeg',
|
||||||
].map((code) => counted(r, code));
|
].map((code) => counted(r, code));
|
||||||
const labels = [
|
const nodes = [
|
||||||
'M-CHAT',
|
{ label: 'M-CHAT', color: '#B8433A', value: mchat + psychology + neuro + eeg },
|
||||||
'Evaluación psicológica',
|
{ label: 'Evaluación<br>psicológica', color: '#D0B778', value: psychology + neuro + eeg },
|
||||||
'Evaluación neuropediátrica',
|
{ label: 'Evaluación<br>neuropediátrica', color: '#7184A2', value: neuro + eeg },
|
||||||
'EEG de buena calidad',
|
{ label: 'EEG de<br>buena calidad', color: OLIVE, value: eeg },
|
||||||
'Se quedó en M-CHAT',
|
{ label: 'Se quedó en evaluación<br>neuropediátrica', color: STOPPED, value: neuro },
|
||||||
'Se quedó en evaluación psicológica',
|
{ label: 'Se quedó en evaluación<br>psicológica', color: STOPPED, value: psychology },
|
||||||
'Se quedó en evaluación neuropediátrica',
|
{ label: 'Se quedó en M-CHAT', color: STOPPED, value: mchat },
|
||||||
];
|
];
|
||||||
const colors = ['#B8433A', '#D0B778', '#7184A2', OLIVE, '#888888', '#888888', '#888888'];
|
const shown = nodes.flatMap((node, index) => (node.value > 0 ? [index] : []));
|
||||||
const links = [
|
const position = new Map(shown.map((index, place) => [index, place]));
|
||||||
{ source: 0, target: 1, value: psychology + neuro + eeg },
|
const flows: [number, number][] = [
|
||||||
{ source: 0, target: 4, value: mchat },
|
[0, 1],
|
||||||
{ source: 1, target: 2, value: neuro + eeg },
|
[0, 6],
|
||||||
{ source: 1, target: 5, value: psychology },
|
[1, 2],
|
||||||
{ source: 2, target: 3, value: eeg },
|
[1, 5],
|
||||||
{ source: 2, target: 6, value: neuro },
|
[2, 3],
|
||||||
].filter(({ value }) => value > 0);
|
[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>';
|
const hovertemplate = '%{label}<br>%{value} pacientes<extra></extra>';
|
||||||
return {
|
return {
|
||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
type: 'sankey',
|
type: 'sankey',
|
||||||
arrangement: 'fixed',
|
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: {
|
link: {
|
||||||
source: links.map(({ source }) => source),
|
source: links.map(({ source }) => source),
|
||||||
target: links.map(({ target }) => target),
|
target: links.map(({ target }) => target),
|
||||||
value: links.map(({ value }) => value),
|
value: links.map(({ index }) => nodes[index].value),
|
||||||
color: links.map(({ target }) => rgba(colors[target], 0.45)),
|
color: links.map(({ index }) => rgba(nodes[index].color, 0.45)),
|
||||||
label: links.map(({ target }) => labels[target]),
|
label: links.map(({ index }) => nodes[index].label.replace('<br>', ' ')),
|
||||||
hovertemplate,
|
hovertemplate,
|
||||||
},
|
},
|
||||||
} as Partial<Plotly.Data>,
|
} 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 } },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -129,10 +129,6 @@ h2.figure-card-title {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.figure[data-figure='sankey'] {
|
|
||||||
grid-column: 1 / -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
.figure h3 {
|
.figure h3 {
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
font-weight: 700;
|
font-weight: 700;
|
||||||
@@ -234,6 +230,10 @@ h2.box-subtitle {
|
|||||||
grid-template-columns: 1fr;
|
grid-template-columns: 1fr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.grid-figures > [data-figure='sankey'] {
|
||||||
|
grid-column: 1 / -1;
|
||||||
|
}
|
||||||
|
|
||||||
@media (min-width: 1100px) {
|
@media (min-width: 1100px) {
|
||||||
.grid-figures {
|
.grid-figures {
|
||||||
grid-template-columns: 1fr 1fr;
|
grid-template-columns: 1fr 1fr;
|
||||||
|
|||||||
@@ -369,7 +369,7 @@ describe('TEA clinical figures', () => {
|
|||||||
});
|
});
|
||||||
expect(figure.data[0]).toMatchObject({
|
expect(figure.data[0]).toMatchObject({
|
||||||
type: 'sankey',
|
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] },
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user