Skip to content

Commit 6bbfa65

Browse files
committed
add support for custom hover label in gl3d
1 parent 7332d23 commit 6bbfa65

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/plots/gl3d/scene.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ function render(scene) {
6262
return Axes.tickText(axis, axis.d2l(val), 'hover').text;
6363
}
6464

65+
function castHoverOption(attr, ptNumber) {
66+
var labelOpts = trace.hoverlabel || {};
67+
var val = Lib.nestedProperty(labelOpts, attr).get();
68+
69+
if(Array.isArray(val)) {
70+
if(Array.isArray(ptNumber) && Array.isArray(val[ptNumber[0]])) {
71+
return val[ptNumber[0]][ptNumber[1]];
72+
} else {
73+
return val[ptNumber];
74+
}
75+
} else {
76+
return val;
77+
}
78+
}
79+
6580
var oldEventData;
6681

6782
if(lastPicked !== null) {
@@ -92,7 +107,11 @@ function render(scene) {
92107
zLabel: zVal,
93108
text: selection.textLabel,
94109
name: lastPicked.name,
95-
color: lastPicked.color
110+
color: castHoverOption('bgcolor', ptNumber) || lastPicked.color,
111+
borderColor: castHoverOption('bordercolor', ptNumber),
112+
fontFamily: castHoverOption('font.family', ptNumber),
113+
fontSize: castHoverOption('font.size', ptNumber),
114+
fontColor: castHoverOption('font.color', ptNumber)
96115
}, {
97116
container: svgContainer
98117
});

test/jasmine/tests/gl_plot_interact_test.js

+49-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ describe('Test gl3d plots', function() {
5858
}
5959
}
6060

61+
function assertHoverLabelStyle(bgColor, borderColor, fontSize, fontFamily, fontColor) {
62+
var node = d3.selectAll('g.hovertext');
63+
64+
var path = node.select('path');
65+
expect(path.style('fill')).toEqual(bgColor, 'bgcolor');
66+
expect(path.style('stroke')).toEqual(borderColor, 'bordercolor');
67+
68+
var text = node.select('text');
69+
expect(parseInt(text.style('font-size'))).toEqual(fontSize, 'font.size');
70+
expect(text.style('font-family').split(',')[0]).toEqual(fontFamily, 'font.family');
71+
expect(text.style('fill')).toEqual(fontColor, 'font.color');
72+
}
73+
6174
function assertEventData(x, y, z, curveNumber, pointNumber) {
6275
expect(Object.keys(ptData)).toEqual([
6376
'x', 'y', 'z',
@@ -101,9 +114,10 @@ describe('Test gl3d plots', function() {
101114
.then(function() {
102115
assertHoverText('x: 140.72', 'y: −96.97', 'z: −96.97');
103116
assertEventData(140.72, -96.97, -96.97, 0, 2);
117+
assertHoverLabelStyle('rgb(0, 0, 255)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)');
104118

105119
return Plotly.restyle(gd, {
106-
x: [['2016-01-11', '2016-01-12', '2017-01-01', '2017-02']]
120+
x: [['2016-01-11', '2016-01-12', '2017-01-01', '2017-02-01']]
107121
});
108122
})
109123
.then(_hover)
@@ -146,6 +160,25 @@ describe('Test gl3d plots', function() {
146160
.then(_hover)
147161
.then(function() {
148162
assertHoverText('x: 二 6, 2017', 'y: c', 'z: 100k', 'Clementine');
163+
164+
return Plotly.restyle(gd, {
165+
'hoverlabel.bgcolor': [['red', 'blue', 'green', 'yellow']],
166+
'hoverlabel.font.size': 20
167+
});
168+
})
169+
.then(_hover)
170+
.then(function() {
171+
assertHoverLabelStyle('rgb(0, 128, 0)', 'rgb(255, 255, 255)', 20, 'Arial', 'rgb(255, 255, 255)');
172+
173+
return Plotly.relayout(gd, {
174+
'hoverlabel.bordercolor': 'yellow',
175+
'hoverlabel.font.color': 'cyan',
176+
'hoverlabel.font.family': 'Roboto'
177+
});
178+
})
179+
.then(_hover)
180+
.then(function() {
181+
assertHoverLabelStyle('rgb(0, 128, 0)', 'rgb(255, 255, 0)', 20, 'Roboto', 'rgb(0, 255, 255)');
149182
})
150183
.then(done);
151184
});
@@ -170,6 +203,21 @@ describe('Test gl3d plots', function() {
170203
.then(function() {
171204
assertHoverText('x: 1', 'y: 2', 'z: 43', 'one two');
172205
assertEventData(1, 2, 43, 0, [1, 2]);
206+
assertHoverLabelStyle('rgb(68, 68, 68)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)');
207+
208+
Plotly.restyle(gd, {
209+
'hoverlabel.bgcolor': 'white',
210+
'hoverlabel.font.size': 9,
211+
'hoverlabel.font.color': [[
212+
['red', 'blue', 'green'],
213+
['pink', 'purple', 'cyan'],
214+
['black', 'orange', 'yellow']
215+
]]
216+
});
217+
})
218+
.then(_hover)
219+
.then(function() {
220+
assertHoverLabelStyle('rgb(255, 255, 255)', 'rgb(68, 68, 68)', 9, 'Arial', 'rgb(0, 255, 255)');
173221
})
174222
.then(done);
175223
});

0 commit comments

Comments
 (0)