Skip to content

Commit

Permalink
Merge pull request #6799 from DominicWuest/fix-sankey-trace-highlight
Browse files Browse the repository at this point in the history
Fix hovering over Sankey node only fully highlights first trace
  • Loading branch information
archmoj committed Dec 15, 2023
2 parents 17f7013 + 2d81fdf commit 8bf6d89
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
1 change: 1 addition & 0 deletions draftlogs/6799_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +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!
43 changes: 23 additions & 20 deletions src/traces/sankey/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,25 @@ function nodeNonHoveredStyle(sankeyNode, d, sankey) {
}

function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
var label = sankeyLink.datum().link.label;

sankeyLink.style('fill-opacity', function(l) {
if(!l.link.concentrationscale) {
return 0.4;
}
});

if(label) {
ownTrace(sankey, d)
.selectAll('.' + cn.sankeyLink)
.filter(function(l) {return l.link.label === label;})
.style('fill-opacity', function(l) {
if(!l.link.concentrationscale) {
return 0.4;
}
});
}
sankeyLink.each(function(curLink) {
var label = curLink.link.label;
if(label !== '') {
ownTrace(sankey, d)
.selectAll('.' + cn.sankeyLink)
.filter(function(l) {return l.link.label === label;})
.style('fill-opacity', function(l) {
if(!l.link.concentrationscale) {
return 0.4;
}
});
}
});

if(visitNodes) {
ownTrace(sankey, d)
Expand All @@ -90,15 +91,17 @@ function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
}

function linkNonHoveredStyle(d, sankey, visitNodes, sankeyLink) {
var label = sankeyLink.datum().link.label;

sankeyLink.style('fill-opacity', function(d) {return d.tinyColorAlpha;});
if(label) {
ownTrace(sankey, d)
.selectAll('.' + cn.sankeyLink)
.filter(function(l) {return l.link.label === label;})
.style('fill-opacity', function(d) {return d.tinyColorAlpha;});
}

sankeyLink.each(function(curLink) {
var label = curLink.link.label;
if(label !== '') {
ownTrace(sankey, d)
.selectAll('.' + cn.sankeyLink)
.filter(function(l) {return l.link.label === label;})
.style('fill-opacity', function(d) {return d.tinyColorAlpha;});
}
});

if(visitNodes) {
ownTrace(sankey, d)
Expand Down
28 changes: 28 additions & 0 deletions test/jasmine/tests/sankey_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,34 @@ describe('sankey tests', function() {
})
.then(done, done.fail);
});

it('should (un-)highlight all traces ending in a (un-)hovered node', function(done) {
var gd = createGraphDiv();
var mockCopy = Lib.extendDeep({}, mock);

Plotly.newPlot(gd, mockCopy)
.then(function() {
_hover(200, 250);
})
.then(function() {
d3SelectAll('.sankey-link')
.filter(function(obj) {
return obj.link.label === 'stream 1';
})[0].forEach(function(l) {
expect(l.style.fillOpacity).toEqual('0.4');
});
}).then(function() {
mouseEvent('mouseout', 200, 250);
}).then(function() {
d3SelectAll('.sankey-link')
.filter(function(obj) {
return obj.link.label === 'stream 1';
})[0].forEach(function(l) {
expect(l.style.fillOpacity).toEqual('0.2');
});
})
.then(done, done.fail);
});
});

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

0 comments on commit 8bf6d89

Please sign in to comment.