Skip to content

Commit b8238e0

Browse files
author
Michael Potter
committed
Allow end users to override the sankey link hover style by providing their own css styling
1 parent 9664ca9 commit b8238e0

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/traces/sankey/plot.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ var cn = require('./constants').cn;
1010

1111
var _ = Lib._;
1212

13+
var linkStyleInitialized = false;
14+
1315
function renderableValuePresent(d) {return d !== '';}
1416

1517
function ownTrace(selection, d) {
@@ -62,9 +64,38 @@ function nodeNonHoveredStyle(sankeyNode, d, sankey) {
6264
}
6365

6466
function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
65-
sankeyLink.style('fill-opacity', function(l) {
67+
if (!linkStyleInitialized) {
68+
// Figure out whether the user has provided their own sankey-link-hover style.
69+
let styleExists = false;
70+
for (let i=0; i<document.styleSheets.length; i++) {
71+
const rules = document.styleSheets[i].cssRules;
72+
for (let j=0; j < rules.length; j++) {
73+
if (rules[j].selectorText === '.sankey-link-hover') {
74+
styleExists = true;
75+
break;
76+
}
77+
}
78+
if (styleExists) break;
79+
}
80+
81+
// If not, insert a default one
82+
if (!styleExists) {
83+
var style = document.querySelector('style');
84+
if (!style) {
85+
style = document.createElement('style');
86+
document.head.appendChild(style);
87+
}
88+
const sheet = style.sheet;
89+
// If these are not flagged as !important chrome won't render the change
90+
sheet.insertRule('.sankey-link-hover { fill-opacity: 0.4 !important; }', 0);
91+
}
92+
93+
linkStyleInitialized = true;
94+
}
95+
96+
sankeyLink.classed('sankey-link-hover', function(l) {
6697
if(!l.link.concentrationscale) {
67-
return 0.4;
98+
return true;
6899
}
69100
});
70101

@@ -74,9 +105,9 @@ function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
74105
ownTrace(sankey, d)
75106
.selectAll('.' + cn.sankeyLink)
76107
.filter(function(l) {return l.link.label === label;})
77-
.style('fill-opacity', function(l) {
108+
.classed('sankey-link-hover', function(l) {
78109
if(!l.link.concentrationscale) {
79-
return 0.4;
110+
return true;
80111
}
81112
});
82113
}
@@ -91,15 +122,15 @@ function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
91122
}
92123

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

96127
sankeyLink.each(function(curLink) {
97128
var label = curLink.link.label;
98129
if(label !== '') {
99130
ownTrace(sankey, d)
100131
.selectAll('.' + cn.sankeyLink)
101132
.filter(function(l) {return l.link.label === label;})
102-
.style('fill-opacity', function(d) {return d.tinyColorAlpha;});
133+
.classed('sankey-link-hover', false);
103134
}
104135
});
105136

0 commit comments

Comments
 (0)