diff --git a/draftlogs/7957_fix.md b/draftlogs/7957_fix.md new file mode 100644 index 00000000000..ecf8da62315 --- /dev/null +++ b/draftlogs/7957_fix.md @@ -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! diff --git a/src/traces/sankey/attributes.js b/src/traces/sankey/attributes.js index 11003ada21d..60c5fefbbbb 100644 --- a/src/traces/sankey/attributes.js +++ b/src/traces/sankey/attributes.js @@ -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', diff --git a/src/traces/sankey/render.js b/src/traces/sankey/render.js index 30a7df024d1..a62ad4555ac 100644 --- a/src/traces/sankey/render.js +++ b/src/traces/sankey/render.js @@ -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; @@ -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; + } } Registry.call('_guiRestyle', gd, { 'node.x': [x], diff --git a/src/types/generated/schema.d.ts b/src/types/generated/schema.d.ts index 796fdd93ca9..8dda5470565 100644 --- a/src/types/generated/schema.d.ts +++ b/src/types/generated/schema.d.ts @@ -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; diff --git a/test/jasmine/tests/sankey_test.js b/test/jasmine/tests/sankey_test.js index 9220f95d7c3..5e3598002cc 100644 --- a/test/jasmine/tests/sankey_test.js +++ b/test/jasmine/tests/sankey_test.js @@ -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); diff --git a/test/plot-schema.json b/test/plot-schema.json index 3e04122d215..5228652e592 100644 --- a/test/plot-schema.json +++ b/test/plot-schema.json @@ -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"