Skip to content

Commit 5a5db77

Browse files
committed
sankey: introduce link.customdata
1 parent 83f25af commit 5a5db77

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

src/traces/sankey/attributes.js

+7
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ var attrs = module.exports = overrideAll({
207207
'If `link.color` is omitted, then by default, a translucent grey link will be used.'
208208
].join(' ')
209209
},
210+
customdata: {
211+
valType: 'data_array',
212+
editType: 'calc',
213+
description: [
214+
'Assigns extra data each link.'
215+
].join(' ')
216+
},
210217
line: {
211218
color: {
212219
valType: 'color',

src/traces/sankey/calc.js

+2
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,

src/traces/sankey/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
7474
'rgba(0, 0, 0, 0.2)';
7575

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

7879
handleArrayContainerDefaults(linkIn, linkOut, {
7980
name: 'colorscales',

test/jasmine/tests/sankey_test.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -816,7 +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 = [0, 0, 0, 0, '15']
819+
mockCopy.data[0].node.customdata = [];
820+
mockCopy.data[0].node.customdata[4] = 'nodeCustomdata';
821+
mockCopy.data[0].link.customdata = [];
822+
mockCopy.data[0].link.customdata[61] = 'linkCustomdata';
820823

821824
Plotly.plot(gd, mockCopy).then(function() {
822825
_hover(404, 302);
@@ -838,22 +841,22 @@ describe('sankey tests', function() {
838841
.then(function() {
839842
return Plotly.restyle(gd, {
840843
'node.hovertemplate': 'hovertemplate<br>%{value}<br>%{value:0.2f}<br>%{customdata}<extra>%{fullData.name}</extra>',
841-
'link.hovertemplate': 'hovertemplate<br>source: %{source.label}<br>target: %{target.label}<br>size: %{value:0.0f}TWh<extra>%{fullData.name}</extra>'
844+
'link.hovertemplate': 'hovertemplate<br>source: %{source.label}<br>target: %{target.label}<br>size: %{value:0.0f}TWh<br>%{customdata}<extra>%{fullData.name}</extra>'
842845
});
843846
})
844847
.then(function() {
845848
_hover(404, 302);
846849

847850
assertLabel(
848-
[ 'hovertemplate', '447TWh', '447.48', '15', 'trace 0'],
851+
[ 'hovertemplate', '447TWh', '447.48', 'nodeCustomdata', 'trace 0'],
849852
['rgb(148, 103, 189)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
850853
);
851854
})
852855
.then(function() {
853856
_hover(450, 300);
854857

855858
assertLabel(
856-
['hovertemplate', 'source: Solid', 'target: Industry', 'size: 46TWh', 'trace 0'],
859+
['hovertemplate', 'source: Solid', 'target: Industry', 'size: 46TWh', 'linkCustomdata', 'trace 0'],
857860
['rgb(0, 0, 96)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
858861
);
859862
})

0 commit comments

Comments
 (0)