From 96a7b12ba1bb37999ead963e4be96deb43d7481c Mon Sep 17 00:00:00 2001 From: etienne Date: Fri, 1 Jun 2018 17:59:58 -0400 Subject: [PATCH] rm bartext element (if any) in update selection ... when tx item is blank or textposition is 'none' --- src/traces/bar/plot.js | 8 +++++--- test/jasmine/tests/bar_test.js | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/traces/bar/plot.js b/src/traces/bar/plot.js index 31de45c3b29..309f6eca7c4 100644 --- a/src/traces/bar/plot.js +++ b/src/traces/bar/plot.js @@ -189,10 +189,12 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) { orientation = trace.orientation; var text = getText(trace, i); - if(!text) return; - textPosition = getTextPosition(trace, i); - if(textPosition === 'none') return; + + if(!text || textPosition === 'none') { + bar.select('text').remove(); + return; + } var textFont = getTextFont(trace, i, gd._fullLayout.font), insideTextFont = getInsideTextFont(trace, i, textFont), diff --git a/test/jasmine/tests/bar_test.js b/test/jasmine/tests/bar_test.js index 78f0a3eab20..a1c242da8c4 100644 --- a/test/jasmine/tests/bar_test.js +++ b/test/jasmine/tests/bar_test.js @@ -1294,6 +1294,43 @@ describe('A bar plot', function() { .catch(fail) .then(done); }); + + it('should be able to add/remove text node on restyle', function(done) { + function _assertNumberOfBarTextNodes(cnt) { + var sel = d3.select(gd).select('.barlayer').selectAll('text'); + expect(sel.size()).toBe(cnt); + } + + Plotly.plot(gd, [{ + type: 'bar', + x: ['Product A', 'Product B', 'Product C'], + y: [20, 14, 23], + text: [20, 14, 23], + textposition: 'auto' + }]) + .then(function() { + _assertNumberOfBarTextNodes(3); + return Plotly.restyle(gd, 'textposition', 'none'); + }) + .then(function() { + _assertNumberOfBarTextNodes(0); + return Plotly.restyle(gd, 'textposition', 'auto'); + }) + .then(function() { + _assertNumberOfBarTextNodes(3); + return Plotly.restyle(gd, 'text', [[null, 0, '']]); + }) + .then(function() { + // N.B. that '0' should be there! + _assertNumberOfBarTextNodes(1); + return Plotly.restyle(gd, 'text', 'yo!'); + }) + .then(function() { + _assertNumberOfBarTextNodes(3); + }) + .catch(fail) + .then(done); + }); }); describe('bar hover', function() {