Skip to content
Open
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
1 change: 1 addition & 0 deletions draftlogs/8078_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix `textposition: 'auto'` text of zero-length `bar` traces [[#8078](https://github.com/plotly/plotly.js/pull/8078)]
7 changes: 6 additions & 1 deletion src/traces/bar/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,15 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, r, overhead, op
var hasB = calcBar.hasB;
var barIsRounded = r && r - overhead > TEXTPAD;

// A null value has no bar to place text inside of or next to.
// A zero-length bar keeps a position, so `auto` text can fall through to
// the outside placement below.
var isNullBar = !isNumeric(calcBar.s1);

if (
!text ||
textPosition === 'none' ||
((calcBar.isBlank || x0 === x1 || y0 === y1) && (textPosition === 'auto' || textPosition === 'inside'))
((calcBar.isBlank || x0 === x1 || y0 === y1) && (textPosition === 'inside' || isNullBar))
) {
bar.select('text').remove();
return;
Expand Down
24 changes: 24 additions & 0 deletions test/jasmine/tests/bar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,30 @@ describe('A bar plot', function() {
.then(done, done.fail);
});

it('should draw *auto* text of zero-length bars outside', function(done) {
Plotly.newPlot(gd, {
data: [{
type: 'bar',
x: ['a', 'b', 'c'],
y: [0, 5, null],
text: ['ZERO', 'FIVE', 'NULLPT'],
textposition: 'auto'
}],
layout: {width: 400, height: 400, margin: {l: 0, t: 0, r: 0, b: 0}}
})
.then(function() {
var texts = [];
d3Select(gd).selectAll('.barlayer .bartext').each(function() {
texts.push(this.textContent);
});

expect(texts).toEqual(['ZERO', 'FIVE'], 'drawn bar text');
expect(d3Select(gd).select('.barlayer .bartext').attr('class'))
.toBe('bartext bartext-outside', 'zero-length bar text placement');
})
.then(done, done.fail);
});

describe('show narrow bars', function() {
['initial zoom', 'after zoom out'].forEach(function(zoomStr) {
it(zoomStr, function(done) {
Expand Down