Skip to content

Commit

Permalink
Merge pull request #4179 from plotly/texttemplate-empty-string
Browse files Browse the repository at this point in the history
texttemplate: accept empty text string
  • Loading branch information
antoinerg committed Sep 10, 2019
2 parents 46866fd + a4bce50 commit 9b5f089
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/traces/pie/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ exports.getFirstFilled = function getFirstFilled(array, indices) {
if(!Array.isArray(array)) return;
for(var i = 0; i < indices.length; i++) {
var v = array[indices[i]];
if(v || v === 0) return v;
if(v || v === 0 || v === '') return v;
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/traces/pie/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ function formatSliceLabel(gd, pt, cd0) {
} else {
var obj = makeTemplateVariables(pt);
var ptTx = helpers.getFirstFilled(trace.text, pt.pts);
if(isValidTextValue(ptTx)) obj.text = ptTx;
if(isValidTextValue(ptTx) || ptTx === '') obj.text = ptTx;
pt.text = Lib.texttemplateString(txt, obj, gd._fullLayout._d3locale, obj, trace._meta || {});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/traces/sunburst/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ function formatSliceLabel(pt, trace, fullLayout) {
obj.color = cdi.color;
}
var ptTx = Lib.castOption(trace, cdi.i, 'text');
if(Lib.isValidTextValue(ptTx)) obj.text = ptTx;
if(Lib.isValidTextValue(ptTx) || ptTx === '') obj.text = ptTx;
obj.customdata = Lib.castOption(trace, cdi.i, 'customdata');
return Lib.texttemplateString(txt, obj, fullLayout._d3locale, obj, trace._meta || {});
}
Expand Down
50 changes: 31 additions & 19 deletions test/jasmine/assets/check_texttemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var supplyAllDefaults = require('../assets/supply_defaults');

'use strict';

module.exports = function checkTextTemplate(mock, selector, tests) {
module.exports = function checkTextTemplate(mock, selector, tests, skipExtra) {
var isGL = Registry.traceIs(mock[0].type, 'gl');
var isPolar = Registry.traceIs(mock[0].type, 'polar');
var isScatterLike = Registry.traceIs(mock[0].type, 'scatter-like');
Expand Down Expand Up @@ -46,30 +46,42 @@ module.exports = function checkTextTemplate(mock, selector, tests) {
});
}

var N = tests[0][1].length;
var i;
// Extra tests
if(!skipExtra) {
var N = tests[0][1].length;
var i;

// Generate customdata
var customdata = [];
for(i = 0; i < N; i++) {
customdata.push(Lib.randstr({}));
}
mock[0].customdata = customdata;
tests.push(['%{customdata}', customdata]);
// Generate customdata
var customdata = [];
for(i = 0; i < N; i++) {
customdata.push(Lib.randstr({}));
}
mock[0].customdata = customdata;
tests.push(['%{customdata}', customdata]);

// Generate meta
mock[0].meta = {'colname': 'A'};
var metaSolution = [];
for(i = 0; i < N; i++) {
metaSolution.push(mock[0].meta.colname);
}
tests.push(['%{meta.colname}', metaSolution]);
// Generate meta
mock[0].meta = {'colname': 'A'};
var metaSolution = [];
for(i = 0; i < N; i++) {
metaSolution.push(mock[0].meta.colname);
}
tests.push(['%{meta.colname}', metaSolution]);

// Make sure that empty text shows up as an empty string
var emptyTextMock = Lib.extendDeep([], mock);
var emptyTextSolution = [];
emptyTextMock[0].text = [];
for(i = 0; i < N; i++) {
emptyTextMock[0].text[i] = '';
emptyTextSolution[i] = 'text:';
}
tests.push(['text:%{text}', emptyTextSolution, emptyTextMock]);
}
if(isGL) {
tests.forEach(function(test) {
it('@gl should support texttemplate', function(done) {
var gd = createGraphDiv();
var mockCopy = Lib.extendDeep([], mock);
var mockCopy = Lib.extendDeep([], test[2] || mock);
mockCopy[0].texttemplate = test[0];
Plotly.newPlot(gd, mockCopy)
.then(function() {
Expand Down Expand Up @@ -101,7 +113,7 @@ module.exports = function checkTextTemplate(mock, selector, tests) {
tests.forEach(function(test) {
it('should support texttemplate', function(done) {
var gd = createGraphDiv();
var mockCopy = Lib.extendDeep([], mock);
var mockCopy = Lib.extendDeep([], test[2] || mock);
mockCopy[0].texttemplate = test[0];
Plotly.newPlot(gd, mockCopy)
.then(function() {
Expand Down
11 changes: 11 additions & 0 deletions test/jasmine/tests/pie_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,17 @@ describe('Pie traces', function() {
[['%{label} - %{value}', '%{text}', '%{value}', '%{percent}'], ['A - 1', 'textB', '3', '18.2%']],
['%{text}-%{color}', ['textA-#d62728', 'textB-#1f77b4', 'textC-#ff7f0e', 'textD-#2ca02c']]
]);

// Check texttemplate with aggregated values
checkTextTemplate([{
type: 'pie',
values: [1, 1, 1],
labels: ['A', 'A', 'B'],
text: ['textA1', 'textA2', 'textB'],
textposition: 'inside'
}], 'g.slicetext', [
['%{text}', ['textA1', 'textB']]
], true);
});

describe('pie hovering', function() {
Expand Down

0 comments on commit 9b5f089

Please sign in to comment.