Skip to content

Add support for sankey links with arrows #6276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 9, 2022
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
2 changes: 2 additions & 0 deletions draftlogs/6276_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add support for sankey links with arrows [[#6276](https://github.com/plotly/plotly.js/pull/6276)],
with thanks to @Andy2003 for the contribution!
8 changes: 8 additions & 0 deletions src/traces/sankey/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ var attrs = module.exports = overrideAll({
},

link: {
arrowlen: {
valType: 'number',
min: 0,
dflt: 0,
description: [
'Sets the length (in px) of the links arrow, if 0 no arrow will be drawn.'
].join(' ')
},
label: {
valType: 'data_array',
dflt: [],
Expand Down
1 change: 1 addition & 0 deletions src/traces/sankey/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
return Lib.coerce(linkIn, linkOut, attributes.link, attr, dflt);
}
coerceLink('label');
coerceLink('arrowlen');
coerceLink('source');
coerceLink('target');
coerceLink('value');
Expand Down
75 changes: 43 additions & 32 deletions src/traces/sankey/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ function sankeyModel(layout, d, traceIndex) {
nodeLineWidth: trace.node.line.width,
linkLineColor: trace.link.line.color,
linkLineWidth: trace.link.line.width,
linkArrowLength: trace.link.arrowlen,
valueFormat: trace.valueformat,
valueSuffix: trace.valuesuffix,
textFont: trace.textfont,
Expand Down Expand Up @@ -309,6 +310,7 @@ function linkModel(d, l, i) {
linkPath: linkPath,
linkLineColor: d.linkLineColor,
linkLineWidth: d.linkLineWidth,
linkArrowLength: d.linkArrowLength,
valueFormat: d.valueFormat,
valueSuffix: d.valueSuffix,
sankey: d.sankey,
Expand All @@ -318,7 +320,7 @@ function linkModel(d, l, i) {
};
}

function createCircularClosedPathString(link) {
function createCircularClosedPathString(link, arrowLen) {
// Using coordinates computed by d3-sankey-circular
var pathString = '';
var offset = link.width / 2;
Expand All @@ -328,17 +330,17 @@ function createCircularClosedPathString(link) {
pathString =
// start at the left of the target node
'M ' +
coords.targetX + ' ' + (coords.targetY + offset) + ' ' +
(coords.targetX - arrowLen) + ' ' + (coords.targetY + offset) + ' ' +
'L' +
coords.rightInnerExtent + ' ' + (coords.targetY + offset) +
(coords.rightInnerExtent - arrowLen) + ' ' + (coords.targetY + offset) +
'A' +
(coords.rightLargeArcRadius + offset) + ' ' + (coords.rightSmallArcRadius + offset) + ' 0 0 1 ' +
(coords.rightFullExtent - offset) + ' ' + (coords.targetY - coords.rightSmallArcRadius) +
(coords.rightFullExtent - offset - arrowLen) + ' ' + (coords.targetY - coords.rightSmallArcRadius) +
'L' +
(coords.rightFullExtent - offset) + ' ' + coords.verticalRightInnerExtent +
(coords.rightFullExtent - offset - arrowLen) + ' ' + coords.verticalRightInnerExtent +
'A' +
(coords.rightLargeArcRadius + offset) + ' ' + (coords.rightLargeArcRadius + offset) + ' 0 0 1 ' +
coords.rightInnerExtent + ' ' + (coords.verticalFullExtent - offset) +
(coords.rightInnerExtent - arrowLen) + ' ' + (coords.verticalFullExtent - offset) +
'L' +
coords.leftInnerExtent + ' ' + (coords.verticalFullExtent - offset) +
'A' +
Expand Down Expand Up @@ -366,34 +368,35 @@ function createCircularClosedPathString(link) {
(coords.leftLargeArcRadius - offset) + ' ' + (coords.leftLargeArcRadius - offset) + ' 0 0 0 ' +
coords.leftInnerExtent + ' ' + (coords.verticalFullExtent + offset) +
'L' +
coords.rightInnerExtent + ' ' + (coords.verticalFullExtent + offset) +
(coords.rightInnerExtent - arrowLen) + ' ' + (coords.verticalFullExtent + offset) +
'A' +
(coords.rightLargeArcRadius - offset) + ' ' + (coords.rightLargeArcRadius - offset) + ' 0 0 0 ' +
(coords.rightFullExtent + offset) + ' ' + coords.verticalRightInnerExtent +
(coords.rightFullExtent + offset - arrowLen) + ' ' + coords.verticalRightInnerExtent +
'L' +
(coords.rightFullExtent + offset) + ' ' + (coords.targetY - coords.rightSmallArcRadius) +
(coords.rightFullExtent + offset - arrowLen) + ' ' + (coords.targetY - coords.rightSmallArcRadius) +
'A' +
(coords.rightLargeArcRadius - offset) + ' ' + (coords.rightSmallArcRadius - offset) + ' 0 0 0 ' +
coords.rightInnerExtent + ' ' + (coords.targetY - offset) +
(coords.rightInnerExtent - arrowLen) + ' ' + (coords.targetY - offset) +
'L' +
coords.targetX + ' ' + (coords.targetY - offset) +
(coords.targetX - arrowLen) + ' ' + (coords.targetY - offset) +
(arrowLen > 0 ? 'L' + coords.targetX + ' ' + (coords.targetY) : '') +
'Z';
} else {
// Bottom path
pathString =
// start at the left of the target node
'M ' +
coords.targetX + ' ' + (coords.targetY - offset) + ' ' +
(coords.targetX - arrowLen) + ' ' + (coords.targetY - offset) + ' ' +
'L' +
coords.rightInnerExtent + ' ' + (coords.targetY - offset) +
(coords.rightInnerExtent - arrowLen) + ' ' + (coords.targetY - offset) +
'A' +
(coords.rightLargeArcRadius + offset) + ' ' + (coords.rightSmallArcRadius + offset) + ' 0 0 0 ' +
(coords.rightFullExtent - offset) + ' ' + (coords.targetY + coords.rightSmallArcRadius) +
(coords.rightFullExtent - offset - arrowLen) + ' ' + (coords.targetY + coords.rightSmallArcRadius) +
'L' +
(coords.rightFullExtent - offset) + ' ' + coords.verticalRightInnerExtent +
(coords.rightFullExtent - offset - arrowLen) + ' ' + coords.verticalRightInnerExtent +
'A' +
(coords.rightLargeArcRadius + offset) + ' ' + (coords.rightLargeArcRadius + offset) + ' 0 0 0 ' +
coords.rightInnerExtent + ' ' + (coords.verticalFullExtent + offset) +
(coords.rightInnerExtent - arrowLen) + ' ' + (coords.verticalFullExtent + offset) +
'L' +
coords.leftInnerExtent + ' ' + (coords.verticalFullExtent + offset) +
'A' +
Expand Down Expand Up @@ -421,17 +424,18 @@ function createCircularClosedPathString(link) {
(coords.leftLargeArcRadius - offset) + ' ' + (coords.leftLargeArcRadius - offset) + ' 0 0 1 ' +
coords.leftInnerExtent + ' ' + (coords.verticalFullExtent - offset) +
'L' +
coords.rightInnerExtent + ' ' + (coords.verticalFullExtent - offset) +
(coords.rightInnerExtent - arrowLen) + ' ' + (coords.verticalFullExtent - offset) +
'A' +
(coords.rightLargeArcRadius - offset) + ' ' + (coords.rightLargeArcRadius - offset) + ' 0 0 1 ' +
(coords.rightFullExtent + offset) + ' ' + coords.verticalRightInnerExtent +
(coords.rightFullExtent + offset - arrowLen) + ' ' + coords.verticalRightInnerExtent +
'L' +
(coords.rightFullExtent + offset) + ' ' + (coords.targetY + coords.rightSmallArcRadius) +
(coords.rightFullExtent + offset - arrowLen) + ' ' + (coords.targetY + coords.rightSmallArcRadius) +
'A' +
(coords.rightLargeArcRadius - offset) + ' ' + (coords.rightSmallArcRadius - offset) + ' 0 0 1 ' +
coords.rightInnerExtent + ' ' + (coords.targetY + offset) +
(coords.rightInnerExtent - arrowLen) + ' ' + (coords.targetY + offset) +
'L' +
coords.targetX + ' ' + (coords.targetY + offset) +
(coords.targetX - arrowLen) + ' ' + (coords.targetY + offset) +
(arrowLen > 0 ? 'L' + coords.targetX + ' ' + (coords.targetY) : '') +
'Z';
}
return pathString;
Expand All @@ -440,27 +444,34 @@ function createCircularClosedPathString(link) {
function linkPath() {
var curvature = 0.5;
function path(d) {
var arrowLen = d.linkArrowLength;
if(d.link.circular) {
return createCircularClosedPathString(d.link);
return createCircularClosedPathString(d.link, arrowLen);
} else {
var maxArrowLength = Math.abs((d.link.target.x0 - d.link.source.x1) / 2);
if(arrowLen > maxArrowLength) {
arrowLen = maxArrowLength;
}
var x0 = d.link.source.x1;
var x1 = d.link.target.x0;
var x1 = d.link.target.x0 - arrowLen;
var xi = interpolateNumber(x0, x1);
var x2 = xi(curvature);
var x3 = xi(1 - curvature);
var y0a = d.link.y0 - d.link.width / 2;
var y0b = d.link.y0 + d.link.width / 2;
var y1a = d.link.y1 - d.link.width / 2;
var y1b = d.link.y1 + d.link.width / 2;
return 'M' + x0 + ',' + y0a +
'C' + x2 + ',' + y0a +
' ' + x3 + ',' + y1a +
' ' + x1 + ',' + y1a +
'L' + x1 + ',' + y1b +
'C' + x3 + ',' + y1b +
' ' + x2 + ',' + y0b +
' ' + x0 + ',' + y0b +
'Z';
var start = 'M' + x0 + ',' + y0a;
var upperCurve = 'C' + x2 + ',' + y0a +
' ' + x3 + ',' + y1a +
' ' + x1 + ',' + y1a;
var lowerCurve = 'C' + x3 + ',' + y1b +
' ' + x2 + ',' + y0b +
' ' + x0 + ',' + y0b;

var rightEnd = arrowLen > 0 ? 'L' + (x1 + arrowLen) + ',' + (y1a + d.link.width / 2) : '';
rightEnd += 'L' + x1 + ',' + y1b;
return start + upperCurve + rightEnd + lowerCurve + 'Z';
}
}
return path;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/z-sankey_x_y_with_arrows.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions test/image/mocks/z-sankey_circular_with_arrows.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"data": [
{
"type": "sankey",
"node": {
"pad": 5,
"label": ["0", "1", "2", "3", "4", "5", "6"]
},
"link": {
"arrowlen": 20,
"source": [
0, 0, 1, 2, 5, 4, 3
],
"target": [
5, 3, 4, 3, 0, 2, 2
],
"value": [
1, 2, 1, 1, 1, 1, 1
]
}
}],
"layout": {
"title": {"text": "Sankey with circular data and arrows"},
"width": 800,
"height": 800
}
}
28 changes: 28 additions & 0 deletions test/image/mocks/z-sankey_circular_with_arrows_vertical.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"data": [
{
"type": "sankey",
"orientation": "v",
"node": {
"pad": 5,
"label": ["0", "1", "2", "3", "4", "5", "6"]
},
"link": {
"arrowlen": 20,
"source": [
0, 0, 1, 2, 5, 4, 3
],
"target": [
5, 3, 4, 3, 0, 2, 2
],
"value": [
1, 2, 1, 1, 1, 1, 1
]
}
}],
"layout": {
"title": {"text": "Sankey with circular data and arrows"},
"width": 800,
"height": 800
}
}
29 changes: 29 additions & 0 deletions test/image/mocks/z-sankey_x_y_with_arrows.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"data": [
{
"type": "sankey",
"arrangement": "freeform",
"node": {
"label": ["0", "1", "2"],
"x": [1, 2, 3],
"y": [0,0,0]
},
"link": {
"arrowlen": 40,
"source": [
0, 1, 2
],
"target": [
1, 2, 0
],
"value": [
10, 9, 1
]
}
}],
"layout": {
"title": {"text": "Sankey with little space"},
"width": 400,
"height": 400
}
}
7 changes: 7 additions & 0 deletions test/plot-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -42051,6 +42051,13 @@
"valType": "number"
},
"link": {
"arrowlen": {
"description": "Sets the length (in px) of the links arrow, if 0 no arrow will be drawn.",
"dflt": 0,
"editType": "calc",
"min": 0,
"valType": "number"
},
"color": {
"arrayOk": true,
"description": "Sets the `link` color. It can be a single value, or an array for specifying color for each `link`. If `link.color` is omitted, then by default, a translucent grey link will be used.",
Expand Down