Skip to content

Commit 8bf6d89

Browse files
authoredDec 15, 2023
Merge pull request #6799 from DominicWuest/fix-sankey-trace-highlight
Fix hovering over Sankey node only fully highlights first trace
2 parents 17f7013 + 2d81fdf commit 8bf6d89

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed
 

‎draftlogs/6799_fix.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix hovering over Sankey node only fully highlights first trace [[#6799](https://github.com/plotly/plotly.js/pull/6799)], with thanks to @DominicWuest for the contribution!

‎src/traces/sankey/plot.js

+23-20
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,25 @@ function nodeNonHoveredStyle(sankeyNode, d, sankey) {
6262
}
6363

6464
function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
65-
var label = sankeyLink.datum().link.label;
66-
6765
sankeyLink.style('fill-opacity', function(l) {
6866
if(!l.link.concentrationscale) {
6967
return 0.4;
7068
}
7169
});
7270

73-
if(label) {
74-
ownTrace(sankey, d)
75-
.selectAll('.' + cn.sankeyLink)
76-
.filter(function(l) {return l.link.label === label;})
77-
.style('fill-opacity', function(l) {
78-
if(!l.link.concentrationscale) {
79-
return 0.4;
80-
}
81-
});
82-
}
71+
sankeyLink.each(function(curLink) {
72+
var label = curLink.link.label;
73+
if(label !== '') {
74+
ownTrace(sankey, d)
75+
.selectAll('.' + cn.sankeyLink)
76+
.filter(function(l) {return l.link.label === label;})
77+
.style('fill-opacity', function(l) {
78+
if(!l.link.concentrationscale) {
79+
return 0.4;
80+
}
81+
});
82+
}
83+
});
8384

8485
if(visitNodes) {
8586
ownTrace(sankey, d)
@@ -90,15 +91,17 @@ function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
9091
}
9192

9293
function linkNonHoveredStyle(d, sankey, visitNodes, sankeyLink) {
93-
var label = sankeyLink.datum().link.label;
94-
9594
sankeyLink.style('fill-opacity', function(d) {return d.tinyColorAlpha;});
96-
if(label) {
97-
ownTrace(sankey, d)
98-
.selectAll('.' + cn.sankeyLink)
99-
.filter(function(l) {return l.link.label === label;})
100-
.style('fill-opacity', function(d) {return d.tinyColorAlpha;});
101-
}
95+
96+
sankeyLink.each(function(curLink) {
97+
var label = curLink.link.label;
98+
if(label !== '') {
99+
ownTrace(sankey, d)
100+
.selectAll('.' + cn.sankeyLink)
101+
.filter(function(l) {return l.link.label === label;})
102+
.style('fill-opacity', function(d) {return d.tinyColorAlpha;});
103+
}
104+
});
102105

103106
if(visitNodes) {
104107
ownTrace(sankey, d)

‎test/jasmine/tests/sankey_test.js

+28
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,34 @@ describe('sankey tests', function() {
10731073
})
10741074
.then(done, done.fail);
10751075
});
1076+
1077+
it('should (un-)highlight all traces ending in a (un-)hovered node', function(done) {
1078+
var gd = createGraphDiv();
1079+
var mockCopy = Lib.extendDeep({}, mock);
1080+
1081+
Plotly.newPlot(gd, mockCopy)
1082+
.then(function() {
1083+
_hover(200, 250);
1084+
})
1085+
.then(function() {
1086+
d3SelectAll('.sankey-link')
1087+
.filter(function(obj) {
1088+
return obj.link.label === 'stream 1';
1089+
})[0].forEach(function(l) {
1090+
expect(l.style.fillOpacity).toEqual('0.4');
1091+
});
1092+
}).then(function() {
1093+
mouseEvent('mouseout', 200, 250);
1094+
}).then(function() {
1095+
d3SelectAll('.sankey-link')
1096+
.filter(function(obj) {
1097+
return obj.link.label === 'stream 1';
1098+
})[0].forEach(function(l) {
1099+
expect(l.style.fillOpacity).toEqual('0.2');
1100+
});
1101+
})
1102+
.then(done, done.fail);
1103+
});
10761104
});
10771105

10781106
describe('Test hover/click event data:', function() {

0 commit comments

Comments
 (0)