Skip to content

Commit 864f941

Browse files
committed
do not use hoverlabel.bgcolor for "name" part of hover label
- this makes the trace name visible with e.g. hoverlabel.bgcolor: 'white'
1 parent 45e7a3d commit 864f941

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

src/components/fx/hover.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -796,12 +796,20 @@ function createHoverText(hoverData, opts, gd) {
796796
var name = '';
797797
var text = '';
798798

799-
// combine possible non-opaque trace color with bgColor
800-
var baseColor = Color.opacity(d.color) ? d.color : Color.defaultLine;
801-
var traceColor = Color.combine(baseColor, bgColor);
802-
799+
// combine possible non-opaque trace color with bgColor
800+
var color0 = d.bgcolor || d.color;
801+
// color for 'nums' part of the label
802+
var numsColor = Color.combine(
803+
Color.opacity(color0) ? color0 : Color.defaultLine,
804+
bgColor
805+
);
806+
// color for 'name' part of the label
807+
var nameColor = Color.combine(
808+
Color.opacity(d.color) ? d.color : Color.defaultLine,
809+
bgColor
810+
);
803811
// find a contrasting color for border and text
804-
var contrastColor = d.borderColor || Color.contrast(traceColor);
812+
var contrastColor = d.borderColor || Color.contrast(numsColor);
805813

806814
// to get custom 'name' labels pass cleanPoint
807815
if(d.nameOverride !== undefined) d.name = d.nameOverride;
@@ -870,7 +878,7 @@ function createHoverText(hoverData, opts, gd) {
870878
tx2.call(Drawing.font,
871879
d.fontFamily || fontFamily,
872880
d.fontSize || fontSize,
873-
traceColor)
881+
nameColor)
874882
.text(name)
875883
.attr('data-notex', 1)
876884
.call(svgTextUtils.positionText, 0, 0)
@@ -884,7 +892,7 @@ function createHoverText(hoverData, opts, gd) {
884892

885893
g.select('path')
886894
.style({
887-
fill: traceColor,
895+
fill: numsColor,
888896
stroke: contrastColor
889897
});
890898
var tbb = tx.node().getBoundingClientRect();
@@ -1190,7 +1198,7 @@ function cleanPoint(d, hovermode) {
11901198
}
11911199

11921200
fill('hoverinfo', 'hi', 'hoverinfo');
1193-
fill('color', 'hbg', 'hoverlabel.bgcolor');
1201+
fill('bgcolor', 'hbg', 'hoverlabel.bgcolor');
11941202
fill('borderColor', 'hbc', 'hoverlabel.bordercolor');
11951203
fill('fontFamily', 'htf', 'hoverlabel.font.family');
11961204
fill('fontSize', 'hts', 'hoverlabel.font.size');

test/jasmine/tests/hover_label_test.js

+66
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,72 @@ describe('Test hover label custom styling:', function() {
22032203
.then(done);
22042204
});
22052205

2206+
it('should work for x/y cartesian traces (multi-trace case)', function(done) {
2207+
var gd = createGraphDiv();
2208+
2209+
function assertNameLabel(expectation) {
2210+
var g = d3.selectAll('g.hovertext > text.name');
2211+
2212+
if(expectation === null) {
2213+
expect(g.size()).toBe(0);
2214+
} else {
2215+
g.each(function(_, i) {
2216+
var textStyle = window.getComputedStyle(this);
2217+
expect(textStyle.fill).toBe(expectation.color[i]);
2218+
});
2219+
}
2220+
}
2221+
2222+
Plotly.plot(gd, [{
2223+
x: [1, 2, 3],
2224+
y: [1, 2, 1],
2225+
}, {
2226+
x: [1, 2, 3],
2227+
y: [4, 5, 4],
2228+
}], {
2229+
hovermode: 'x',
2230+
})
2231+
.then(function() {
2232+
_hover(gd, { xval: gd._fullData[0].x[0] });
2233+
assertNameLabel({
2234+
color: ['rgb(31, 119, 180)', 'rgb(255, 127, 14)']
2235+
});
2236+
return Plotly.restyle(gd, 'marker.color', ['red', 'blue']);
2237+
})
2238+
.then(function() {
2239+
_hover(gd, { xval: gd._fullData[0].x[0] });
2240+
assertNameLabel({
2241+
color: ['rgb(255, 0, 0)', 'rgb(0, 0, 255)']
2242+
});
2243+
return Plotly.relayout(gd, 'hoverlabel.bgcolor', 'white');
2244+
})
2245+
.then(function() {
2246+
_hover(gd, { xval: gd._fullData[0].x[0] });
2247+
// should not affect the name font color
2248+
assertNameLabel({
2249+
color: ['rgb(255, 0, 0)', 'rgb(0, 0, 255)']
2250+
});
2251+
return Plotly.restyle(gd, 'marker.color', ['rgba(255,0,0,0.1)', 'rgba(0,0,255,0.1)']);
2252+
})
2253+
.then(function() {
2254+
_hover(gd, { xval: gd._fullData[0].x[0] });
2255+
// should blend with plot_bgcolor
2256+
assertNameLabel({
2257+
color: ['rgb(255, 179, 179)', 'rgb(179, 179, 255)']
2258+
});
2259+
return Plotly.restyle(gd, 'marker.color', ['rgba(255,0,0,0)', 'rgba(0,0,255,0)']);
2260+
})
2261+
.then(function() {
2262+
_hover(gd, { xval: gd._fullData[0].x[0] });
2263+
// uses default line color when opacity=0
2264+
assertNameLabel({
2265+
color: ['rgb(68, 68, 68)', 'rgb(68, 68, 68)']
2266+
});
2267+
})
2268+
.catch(failTest)
2269+
.then(done);
2270+
});
2271+
22062272
it('should work for 2d z cartesian traces', function(done) {
22072273
var gd = createGraphDiv();
22082274

0 commit comments

Comments
 (0)