Skip to content

Commit a7b76ee

Browse files
committed
add 'hovertext' to scatter3d traces
1 parent 7b6911f commit a7b76ee

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/traces/scatter3d/attributes.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,22 @@ module.exports = {
7272
'If a single string, the same string appears over',
7373
'all the data points.',
7474
'If an array of string, the items are mapped in order to the',
75-
'this trace\'s (x,y,z) coordinates.'
75+
'this trace\'s (x,y,z) coordinates.',
76+
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
77+
'these elements will be seen in the hover labels.'
7678
].join(' ')
7779
}),
80+
hovertext: extendFlat({}, scatterAttrs.hovertext, {
81+
description: [
82+
'Sets text elements associated with each (x,y,z) triplet.',
83+
'If a single string, the same string appears over',
84+
'all the data points.',
85+
'If an array of string, the items are mapped in order to the',
86+
'this trace\'s (x,y,z) coordinates.',
87+
'To be seen, trace `hoverinfo` must contain a *text* flag.'
88+
].join(' ')
89+
}),
90+
7891
mode: extendFlat({}, scatterAttrs.mode, // shouldn't this be on-par with 2D?
7992
{dflt: 'lines+markers'}),
8093
surfaceaxis: {

src/traces/scatter3d/convert.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ proto.handlePick = function(selection) {
5858
selection.object = this.scatterPlot;
5959
this.scatterPlot.highlight(selection.data);
6060
}
61-
if(this.textLabels && this.textLabels[selection.data.index] !== undefined) {
62-
selection.textLabel = this.textLabels[selection.data.index];
61+
if(this.textLabels) {
62+
if(this.textLabels[selection.data.index] !== undefined) {
63+
selection.textLabel = this.textLabels[selection.data.index];
64+
} else {
65+
selection.textLabel = this.textLabels;
66+
}
6367
}
6468
else selection.textLabel = '';
6569

@@ -371,7 +375,7 @@ proto.update = function(data) {
371375
opacity: data.opacity
372376
};
373377

374-
this.textLabels = options.text;
378+
this.textLabels = data.hovertext || data.text;
375379

376380
if(this.mode.indexOf('text') !== -1) {
377381
if(this.textMarkers) this.textMarkers.update(textOptions);

src/traces/scatter3d/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3434
}
3535

3636
coerce('text');
37+
coerce('hovertext');
3738
coerce('mode');
3839

3940
if(subTypes.hasLines(traceOut)) {

test/jasmine/tests/gl_plot_interact_test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@ describe('Test gl3d plots', function() {
4646
mouseEvent(type, 605, 271, opts);
4747
}
4848

49-
function assertHoverText(xLabel, yLabel, zLabel) {
49+
function assertHoverText(xLabel, yLabel, zLabel, textLabel) {
5050
var node = d3.selectAll('g.hovertext');
5151
expect(node.size()).toEqual(1, 'hover text group');
5252

5353
var tspan = d3.selectAll('g.hovertext').selectAll('tspan')[0];
5454
expect(tspan[0].innerHTML).toEqual(xLabel, 'x val');
5555
expect(tspan[1].innerHTML).toEqual(yLabel, 'y val');
5656
expect(tspan[2].innerHTML).toEqual(zLabel, 'z val');
57+
58+
if(textLabel) {
59+
expect(tspan[3].innerHTML).toEqual(textLabel, 'text label');
60+
}
5761
}
5862

5963
function assertEventData(x, y, z, curveNumber, pointNumber) {
@@ -132,6 +136,18 @@ describe('Test gl3d plots', function() {
132136
.then(_hover)
133137
.then(function() {
134138
assertHoverText('x: 二 6, 2017', 'y: c', 'z: 100k');
139+
140+
return Plotly.restyle(gd, 'text', [['A', 'B', 'C', 'D']]);
141+
})
142+
.then(_hover)
143+
.then(function() {
144+
assertHoverText('x: 二 6, 2017', 'y: c', 'z: 100k', 'C');
145+
146+
return Plotly.restyle(gd, 'hovertext', [['Apple', 'Banana', 'Clementine', 'Dragon fruit']]);
147+
})
148+
.then(_hover)
149+
.then(function() {
150+
assertHoverText('x: 二 6, 2017', 'y: c', 'z: 100k', 'Clementine');
135151
})
136152
.then(done);
137153

0 commit comments

Comments
 (0)