Skip to content

Commit 71bdd14

Browse files
committed
Fix *auto* text of zero-length bar traces
A zero-length bar kept its text hidden when textposition was *auto*, while *outside* text was drawn. Only hide text for a bar with no position or for *inside* placement.
1 parent 6b83cb1 commit 71bdd14

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎src/traces/bar/plot.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,15 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, r, overhead, op
568568
var hasB = calcBar.hasB;
569569
var barIsRounded = r && r - overhead > TEXTPAD;
570570

571+
// A null value has no bar to place text inside of or next to.
572+
// A zero-length bar keeps a position, so `auto` text can fall through to
573+
// the outside placement below.
574+
var isNullBar = !isNumeric(calcBar.s1);
575+
571576
if (
572577
!text ||
573578
textPosition === 'none' ||
574-
((calcBar.isBlank || x0 === x1 || y0 === y1) && (textPosition === 'auto' || textPosition === 'inside'))
579+
((calcBar.isBlank || x0 === x1 || y0 === y1) && (textPosition === 'inside' || isNullBar))
575580
) {
576581
bar.select('text').remove();
577582
return;

‎test/jasmine/tests/bar_test.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,30 @@ describe('A bar plot', function() {
22612261
.then(done, done.fail);
22622262
});
22632263

2264+
it('should draw *auto* text of zero-length bars outside', function(done) {
2265+
Plotly.newPlot(gd, {
2266+
data: [{
2267+
type: 'bar',
2268+
x: ['a', 'b', 'c'],
2269+
y: [0, 5, null],
2270+
text: ['ZERO', 'FIVE', 'NULLPT'],
2271+
textposition: 'auto'
2272+
}],
2273+
layout: {width: 400, height: 400, margin: {l: 0, t: 0, r: 0, b: 0}}
2274+
})
2275+
.then(function() {
2276+
var texts = [];
2277+
d3Select(gd).selectAll('.barlayer .bartext').each(function() {
2278+
texts.push(this.textContent);
2279+
});
2280+
2281+
expect(texts).toEqual(['ZERO', 'FIVE'], 'drawn bar text');
2282+
expect(d3Select(gd).select('.barlayer .bartext').attr('class'))
2283+
.toBe('bartext bartext-outside', 'zero-length bar text placement');
2284+
})
2285+
.then(done, done.fail);
2286+
});
2287+
22642288
describe('show narrow bars', function() {
22652289
['initial zoom', 'after zoom out'].forEach(function(zoomStr) {
22662290
it(zoomStr, function(done) {

0 commit comments

Comments
 (0)