Skip to content

Commit 92ffc2b

Browse files
committed
reuse lib function and add jasmine test for pie and sunburst hover falsy zero text
1 parent d66c996 commit 92ffc2b

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

src/traces/pie/calc.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var tinycolor = require('tinycolor2');
1414

1515
var Color = require('../../components/color');
1616
var helpers = require('./helpers');
17+
var isValidTextValue = require('../../lib').isValidTextValue;
1718

1819
var pieExtendedColorWays = {};
1920

@@ -99,8 +100,8 @@ function calc(gd, trace) {
99100
pt = cd[i];
100101
thisText = hasLabel ? [pt.label] : [];
101102
if(hasText) {
102-
var texti = helpers.getFirstFilled(trace.text, pt.pts);
103-
if(texti || texti === 0) thisText.push(texti);
103+
var tx = helpers.getFirstFilled(trace.text, pt.pts);
104+
if(isValidTextValue(tx)) thisText.push(tx);
104105
}
105106
if(hasValue) thisText.push(helpers.formatPieValue(pt.v, separators));
106107
if(hasPercent) thisText.push(helpers.formatPiePercent(pt.v / vTotal, separators));

src/traces/pie/plot.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ function attachFxHandlers(sliceTop, gd, cd) {
353353
pt.text = helpers.castOption(trace2.hovertext || trace2.text, pt.pts);
354354
if(hoverinfo && hoverinfo.indexOf('text') !== -1) {
355355
var tx = pt.text;
356-
if(tx || tx === 0) thisText.push(tx);
356+
if(Lib.isValidTextValue(tx)) thisText.push(tx);
357357
}
358358
pt.value = pt.v;
359359
pt.valueLabel = helpers.formatPieValue(pt.v, separators);

src/traces/sunburst/plot.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ function attachFxHandlers(sliceTop, gd, cd) {
580580
hoverPt.text = _cast('hovertext') || _cast('text');
581581
if(hasFlag('text')) {
582582
var tx = hoverPt.text;
583-
if(tx || tx === 0) thisText.push(tx);
583+
if(Lib.isValidTextValue(tx)) thisText.push(tx);
584584
}
585585

586586
Fx.loneHover({
@@ -739,7 +739,7 @@ function formatSliceLabel(pt, trace, fullLayout) {
739739

740740
if(hasFlag('text')) {
741741
var tx = Lib.castOption(trace, cdi.i, 'text');
742-
if(tx || tx === 0) thisText.push(tx);
742+
if(Lib.isValidTextValue(tx)) thisText.push(tx);
743743
}
744744

745745
return thisText.join('<br>');

test/jasmine/tests/pie_test.js

+26
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,11 @@ describe('pie hovering', function() {
998998
Lib.clearThrottle();
999999
}
10001000

1001+
function _hover2() {
1002+
mouseEvent('mouseover', 200, 250);
1003+
Lib.clearThrottle();
1004+
}
1005+
10011006
function assertLabel(content, style, msg) {
10021007
assertHoverLabelContent({nums: content}, msg);
10031008

@@ -1103,6 +1108,27 @@ describe('pie hovering', function() {
11031108
.then(done);
11041109
});
11051110

1111+
it('should show falsy zero text', function(done) {
1112+
Plotly.plot(gd, {
1113+
data: [{
1114+
type: 'pie',
1115+
labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
1116+
values: [7, 6, 5, 4, 3, 2, 1],
1117+
text: [null, '', '0', 0, 1, true, false],
1118+
textinfo: 'label+text+value'
1119+
}],
1120+
layout: {
1121+
width: 400,
1122+
height: 400
1123+
}
1124+
})
1125+
.then(_hover2)
1126+
.then(function() {
1127+
assertLabel('D\n0\n4\n14.3%');
1128+
})
1129+
.then(done);
1130+
});
1131+
11061132
it('should use hovertemplate if specified', function(done) {
11071133
mockCopy.data[0].name = '';
11081134
Plotly.plot(gd, mockCopy.data, mockCopy.layout)

test/jasmine/tests/sunburst_test.js

+22
Original file line numberDiff line numberDiff line change
@@ -1138,4 +1138,26 @@ describe('Test sunburst interactions edge cases', function() {
11381138
.catch(failTest)
11391139
.then(done);
11401140
});
1141+
1142+
it('should show falsy zero text', function(done) {
1143+
Plotly.plot(gd, {
1144+
data: [{
1145+
type: 'sunburst',
1146+
parents: ['', 'A', 'B', 'C', 'D', 'E', 'F'],
1147+
labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
1148+
values: [7, 6, 5, 4, 3, 2, 1],
1149+
text: [null, '', '0', 0, 1, true, false],
1150+
textinfo: 'label+text+value'
1151+
}],
1152+
layout: {
1153+
width: 400,
1154+
height: 400
1155+
}
1156+
})
1157+
.then(hover(gd, 4))
1158+
.then(function() {
1159+
assertHoverLabelContent({ nums: 'D\n4\n0' });
1160+
})
1161+
.then(done);
1162+
});
11411163
});

0 commit comments

Comments
 (0)