Skip to content

Commit dd4765e

Browse files
author
Michael Potter
committed
add a link hovercolor attribute so that end users can customize their hover styling easily (+6 squashed commits)
Squashed commits: [9555c0c] catch exceptions when trying to read access-restricted css files, and update test case to look for css styling [635e5ee] adhere to style guide CI/CD feedback [afcbcc2] remove 'const' in favor of 'var' per CI/CD rules [5e2444b] use 'var' instead of 'let' per ci/cd guide [ba96e96] add a draftlogs entry [b8238e0] Allow end users to override the sankey link hover style by providing their own css styling
1 parent 620a56b commit dd4765e

File tree

7 files changed

+40
-7
lines changed

7 files changed

+40
-7
lines changed

draftlogs/6839_change.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Update Sankey trace to allow user-defined link hover style override [[#6839](https://github.com/plotly/plotly.js/pull/6839)]

src/traces/sankey/attributes.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ var attrs = module.exports = overrideAll({
195195
'If `link.color` is omitted, then by default, a translucent grey link will be used.'
196196
].join(' ')
197197
},
198+
hovercolor: {
199+
valType: 'color',
200+
arrayOk: true,
201+
description: [
202+
'Sets the `link` hover color. It can be a single value, or an array for specifying hovor colors for',
203+
'each `link`. If `link.hovercolor` is omitted, then by default, links will become solid grey when',
204+
'hovered over.'
205+
].join(' ')
206+
},
198207
customdata: {
199208
valType: 'data_array',
200209
editType: 'calc',

src/traces/sankey/calc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function convertToD3Sankey(trace) {
1414

1515
var links = [];
1616
var hasLinkColorArray = isArrayOrTypedArray(linkSpec.color);
17+
var hasLinkHoverColorArray = isArrayOrTypedArray(linkSpec.hovercolor);
1718
var hasLinkCustomdataArray = isArrayOrTypedArray(linkSpec.customdata);
1819
var linkedNodes = {};
1920

@@ -96,6 +97,7 @@ function convertToD3Sankey(trace) {
9697
pointNumber: i,
9798
label: label,
9899
color: hasLinkColorArray ? linkSpec.color[i] : linkSpec.color,
100+
hovercolor: hasLinkHoverColorArray ? linkSpec.hovercolor[i] : linkSpec.hovercolor,
99101
customdata: hasLinkCustomdataArray ? linkSpec.customdata[i] : linkSpec.customdata,
100102
concentrationscale: concentrationscale,
101103
source: source,

src/traces/sankey/defaults.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
6767
'rgba(255, 255, 255, 0.6)' :
6868
'rgba(0, 0, 0, 0.2)';
6969

70+
var defaultHoverColor = tinycolor(layout.paper_bgcolor).getLuminance() < 0.333 ?
71+
'rgba(128, 128, 128, 1.0)' :
72+
'rgba(128, 128, 128, 1.0)';
73+
7074
coerceLink('color', Lib.repeat(defaultLinkColor, linkOut.value.length));
75+
coerceLink('hovercolor', Lib.repeat(defaultHoverColor, linkOut.value.length));
7176
coerceLink('customdata');
7277

7378
handleArrayContainerDefaults(linkIn, linkOut, {

src/traces/sankey/plot.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ function nodeNonHoveredStyle(sankeyNode, d, sankey) {
6262
}
6363

6464
function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
65-
sankeyLink.style('fill-opacity', function(l) {
65+
sankeyLink.style('fill', function(l) {
6666
if(!l.link.concentrationscale) {
67-
return 0.4;
67+
return l.tinyColorHoverHue;
68+
}
69+
}).style('fill-opacity', function(l) {
70+
if(!l.link.concentrationscale) {
71+
return l.tinyColorHoverAlpha;
6872
}
6973
});
7074

@@ -74,9 +78,13 @@ function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
7478
ownTrace(sankey, d)
7579
.selectAll('.' + cn.sankeyLink)
7680
.filter(function(l) {return l.link.label === label;})
77-
.style('fill-opacity', function(l) {
81+
.style('fill', function(l) {
7882
if(!l.link.concentrationscale) {
79-
return 0.4;
83+
return l.tinyColorHoverHue;
84+
}
85+
}).style('fill-opacity', function(l) {
86+
if(!l.link.concentrationscale) {
87+
return l.tinyColorHoverAlpha;
8088
}
8189
});
8290
}
@@ -91,15 +99,20 @@ function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
9199
}
92100

93101
function linkNonHoveredStyle(d, sankey, visitNodes, sankeyLink) {
94-
sankeyLink.style('fill-opacity', function(d) {return d.tinyColorAlpha;});
102+
sankeyLink.style('fill', function(l) {
103+
return l.tinyColorHue;
104+
}).style('fill-opacity', function(l) {
105+
return l.tinyColorAlpha;
106+
});
95107

96108
sankeyLink.each(function(curLink) {
97109
var label = curLink.link.label;
98110
if(label !== '') {
99111
ownTrace(sankey, d)
100112
.selectAll('.' + cn.sankeyLink)
101113
.filter(function(l) {return l.link.label === label;})
102-
.style('fill-opacity', function(d) {return d.tinyColorAlpha;});
114+
.style('fill', function(l) {return l.tinyColorHue;})
115+
.style('fill-opacity', function(l) {return l.tinyColorAlpha;});
103116
}
104117
});
105118

src/traces/sankey/render.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ function sankeyModel(layout, d, traceIndex) {
299299

300300
function linkModel(d, l, i) {
301301
var tc = tinycolor(l.color);
302+
var htc = tinycolor(l.hovercolor);
302303
var basicKey = l.source.label + '|' + l.target.label;
303304
var key = basicKey + '__' + i;
304305

@@ -314,6 +315,8 @@ function linkModel(d, l, i) {
314315
link: l,
315316
tinyColorHue: Color.tinyRGB(tc),
316317
tinyColorAlpha: tc.getAlpha(),
318+
tinyColorHoverHue: Color.tinyRGB(htc),
319+
tinyColorHoverAlpha: htc.getAlpha(),
317320
linkPath: linkPath,
318321
linkLineColor: d.linkLineColor,
319322
linkLineWidth: d.linkLineWidth,

test/jasmine/tests/sankey_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ describe('sankey tests', function() {
10871087
.filter(function(obj) {
10881088
return obj.link.label === 'stream 1';
10891089
})[0].forEach(function(l) {
1090-
expect(l.style.fillOpacity).toEqual('0.4');
1090+
expect(l.style.fillOpacity).toEqual('1.0');
10911091
});
10921092
}).then(function() {
10931093
mouseEvent('mouseout', 200, 250);

0 commit comments

Comments
 (0)