Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

texttemplate: accept empty text string #4179

Merged
merged 4 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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