Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/6839_change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Update Sankey trace to allow user-defined link hover style override [[#6839](https://github.com/plotly/plotly.js/pull/6839)]
47 changes: 41 additions & 6 deletions src/traces/sankey/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var cn = require('./constants').cn;

var _ = Lib._;

var linkStyleInitialized = false;

function renderableValuePresent(d) {return d !== '';}

function ownTrace(selection, d) {
Expand Down Expand Up @@ -62,9 +64,42 @@ function nodeNonHoveredStyle(sankeyNode, d, sankey) {
}

function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
sankeyLink.style('fill-opacity', function(l) {
if(!linkStyleInitialized) {
// Figure out whether the user has provided their own sankey-link-hover style.
var styleExists = false;
for(var i = 0; i < document.styleSheets.length; i++) {
try {
var rules = document.styleSheets[i].cssRules;
for(var j = 0; j < rules.length; j++) {
if(rules[j].selectorText === '.sankey-link-hover') {
styleExists = true;
break;
}
}
if(styleExists) break;
} catch(e) {
// This particular style sheet cannot be accessed (due to CORS policy)
}
}

// If not, insert a default one
if(!styleExists) {
var style = document.querySelector('style');
if(!style) {
style = document.createElement('style');
document.head.appendChild(style);
}
var sheet = style.sheet;
// If these are not flagged as !important chrome won't render the change
sheet.insertRule('.sankey-link-hover { fill-opacity: 0.4 !important; }', 0);
}

linkStyleInitialized = true;
}

sankeyLink.classed('sankey-link-hover', function(l) {
if(!l.link.concentrationscale) {
return 0.4;
return true;
}
});

Expand All @@ -74,9 +109,9 @@ function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
ownTrace(sankey, d)
.selectAll('.' + cn.sankeyLink)
.filter(function(l) {return l.link.label === label;})
.style('fill-opacity', function(l) {
.classed('sankey-link-hover', function(l) {
if(!l.link.concentrationscale) {
return 0.4;
return true;
}
});
}
Expand All @@ -91,15 +126,15 @@ function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
}

function linkNonHoveredStyle(d, sankey, visitNodes, sankeyLink) {
sankeyLink.style('fill-opacity', function(d) {return d.tinyColorAlpha;});
sankeyLink.classed('sankey-link-hover', false);

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;});
.classed('sankey-link-hover', false);
}
});

Expand Down
4 changes: 2 additions & 2 deletions test/jasmine/tests/sankey_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ describe('sankey tests', function() {
.filter(function(obj) {
return obj.link.label === 'stream 1';
})[0].forEach(function(l) {
expect(l.style.fillOpacity).toEqual('0.4');
expect(l.classList.contains('sankey-link-hover')).toBe(true);
});
}).then(function() {
mouseEvent('mouseout', 200, 250);
Expand All @@ -1096,7 +1096,7 @@ describe('sankey tests', function() {
.filter(function(obj) {
return obj.link.label === 'stream 1';
})[0].forEach(function(l) {
expect(l.style.fillOpacity).toEqual('0.2');
expect(l.classList.contains('sankey-link-hover')).toBe(false);
});
})
.then(done, done.fail);
Expand Down