Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Sankey Link Hover Styling #6839

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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)]
43 changes: 37 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,38 @@ 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++) {
var rules = document.styleSheets[i].cssRules;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I get the following error in the console when start using hover:

Uncaught DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @archmoj , thanks for having a look at this. I guess this is due to cross-origin resource sharing policy preventing your browser from looking at css sheets from different domains. I didn't encounter it personally, but I've added a try/catch block to allow the code to skip over css sheets that it's not allowed to read. Does it work now on your end?

for(var j = 0; j < rules.length; j++) {
if(rules[j].selectorText === '.sankey-link-hover') {
styleExists = true;
break;
}
}
if(styleExists) break;
}

// 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 +105,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 +122,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