Skip to content

Commit 6233297

Browse files
committed
adding hovertemplate support to sankey
1 parent 2f6c6e7 commit 6233297

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

src/traces/sankey/attributes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var plotAttrs = require('../../plots/attributes');
1313
var colorAttrs = require('../../components/color/attributes');
1414
var fxAttrs = require('../../components/fx/attributes');
1515
var domainAttrs = require('../../plots/domain').attributes;
16+
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
1617

1718
var extendFlat = require('../../lib/extend').extendFlat;
1819
var overrideAll = require('../../plot_api/edit_types').overrideAll;
@@ -146,6 +147,9 @@ var attrs = module.exports = overrideAll({
146147
].join(' ')
147148
},
148149
hoverlabel: fxAttrs.hoverlabel, // needs editType override,
150+
hovertemplate: hovertemplateAttrs({}, {
151+
keys: []
152+
}),
149153
description: 'The nodes of the Sankey plot.'
150154
},
151155

@@ -216,6 +220,9 @@ var attrs = module.exports = overrideAll({
216220
].join(' ')
217221
},
218222
hoverlabel: fxAttrs.hoverlabel, // needs editType override,
223+
hovertemplate: hovertemplateAttrs({}, {
224+
keys: []
225+
}),
219226
description: 'The links of the Sankey plot.'
220227
}
221228
}, 'calc', 'nested');

src/traces/sankey/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3535
coerceNode('line.width');
3636
coerceNode('hoverinfo', traceIn.hoverinfo);
3737
handleHoverLabelDefaults(nodeIn, nodeOut, coerceNode, hoverlabelDefault);
38+
coerceNode('hovertemplate');
3839

3940
var colors = layout.colorway;
4041

@@ -57,6 +58,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
5758
coerceLink('line.width');
5859
coerceLink('hoverinfo', traceIn.hoverinfo);
5960
handleHoverLabelDefaults(linkIn, linkOut, coerceLink, hoverlabelDefault);
61+
coerceLink('hovertemplate');
6062

6163
var defaultLinkColor = tinycolor(layout.paper_bgcolor).getLuminance() < 0.333 ?
6264
'rgba(255, 255, 255, 0.6)' :

src/traces/sankey/plot.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,12 @@ module.exports = function plot(gd, calcData) {
155155
var hoverCenterX = boundingBox.left + boundingBox.width / 2;
156156
var hoverCenterY = boundingBox.top + boundingBox.height / 2;
157157

158+
var hovertemplateLabels = {valueLabel: d3.format(d.valueFormat)(d.link.value) + d.valueSuffix};
159+
158160
var tooltip = Fx.loneHover({
159161
x: hoverCenterX - rootBBox.left,
160162
y: hoverCenterY - rootBBox.top,
161-
name: d3.format(d.valueFormat)(d.link.value) + d.valueSuffix,
163+
name: hovertemplateLabels.valueLabel,
162164
text: [
163165
d.link.label || '',
164166
sourceLabel + d.link.source.label,
@@ -169,7 +171,11 @@ module.exports = function plot(gd, calcData) {
169171
fontFamily: castHoverOption(obj, 'font.family'),
170172
fontSize: castHoverOption(obj, 'font.size'),
171173
fontColor: castHoverOption(obj, 'font.color'),
172-
idealAlign: d3.event.x < hoverCenterX ? 'right' : 'left'
174+
idealAlign: d3.event.x < hoverCenterX ? 'right' : 'left',
175+
176+
hovertemplate: obj.hovertemplate,
177+
hovertemplateLabels: hovertemplateLabels,
178+
eventData: [d.link]
173179
}, {
174180
container: fullLayout._hoverlayer.node(),
175181
outerContainer: fullLayout._paper.node(),
@@ -224,6 +230,8 @@ module.exports = function plot(gd, calcData) {
224230
var hoverCenterX1 = boundingBox.right + 2 - rootBBox.left;
225231
var hoverCenterY = boundingBox.top + boundingBox.height / 4 - rootBBox.top;
226232

233+
var hovertemplateLabels = {valueLabel: d3.format(d.valueFormat)(d.node.value) + d.valueSuffix};
234+
227235
var tooltip = Fx.loneHover({
228236
x0: hoverCenterX0,
229237
x1: hoverCenterX1,
@@ -239,7 +247,11 @@ module.exports = function plot(gd, calcData) {
239247
fontFamily: castHoverOption(obj, 'font.family'),
240248
fontSize: castHoverOption(obj, 'font.size'),
241249
fontColor: castHoverOption(obj, 'font.color'),
242-
idealAlign: 'left'
250+
idealAlign: 'left',
251+
252+
hovertemplate: obj.hovertemplate,
253+
hovertemplateLabels: hovertemplateLabels,
254+
eventData: [d.node]
243255
}, {
244256
container: fullLayout._hoverlayer.node(),
245257
outerContainer: fullLayout._paper.node(),

test/jasmine/tests/sankey_test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,8 +744,12 @@ describe('sankey tests', function() {
744744
_assert(d, {
745745
curveNumber: 0,
746746
pointNumber: 4,
747-
label: 'Solid'
747+
label: 'Solid',
748+
value: 447.48
748749
});
750+
var pt = d.points[0];
751+
expect(pt.sourceLinks.length).toBe(3);
752+
expect(pt.targetLinks.length).toBe(4);
749753
})
750754
.then(function() { return _hover('link'); })
751755
.then(function(d) {
@@ -754,6 +758,9 @@ describe('sankey tests', function() {
754758
pointNumber: 61,
755759
value: 46.477
756760
});
761+
var pt = d.points[0];
762+
expect(pt.hasOwnProperty('source')).toBeTruthy();
763+
expect(pt.hasOwnProperty('target')).toBeTruthy();
757764
})
758765
.then(function() { return _unhover('node'); })
759766
.then(function(d) {

0 commit comments

Comments
 (0)