Skip to content

Commit 9b5f089

Browse files
authored
Merge pull request #4179 from plotly/texttemplate-empty-string
texttemplate: accept empty text string
2 parents 46866fd + a4bce50 commit 9b5f089

File tree

5 files changed

+45
-22
lines changed

5 files changed

+45
-22
lines changed

src/traces/pie/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports.getFirstFilled = function getFirstFilled(array, indices) {
3030
if(!Array.isArray(array)) return;
3131
for(var i = 0; i < indices.length; i++) {
3232
var v = array[indices[i]];
33-
if(v || v === 0) return v;
33+
if(v || v === 0 || v === '') return v;
3434
}
3535
};
3636

src/traces/pie/plot.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ function formatSliceLabel(gd, pt, cd0) {
992992
} else {
993993
var obj = makeTemplateVariables(pt);
994994
var ptTx = helpers.getFirstFilled(trace.text, pt.pts);
995-
if(isValidTextValue(ptTx)) obj.text = ptTx;
995+
if(isValidTextValue(ptTx) || ptTx === '') obj.text = ptTx;
996996
pt.text = Lib.texttemplateString(txt, obj, gd._fullLayout._d3locale, obj, trace._meta || {});
997997
}
998998
}

src/traces/sunburst/plot.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ function formatSliceLabel(pt, trace, fullLayout) {
718718
obj.color = cdi.color;
719719
}
720720
var ptTx = Lib.castOption(trace, cdi.i, 'text');
721-
if(Lib.isValidTextValue(ptTx)) obj.text = ptTx;
721+
if(Lib.isValidTextValue(ptTx) || ptTx === '') obj.text = ptTx;
722722
obj.customdata = Lib.castOption(trace, cdi.i, 'customdata');
723723
return Lib.texttemplateString(txt, obj, fullLayout._d3locale, obj, trace._meta || {});
724724
}

test/jasmine/assets/check_texttemplate.js

+31-19
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var supplyAllDefaults = require('../assets/supply_defaults');
99

1010
'use strict';
1111

12-
module.exports = function checkTextTemplate(mock, selector, tests) {
12+
module.exports = function checkTextTemplate(mock, selector, tests, skipExtra) {
1313
var isGL = Registry.traceIs(mock[0].type, 'gl');
1414
var isPolar = Registry.traceIs(mock[0].type, 'polar');
1515
var isScatterLike = Registry.traceIs(mock[0].type, 'scatter-like');
@@ -46,30 +46,42 @@ module.exports = function checkTextTemplate(mock, selector, tests) {
4646
});
4747
}
4848

49-
var N = tests[0][1].length;
50-
var i;
49+
// Extra tests
50+
if(!skipExtra) {
51+
var N = tests[0][1].length;
52+
var i;
5153

52-
// Generate customdata
53-
var customdata = [];
54-
for(i = 0; i < N; i++) {
55-
customdata.push(Lib.randstr({}));
56-
}
57-
mock[0].customdata = customdata;
58-
tests.push(['%{customdata}', customdata]);
54+
// Generate customdata
55+
var customdata = [];
56+
for(i = 0; i < N; i++) {
57+
customdata.push(Lib.randstr({}));
58+
}
59+
mock[0].customdata = customdata;
60+
tests.push(['%{customdata}', customdata]);
5961

60-
// Generate meta
61-
mock[0].meta = {'colname': 'A'};
62-
var metaSolution = [];
63-
for(i = 0; i < N; i++) {
64-
metaSolution.push(mock[0].meta.colname);
65-
}
66-
tests.push(['%{meta.colname}', metaSolution]);
62+
// Generate meta
63+
mock[0].meta = {'colname': 'A'};
64+
var metaSolution = [];
65+
for(i = 0; i < N; i++) {
66+
metaSolution.push(mock[0].meta.colname);
67+
}
68+
tests.push(['%{meta.colname}', metaSolution]);
6769

70+
// Make sure that empty text shows up as an empty string
71+
var emptyTextMock = Lib.extendDeep([], mock);
72+
var emptyTextSolution = [];
73+
emptyTextMock[0].text = [];
74+
for(i = 0; i < N; i++) {
75+
emptyTextMock[0].text[i] = '';
76+
emptyTextSolution[i] = 'text:';
77+
}
78+
tests.push(['text:%{text}', emptyTextSolution, emptyTextMock]);
79+
}
6880
if(isGL) {
6981
tests.forEach(function(test) {
7082
it('@gl should support texttemplate', function(done) {
7183
var gd = createGraphDiv();
72-
var mockCopy = Lib.extendDeep([], mock);
84+
var mockCopy = Lib.extendDeep([], test[2] || mock);
7385
mockCopy[0].texttemplate = test[0];
7486
Plotly.newPlot(gd, mockCopy)
7587
.then(function() {
@@ -101,7 +113,7 @@ module.exports = function checkTextTemplate(mock, selector, tests) {
101113
tests.forEach(function(test) {
102114
it('should support texttemplate', function(done) {
103115
var gd = createGraphDiv();
104-
var mockCopy = Lib.extendDeep([], mock);
116+
var mockCopy = Lib.extendDeep([], test[2] || mock);
105117
mockCopy[0].texttemplate = test[0];
106118
Plotly.newPlot(gd, mockCopy)
107119
.then(function() {

test/jasmine/tests/pie_test.js

+11
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,17 @@ describe('Pie traces', function() {
876876
[['%{label} - %{value}', '%{text}', '%{value}', '%{percent}'], ['A - 1', 'textB', '3', '18.2%']],
877877
['%{text}-%{color}', ['textA-#d62728', 'textB-#1f77b4', 'textC-#ff7f0e', 'textD-#2ca02c']]
878878
]);
879+
880+
// Check texttemplate with aggregated values
881+
checkTextTemplate([{
882+
type: 'pie',
883+
values: [1, 1, 1],
884+
labels: ['A', 'A', 'B'],
885+
text: ['textA1', 'textA2', 'textB'],
886+
textposition: 'inside'
887+
}], 'g.slicetext', [
888+
['%{text}', ['textA1', 'textB']]
889+
], true);
879890
});
880891

881892
describe('pie hovering', function() {

0 commit comments

Comments
 (0)