diff --git a/src/traces/bar/attributes.js b/src/traces/bar/attributes.js index a84f5efd0d7..0b0ed5582fb 100644 --- a/src/traces/bar/attributes.js +++ b/src/traces/bar/attributes.js @@ -72,9 +72,11 @@ module.exports = { '*inside* positions `text` inside, next to the bar end', '(rotated and scaled if needed).', '*outside* positions `text` outside, next to the bar end', - '(scaled if needed).', - '*auto* positions `text` inside or outside', - 'so that `text` size is maximized.' + '(scaled if needed), unless there is another bar stacked on', + 'this one, then the text gets pushed inside.', + '*auto* tries to position `text` inside the bar, but if', + 'the bar is too small and no bar is stacked on this one', + 'the text is moved outside.' ].join(' ') }, diff --git a/src/traces/bar/plot.js b/src/traces/bar/plot.js index ead3f4eb754..51c0f53b655 100644 --- a/src/traces/bar/plot.js +++ b/src/traces/bar/plot.js @@ -199,7 +199,7 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) { textHeight; if(textPosition === 'outside') { - if(!isOutmostBar) textPosition = 'inside'; + if(!isOutmostBar && !calcBar.hasB) textPosition = 'inside'; } if(textPosition === 'auto') { diff --git a/test/jasmine/tests/bar_test.js b/test/jasmine/tests/bar_test.js index 609231fffda..c080932123b 100644 --- a/test/jasmine/tests/bar_test.js +++ b/test/jasmine/tests/bar_test.js @@ -901,6 +901,78 @@ describe('A bar plot', function() { .then(done); }); + it('Pushes outside text relative bars inside when not outmost', function(done) { + var data = [{ + x: [1, 2], + y: [20, 10], + type: 'bar', + text: ['a', 'b'], + textposition: 'outside', + }, { + x: [1, 2], + y: [20, 10], + type: 'bar', + text: ['c', 'd'] + }]; + var layout = {barmode: 'relative'}; + + Plotly.plot(gd, data, layout).then(function() { + var traceNodes = getAllTraceNodes(gd), + barNodes = getAllBarNodes(traceNodes[0]), + foundTextNodes; + + for(var i = 0; i < barNodes.length; i++) { + var barNode = barNodes[i], + pathNode = barNode.querySelector('path'), + textNode = barNode.querySelector('text'); + if(textNode) { + foundTextNodes = true; + assertTextIsInsidePath(textNode, pathNode); + } + } + + expect(foundTextNodes).toBe(true); + }) + .catch(failTest) + .then(done); + }); + + it('does not push text inside when base is set', function(done) { + var data = [{ + x: [1, 2], + y: [20, 10], + base: [1, 2], + type: 'bar', + text: ['a', 'b'], + textposition: 'outside', + }, { + x: [3, 4], + y: [30, 40], + type: 'bar', + text: ['c', 'd'] + }]; + var layout = {barmode: 'relative'}; + + Plotly.plot(gd, data, layout).then(function() { + var traceNodes = getAllTraceNodes(gd), + barNodes = getAllBarNodes(traceNodes[0]), + foundTextNodes; + + for(var i = 0; i < barNodes.length; i++) { + var barNode = barNodes[i], + pathNode = barNode.querySelector('path'), + textNode = barNode.querySelector('text'); + if(textNode) { + foundTextNodes = true; + assertTextIsAbovePath(textNode, pathNode); + } + } + + expect(foundTextNodes).toBe(true); + }) + .catch(failTest) + .then(done); + }); it('should show bar texts (outside case)', function(done) { var data = [{ y: [10, -20, 30],