Skip to content

Commit ac983b3

Browse files
authored
Merge pull request #6864 from TortoiseHam/pf/sankey-tracking2
Feature: Sankey Link Hover Styling (via link hovercolor attribute)
2 parents 86fb8a9 + 2c7eda1 commit ac983b3

File tree

7 files changed

+68
-10
lines changed

7 files changed

+68
-10
lines changed

draftlogs/6864_change.md

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

src/traces/sankey/attributes.js

+9
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 hover colors for',
203+
'each `link`. If `link.hovercolor` is omitted, then by default, links will become slightly more',
204+
'opaque when hovered over.'
205+
].join(' ')
206+
},
198207
customdata: {
199208
valType: 'data_array',
200209
editType: 'calc',

src/traces/sankey/calc.js

+2
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

+23-4
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,30 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
6363
handleHoverLabelDefaults(linkIn, linkOut, coerceLink, hoverlabelDefault);
6464
coerceLink('hovertemplate');
6565

66-
var defaultLinkColor = tinycolor(layout.paper_bgcolor).getLuminance() < 0.333 ?
67-
'rgba(255, 255, 255, 0.6)' :
68-
'rgba(0, 0, 0, 0.2)';
66+
var darkBG = tinycolor(layout.paper_bgcolor).getLuminance() < 0.333;
67+
var defaultLinkColor = darkBG ? 'rgba(255, 255, 255, 0.6)' : 'rgba(0, 0, 0, 0.2)';
68+
var linkColor = coerceLink('color', defaultLinkColor);
69+
70+
function makeDefaultHoverColor(_linkColor) {
71+
var tc = tinycolor(_linkColor);
72+
if(!tc.isValid()) {
73+
// hopefully the user-specified color is valid, but if not that can be caught elsewhere
74+
return _linkColor;
75+
}
76+
var alpha = tc.getAlpha();
77+
if(alpha <= 0.8) {
78+
tc.setAlpha(alpha + 0.2);
79+
} else {
80+
tc = darkBG ? tc.brighten() : tc.darken();
81+
}
82+
return tc.toRgbString();
83+
}
84+
85+
coerceLink('hovercolor', Array.isArray(linkColor) ?
86+
linkColor.map(makeDefaultHoverColor) :
87+
makeDefaultHoverColor(linkColor)
88+
);
6989

70-
coerceLink('color', Lib.repeat(defaultLinkColor, linkOut.value.length));
7190
coerceLink('customdata');
7291

7392
handleArrayContainerDefaults(linkIn, linkOut, {

src/traces/sankey/plot.js

+19-6
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

+3
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/plot-schema.json

+11
Original file line numberDiff line numberDiff line change
@@ -44190,6 +44190,17 @@
4419044190
},
4419144191
"description": "The links of the Sankey plot.",
4419244192
"editType": "calc",
44193+
"hovercolor": {
44194+
"arrayOk": true,
44195+
"description": "Sets the `link` hover color. It can be a single value, or an array for specifying hover colors for each `link`. If `link.hovercolor` is omitted, then by default, links will become slightly more opaque when hovered over.",
44196+
"editType": "calc",
44197+
"valType": "color"
44198+
},
44199+
"hovercolorsrc": {
44200+
"description": "Sets the source reference on Chart Studio Cloud for `hovercolor`.",
44201+
"editType": "none",
44202+
"valType": "string"
44203+
},
4419344204
"hoverinfo": {
4419444205
"description": "Determines which trace information appear when hovering links. If `none` or `skip` are set, no information is displayed upon hovering. But, if `none` is set, click and hover events are still fired.",
4419544206
"dflt": "all",

0 commit comments

Comments
 (0)