Skip to content

Commit 8f049fd

Browse files
authored
Merge pull request #4621 from plotly/sankey-customdata
sankey: introducing attributes (node|link).customdata
2 parents a1b0334 + c27ced0 commit 8f049fd

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/traces/sankey/attributes.js

+17
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ var attrs = module.exports = overrideAll({
8484
description: 'Sets the font for node labels'
8585
}),
8686

87+
// Remove top-level customdata
88+
customdata: undefined,
89+
8790
node: {
8891
label: {
8992
valType: 'data_array',
@@ -128,6 +131,13 @@ var attrs = module.exports = overrideAll({
128131
'what is beneath the node.'
129132
].join(' ')
130133
},
134+
customdata: {
135+
valType: 'data_array',
136+
editType: 'calc',
137+
description: [
138+
'Assigns extra data to each node.'
139+
].join(' ')
140+
},
131141
line: {
132142
color: {
133143
valType: 'color',
@@ -200,6 +210,13 @@ var attrs = module.exports = overrideAll({
200210
'If `link.color` is omitted, then by default, a translucent grey link will be used.'
201211
].join(' ')
202212
},
213+
customdata: {
214+
valType: 'data_array',
215+
editType: 'calc',
216+
description: [
217+
'Assigns extra data to each link.'
218+
].join(' ')
219+
},
203220
line: {
204221
color: {
205222
valType: 'color',

src/traces/sankey/calc.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function convertToD3Sankey(trace) {
2222

2323
var links = [];
2424
var hasLinkColorArray = isArrayOrTypedArray(linkSpec.color);
25+
var hasLinkCustomdataArray = isArrayOrTypedArray(linkSpec.customdata);
2526
var linkedNodes = {};
2627

2728
var components = {};
@@ -103,6 +104,7 @@ function convertToD3Sankey(trace) {
103104
pointNumber: i,
104105
label: label,
105106
color: hasLinkColorArray ? linkSpec.color[i] : linkSpec.color,
107+
customdata: hasLinkCustomdataArray ? linkSpec.customdata[i] : linkSpec.customdata,
106108
concentrationscale: concentrationscale,
107109
source: source,
108110
target: target,
@@ -116,6 +118,7 @@ function convertToD3Sankey(trace) {
116118
// Process nodes
117119
var totalCount = nodeCount + groups.length;
118120
var hasNodeColorArray = isArrayOrTypedArray(nodeSpec.color);
121+
var hasNodeCustomdataArray = isArrayOrTypedArray(nodeSpec.customdata);
119122
var nodes = [];
120123
for(i = 0; i < totalCount; i++) {
121124
if(!linkedNodes[i]) continue;
@@ -126,7 +129,8 @@ function convertToD3Sankey(trace) {
126129
childrenNodes: [],
127130
pointNumber: i,
128131
label: l,
129-
color: hasNodeColorArray ? nodeSpec.color[i] : nodeSpec.color
132+
color: hasNodeColorArray ? nodeSpec.color[i] : nodeSpec.color,
133+
customdata: hasNodeCustomdataArray ? nodeSpec.customdata[i] : nodeSpec.customdata
130134
});
131135
}
132136

src/traces/sankey/defaults.js

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
5050
coerceNode('color', nodeOut.label.map(function(d, i) {
5151
return Color.addOpacity(defaultNodePalette(i), 0.8);
5252
}));
53+
coerceNode('customdata');
5354

5455
// link attributes
5556
var linkIn = traceIn.link || {};
@@ -73,6 +74,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
7374
'rgba(0, 0, 0, 0.2)';
7475

7576
coerceLink('color', Lib.repeat(defaultLinkColor, linkOut.value.length));
77+
coerceLink('customdata');
7678

7779
handleArrayContainerDefaults(linkIn, linkOut, {
7880
name: 'colorscales',

test/jasmine/tests/sankey_test.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,10 @@ describe('sankey tests', function() {
816816
it('should show the correct hover labels when hovertemplate is specified', function(done) {
817817
var gd = createGraphDiv();
818818
var mockCopy = Lib.extendDeep({}, mock);
819+
mockCopy.data[0].node.customdata = [];
820+
mockCopy.data[0].node.customdata[4] = ['nodeCustomdata0', 'nodeCustomdata1'];
821+
mockCopy.data[0].link.customdata = [];
822+
mockCopy.data[0].link.customdata[61] = ['linkCustomdata0', 'linkCustomdata1'];
819823

820824
Plotly.plot(gd, mockCopy).then(function() {
821825
_hover(404, 302);
@@ -836,23 +840,23 @@ describe('sankey tests', function() {
836840
// Test (node|link).hovertemplate
837841
.then(function() {
838842
return Plotly.restyle(gd, {
839-
'node.hovertemplate': 'hovertemplate<br>%{value}<br>%{value:0.2f}<extra>%{fullData.name}</extra>',
840-
'link.hovertemplate': 'hovertemplate<br>source: %{source.label}<br>target: %{target.label}<br>size: %{value:0.0f}TWh<extra>%{fullData.name}</extra>'
843+
'node.hovertemplate': 'hovertemplate<br>%{value}<br>%{value:0.2f}<br>%{customdata[0]}/%{customdata[1]}<extra>%{fullData.name}</extra>',
844+
'link.hovertemplate': 'hovertemplate<br>source: %{source.label}<br>target: %{target.label}<br>size: %{value:0.0f}TWh<br>%{customdata[1]}<extra>%{fullData.name}</extra>'
841845
});
842846
})
843847
.then(function() {
844848
_hover(404, 302);
845849

846850
assertLabel(
847-
[ 'hovertemplate', '447TWh', '447.48', 'trace 0'],
851+
[ 'hovertemplate', '447TWh', '447.48', 'nodeCustomdata0/nodeCustomdata1', 'trace 0'],
848852
['rgb(148, 103, 189)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
849853
);
850854
})
851855
.then(function() {
852856
_hover(450, 300);
853857

854858
assertLabel(
855-
['hovertemplate', 'source: Solid', 'target: Industry', 'size: 46TWh', 'trace 0'],
859+
['hovertemplate', 'source: Solid', 'target: Industry', 'size: 46TWh', 'linkCustomdata1', 'trace 0'],
856860
['rgb(0, 0, 96)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
857861
);
858862
})

0 commit comments

Comments
 (0)