Skip to content

Commit 3204274

Browse files
committed
sankey: add fullData to eventData, test (node|link).hovertemplate
1 parent d952713 commit 3204274

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/traces/sankey/plot.js

+6
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ module.exports = function plot(gd, calcData) {
133133
if(gd._fullLayout.hovermode === false) return;
134134
d3.select(element).call(linkHoveredStyle.bind(0, d, sankey, true));
135135
if(d.link.trace.link.hoverinfo !== 'skip') {
136+
d.link.fullData = d.link.trace;
136137
gd.emit('plotly_hover', {
137138
event: d3.event,
138139
points: [d.link]
@@ -156,6 +157,7 @@ module.exports = function plot(gd, calcData) {
156157
var hoverCenterY = boundingBox.top + boundingBox.height / 2;
157158

158159
var hovertemplateLabels = {valueLabel: d3.format(d.valueFormat)(d.link.value) + d.valueSuffix};
160+
d.link.fullData = d.link.trace;
159161

160162
var tooltip = Fx.loneHover({
161163
x: hoverCenterX - rootBBox.left,
@@ -190,6 +192,7 @@ module.exports = function plot(gd, calcData) {
190192
if(gd._fullLayout.hovermode === false) return;
191193
d3.select(element).call(linkNonHoveredStyle.bind(0, d, sankey, true));
192194
if(d.link.trace.link.hoverinfo !== 'skip') {
195+
d.link.fullData = d.link.trace;
193196
gd.emit('plotly_unhover', {
194197
event: d3.event,
195198
points: [d.link]
@@ -211,6 +214,7 @@ module.exports = function plot(gd, calcData) {
211214
if(gd._fullLayout.hovermode === false) return;
212215
d3.select(element).call(nodeHoveredStyle, d, sankey);
213216
if(d.node.trace.node.hoverinfo !== 'skip') {
217+
d.node.fullData = d.node.trace;
214218
gd.emit('plotly_hover', {
215219
event: d3.event,
216220
points: [d.node]
@@ -231,6 +235,7 @@ module.exports = function plot(gd, calcData) {
231235
var hoverCenterY = boundingBox.top + boundingBox.height / 4 - rootBBox.top;
232236

233237
var hovertemplateLabels = {valueLabel: d3.format(d.valueFormat)(d.node.value) + d.valueSuffix};
238+
d.node.fullData = d.node.trace;
234239

235240
var tooltip = Fx.loneHover({
236241
x0: hoverCenterX0,
@@ -266,6 +271,7 @@ module.exports = function plot(gd, calcData) {
266271
if(gd._fullLayout.hovermode === false) return;
267272
d3.select(element).call(nodeNonHoveredStyle, d, sankey);
268273
if(d.node.trace.node.hoverinfo !== 'skip') {
274+
d.node.fullData = d.node.trace;
269275
gd.emit('plotly_unhover', {
270276
event: d3.event,
271277
points: [d.node]

test/jasmine/tests/sankey_test.js

+47
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,53 @@ describe('sankey tests', function() {
497497
.then(done);
498498
});
499499

500+
it('should show the correct hover labels when hovertemplate is specified', function(done) {
501+
var gd = createGraphDiv();
502+
var mockCopy = Lib.extendDeep({}, mock);
503+
504+
Plotly.plot(gd, mockCopy).then(function() {
505+
_hover(404, 302);
506+
507+
assertLabel(
508+
['Solid', 'incoming flow count: 4', 'outgoing flow count: 3', '447TWh'],
509+
['rgb(148, 103, 189)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
510+
);
511+
})
512+
.then(function() {
513+
_hover(450, 300);
514+
515+
assertLabel(
516+
['source: Solid', 'target: Industry', '46TWh'],
517+
['rgb(0, 0, 96)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
518+
);
519+
})
520+
// Test (node|link).hovertemplate
521+
.then(function() {
522+
return Plotly.restyle(gd, {
523+
'node.hovertemplate': 'hovertemplate<br>%{value}<br>%{value:0.2f}<extra>%{fullData.name}</extra>',
524+
'link.hovertemplate': 'hovertemplate<br>source: %{source.label}<br>target: %{target.label}<br>size: %{value:0.0f}TWh<extra>%{fullData.name}</extra>'
525+
});
526+
})
527+
.then(function() {
528+
_hover(404, 302);
529+
530+
assertLabel(
531+
[ 'hovertemplate', '447TWh', '447.48', 'trace 0'],
532+
['rgb(148, 103, 189)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
533+
);
534+
})
535+
.then(function() {
536+
_hover(450, 300);
537+
538+
assertLabel(
539+
['hovertemplate', 'source: Solid', 'target: Industry', 'size: 46TWh', 'trace 0'],
540+
['rgb(0, 0, 96)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
541+
);
542+
})
543+
.catch(failTest)
544+
.then(done);
545+
});
546+
500547
it('should show the correct hover labels with the style provided in template', function(done) {
501548
var gd = createGraphDiv();
502549
var mockCopy = Lib.extendDeep({}, mock);

0 commit comments

Comments
 (0)