Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/7957_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix `node.x` and `node.y` scaling for vertical `sankey` traces [[#7957](https://github.com/plotly/plotly.js/pull/7957)], with thanks to @waterWang for the contribution!
14 changes: 12 additions & 2 deletions src/traces/sankey/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,22 @@ var attrs = (module.exports = overrideAll(
x: {
valType: 'data_array',
dflt: [],
description: 'The normalized horizontal position of the node.'
description: [
'The normalized position of the node center along the flow axis, measured from the source end.',
'For `orientation` *h* the flow axis is horizontal.',
'For `orientation` *v* the flow axis is vertical.',
'With `direction` *reversed* the source end is the right or bottom edge.'
].join(' ')
},
y: {
valType: 'data_array',
dflt: [],
description: 'The normalized vertical position of the node.'
description: [
'The normalized position of the node center across the flow axis.',
'For `orientation` *h* the value counts down from the top edge.',
'For `orientation` *v* the value counts from the left edge.',
'The value does not depend on `direction`.'
].join(' ')
},
color: {
valType: 'color',
Expand Down
15 changes: 12 additions & 3 deletions src/traces/sankey/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ function sankeyModel(layout, d, traceIndex) {

var p = forcedNode.pointNumber;
if (trace.node.x[p] && trace.node.y[p]) {
var pos = [trace.node.x[p] * width, trace.node.y[p] * height];
const pos = horizontal
? [trace.node.x[p] * width, trace.node.y[p] * height]
: [trace.node.x[p] * height, trace.node.y[p] * width];
forcedNode.x0 = pos[0] - nodeThickness / 2;
forcedNode.x1 = pos[0] + nodeThickness / 2;

Expand Down Expand Up @@ -838,8 +840,15 @@ function persistFinalNodePositions(d, gd) {
const y = [...d.trace.node.y];
for (const node of d.graph.nodes) {
if (node.partOfGroup) continue;
x[node.pointNumber] = (node.x0 + node.x1) / 2 / d.figure.width;
y[node.pointNumber] = (node.y0 + node.y1) / 2 / d.figure.height;
const nodeX = (node.x0 + node.x1) / 2;
const nodeY = (node.y0 + node.y1) / 2;
if (d.horizontal) {
x[node.pointNumber] = nodeX / d.figure.width;
y[node.pointNumber] = nodeY / d.figure.height;
} else {
x[node.pointNumber] = nodeX / d.figure.height;
y[node.pointNumber] = nodeY / d.figure.width;
}
Comment thread
camdecoster marked this conversation as resolved.
Resolved
}
Registry.call('_guiRestyle', gd, {
'node.x': [x],
Expand Down
4 changes: 2 additions & 2 deletions src/types/generated/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7078,12 +7078,12 @@ export interface SankeyData {
*/
thickness?: number;
/**
* The normalized horizontal position of the node.
* The normalized position of the node center along the flow axis, measured from the source end. For `orientation` *h* the flow axis is horizontal. For `orientation` *v* the flow axis is vertical. With `direction` *reversed* the source end is the right or bottom edge.
* @default []
*/
x?: Datum[] | Datum[][] | TypedArray;
/**
* The normalized vertical position of the node.
* The normalized position of the node center across the flow axis. For `orientation` *h* the value counts down from the top edge. For `orientation` *v* the value counts from the left edge. The value does not depend on `direction`.
* @default []
*/
y?: Datum[] | Datum[][] | TypedArray;
Expand Down
30 changes: 30 additions & 0 deletions test/jasmine/tests/sankey_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,36 @@ describe('sankey tests', function () {
.then(done, done.fail);
});

it('scales node.x and node.y by the transposed extents for orientation v', async () => {
const nodeX = [0.2, 0.8];
const nodeY = [0.5, 0.5];
await Plotly.newPlot(
gd,
[
{
type: 'sankey',
orientation: 'v',
arrangement: 'freeform',
node: { x: nodeX, y: nodeY },
link: { source: [0], target: [1], value: [1] }
}
],
{ width: 900, height: 300, margin: { l: 0, r: 0, t: 0, b: 0 } }
);

// For 'v', the layout space carries the flow along x and spans [height, width]
d3SelectAll('.sankey .sankey-node').each((d) => {
const i = d.node.pointNumber;
expect((d.node.x0 + d.node.x1) / 2).toBeCloseTo(nodeX[i] * 300, 3);
expect((d.node.y0 + d.node.y1) / 2).toBeCloseTo(nodeY[i] * 900, 3);
});

// A drag writes back every node, so the node that stays put must keep its input coordinates
await drag({ node: document.getElementsByClassName('sankey-node').item(0), dpos: [0, 20] });
expect(gd._fullData[0].node.x[1]).toBeCloseTo(nodeX[1], 3);
expect(gd._fullData[0].node.y[1]).toBeCloseTo(nodeY[1], 3);
});

it('Plotly.deleteTraces with two traces removes the deleted plot', function (done) {
var mockCopy = Lib.extendDeep({}, mock);
var mockCopy2 = Lib.extendDeep({}, mockDark);
Expand Down
4 changes: 2 additions & 2 deletions test/plot-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -52459,13 +52459,13 @@
"valType": "number"
},
"x": {
"description": "The normalized horizontal position of the node.",
"description": "The normalized position of the node center along the flow axis, measured from the source end. For `orientation` *h* the flow axis is horizontal. For `orientation` *v* the flow axis is vertical. With `direction` *reversed* the source end is the right or bottom edge.",
"dflt": [],
"editType": "calc",
"valType": "data_array"
},
"y": {
"description": "The normalized vertical position of the node.",
"description": "The normalized position of the node center across the flow axis. For `orientation` *h* the value counts down from the top edge. For `orientation` *v* the value counts from the left edge. The value does not depend on `direction`.",
"dflt": [],
"editType": "calc",
"valType": "data_array"
Expand Down
Loading