Skip to content

Commit ea7941b

Browse files
authored
Merge pull request #3480 from plotly/3478-fix-hovertemplate
do not delete hoverlabel when hovertemplate is defined
2 parents e94ee3d + 7bff254 commit ea7941b

File tree

6 files changed

+75
-3
lines changed

6 files changed

+75
-3
lines changed

src/components/fx/hover.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -957,8 +957,9 @@ function createHoverText(hoverData, opts, gd) {
957957
if(d.extraText !== undefined) text += (text ? '<br>' : '') + d.extraText;
958958

959959
// if 'text' is empty at this point,
960+
// and hovertemplate is not defined,
960961
// put 'name' in main label and don't show secondary label
961-
if(text === '') {
962+
if(text === '' && !d.hovertemplate) {
962963
// if 'name' is also empty, remove entire label
963964
if(name === '') g.remove();
964965
text = name;

test/jasmine/tests/pie_test.js

+1
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,7 @@ describe('pie hovering', function() {
10841084
});
10851085

10861086
it('should use hovertemplate if specified', function(done) {
1087+
mockCopy.data[0].name = '';
10871088
Plotly.plot(gd, mockCopy.data, mockCopy.layout)
10881089
.then(_hover)
10891090
.then(function() {

test/jasmine/tests/scattergeo_test.js

+11
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,17 @@ describe('Test scattergeo hover', function() {
299299
.then(done);
300300
});
301301

302+
it('should not hide hover label when hovertemplate', function(done) {
303+
Plotly.restyle(gd, {
304+
name: '',
305+
hovertemplate: 'tpl %{lat}<extra>x</extra>'
306+
}).then(function() {
307+
check([381, 221], ['tpl 10', 'x']);
308+
})
309+
.catch(failTest)
310+
.then(done);
311+
});
312+
302313
it('should generate hover label info (\'text\' single value case)', function(done) {
303314
Plotly.restyle(gd, 'text', 'text').then(function() {
304315
check([381, 221], ['(10°, 10°)\ntext', null]);

test/jasmine/tests/scattermapbox_test.js

+21
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var destroyGraphDiv = require('../assets/destroy_graph_div');
1010
var failTest = require('../assets/fail_test');
1111
var supplyAllDefaults = require('../assets/supply_defaults');
1212

13+
var assertHoverLabelContent = require('../assets/custom_assertions').assertHoverLabelContent;
1314
var mouseEvent = require('../assets/mouse_event');
1415
var click = require('../assets/click');
1516
var HOVERMINTIME = require('@src/components/fx').constants.HOVERMINTIME;
@@ -601,6 +602,15 @@ describe('@noCI scattermapbox hover', function() {
601602
};
602603
}
603604

605+
function checkHoverLabel(pos, content) {
606+
mouseEvent('mousemove', pos[0], pos[1]);
607+
608+
assertHoverLabelContent({
609+
nums: content[0],
610+
name: content[1]
611+
});
612+
}
613+
604614
it('should generate hover label info (base case)', function() {
605615
var xval = 11;
606616
var yval = 11;
@@ -788,6 +798,17 @@ describe('@noCI scattermapbox hover', function() {
788798
done();
789799
});
790800
});
801+
802+
it('should always display hoverlabel when hovertemplate is defined', function(done) {
803+
Plotly.restyle(gd, {
804+
name: '',
805+
hovertemplate: 'tpl2<extra></extra>'
806+
})
807+
.then(function() {
808+
checkHoverLabel([190, 215], ['tpl2', '']);
809+
done();
810+
});
811+
});
791812
});
792813

793814
describe('@noCI Test plotly events on a scattermapbox plot:', function() {

test/jasmine/tests/scatterpolar_test.js

+9
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ describe('Test scatterpolar hover:', function() {
117117
},
118118
nums: 'template 4.02289202968 128.342009045',
119119
name: 'Trial 3'
120+
}, {
121+
desc: 'with hovertemplate and empty trace name',
122+
patch: function(fig) {
123+
fig.data[2].hovertemplate = 'template %{r} %{theta}';
124+
fig.data[2].name = '';
125+
return fig;
126+
},
127+
nums: 'template 4.02289202968 128.342009045',
128+
name: ''
120129
}, {
121130
desc: '(no labels - out of sector)',
122131
patch: function(fig) {

test/jasmine/tests/scatterternary_test.js

+31-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ var failTest = require('../assets/fail_test');
99
var customAssertions = require('../assets/custom_assertions');
1010
var supplyAllDefaults = require('../assets/supply_defaults');
1111

12+
var mouseEvent = require('../assets/mouse_event');
13+
var assertHoverLabelContent = customAssertions.assertHoverLabelContent;
14+
1215
var assertClip = customAssertions.assertClip;
1316
var assertNodeDisplay = customAssertions.assertNodeDisplay;
1417

@@ -334,17 +337,24 @@ describe('scatterternary hover', function() {
334337

335338
var gd;
336339

340+
function check(pos, content) {
341+
mouseEvent('mousemove', pos[0], pos[1]);
342+
343+
assertHoverLabelContent({
344+
nums: content[0],
345+
name: content[1]
346+
});
347+
}
348+
337349
beforeAll(function(done) {
338350
gd = createGraphDiv();
339-
340351
var data = [{
341352
type: 'scatterternary',
342353
a: [0.1, 0.2, 0.3],
343354
b: [0.3, 0.2, 0.1],
344355
c: [0.1, 0.4, 0.5],
345356
text: ['A', 'B', 'C']
346357
}];
347-
348358
Plotly.plot(gd, data).then(done);
349359
});
350360

@@ -418,6 +428,25 @@ describe('scatterternary hover', function() {
418428
.then(done);
419429
});
420430

431+
it('should always display hoverlabel when hovertemplate is defined', function(done) {
432+
var fig = Lib.extendDeep({}, require('@mocks/ternary_simple.json'));
433+
434+
Plotly.newPlot(gd, fig)
435+
.then(function() {
436+
return Plotly.restyle(gd, {
437+
hovertemplate: '%{a}, %{b}, %{c}',
438+
name: '',
439+
text: null,
440+
hovertext: null
441+
});
442+
})
443+
.then(function() {
444+
check([380, 210], ['0.5, 0.25, 0.25']);
445+
})
446+
.catch(failTest)
447+
.then(done);
448+
});
449+
421450
});
422451

423452
describe('Test scatterternary *cliponaxis*', function() {

0 commit comments

Comments
 (0)