From 8a242b1f3d0d14957f191ffe09c74d6a5f133e71 Mon Sep 17 00:00:00 2001 From: Eivind Jahren Date: Wed, 24 Oct 2018 08:32:32 +0200 Subject: [PATCH 1/4] Do not override outside when base is set When bars are in stack or relative mode, 'outside' position should not be overriden when a base is set. --- src/traces/bar/plot.js | 2 +- test/jasmine/tests/bar_test.js | 62 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) 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..50b24c8d91b 100644 --- a/test/jasmine/tests/bar_test.js +++ b/test/jasmine/tests/bar_test.js @@ -901,6 +901,68 @@ describe('A bar plot', function() { .then(done); }); + it('Pushes outside text relative bars inside when not outmost', function(done) { + var data = [{ + x: [1, 1, 2, 2], + y: [10, 20, 20, 10], + type: 'bar', + text: ['a', 'b', 'c', 'd'], + textposition: ['outside', 'inside', 'outside', 'inside'], + }]; + 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, 1, 2, 2], + y: [10, 20, 20, 10], + base: [1, 2, 3, 4], + type: 'bar', + text: ['a', 'b', 'c', 'd'], + textposition: ['outside', 'outside', 'outside', 'outside'], + }]; + 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], From 89ae29443af50ce232b03087f13ca781e6e9c78f Mon Sep 17 00:00:00 2001 From: Eivind Jahren Date: Wed, 24 Oct 2018 15:25:30 +0200 Subject: [PATCH 2/4] Clarify outside behavior in textposition description --- src/traces/bar/attributes.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/traces/bar/attributes.js b/src/traces/bar/attributes.js index a84f5efd0d7..fb75f3bc0b8 100644 --- a/src/traces/bar/attributes.js +++ b/src/traces/bar/attributes.js @@ -72,7 +72,9 @@ 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).', + '(scaled if needed). For stacked and relative bars,', + 'where *base* is given the default value,', + 'only outmost bars are given the *outside* position.', '*auto* positions `text` inside or outside', 'so that `text` size is maximized.' ].join(' ') From a15e478fd32e6242ad2e283a007e1f8733ad8bde Mon Sep 17 00:00:00 2001 From: Eivind Jahren Date: Wed, 24 Oct 2018 15:45:39 +0200 Subject: [PATCH 3/4] Separate testdata into two traces --- test/jasmine/tests/bar_test.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/test/jasmine/tests/bar_test.js b/test/jasmine/tests/bar_test.js index 50b24c8d91b..c080932123b 100644 --- a/test/jasmine/tests/bar_test.js +++ b/test/jasmine/tests/bar_test.js @@ -903,11 +903,16 @@ describe('A bar plot', function() { it('Pushes outside text relative bars inside when not outmost', function(done) { var data = [{ - x: [1, 1, 2, 2], - y: [10, 20, 20, 10], + x: [1, 2], + y: [20, 10], type: 'bar', - text: ['a', 'b', 'c', 'd'], - textposition: ['outside', 'inside', 'outside', 'inside'], + text: ['a', 'b'], + textposition: 'outside', + }, { + x: [1, 2], + y: [20, 10], + type: 'bar', + text: ['c', 'd'] }]; var layout = {barmode: 'relative'}; @@ -934,12 +939,17 @@ describe('A bar plot', function() { it('does not push text inside when base is set', function(done) { var data = [{ - x: [1, 1, 2, 2], - y: [10, 20, 20, 10], - base: [1, 2, 3, 4], + 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: ['a', 'b', 'c', 'd'], - textposition: ['outside', 'outside', 'outside', 'outside'], + text: ['c', 'd'] }]; var layout = {barmode: 'relative'}; From 706465b05a6f34562cdaa9b398303c3979a1b059 Mon Sep 17 00:00:00 2001 From: Eivind Jahren Date: Thu, 25 Oct 2018 09:46:46 +0200 Subject: [PATCH 4/4] New description for bar textposition --- src/traces/bar/attributes.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/traces/bar/attributes.js b/src/traces/bar/attributes.js index fb75f3bc0b8..0b0ed5582fb 100644 --- a/src/traces/bar/attributes.js +++ b/src/traces/bar/attributes.js @@ -72,11 +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). For stacked and relative bars,', - 'where *base* is given the default value,', - 'only outmost bars are given the *outside* position.', - '*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(' ') },