Skip to content

Commit e8d300b

Browse files
authored
Merge pull request #3715 from plotly/bar-textfont-wo-fix
Don't try to access textfont when bar has no text
2 parents e8b9009 + e975e99 commit e8d300b

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/traces/bar/plot.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,6 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
205205
var text = getText(trace, i);
206206
textPosition = getTextPosition(trace, i);
207207

208-
var layoutFont = fullLayout.font;
209-
var barColor = style.getBarColor(calcTrace[i], trace);
210-
var insideTextFont = style.getInsideTextFont(trace, i, layoutFont, barColor);
211-
var outsideTextFont = style.getOutsideTextFont(trace, i, layoutFont);
212-
213208
// compute text position
214209
var prefix = trace.type === 'waterfall' ? 'waterfall' : 'bar';
215210
var barmode = fullLayout[prefix + 'mode'];
@@ -218,16 +213,21 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
218213
var calcBar = calcTrace[i];
219214
var isOutmostBar = !inStackOrRelativeMode || calcBar._outmost;
220215

221-
// padding excluded
222-
var barWidth = Math.abs(x1 - x0) - 2 * TEXTPAD;
223-
var barHeight = Math.abs(y1 - y0) - 2 * TEXTPAD;
224-
225216
if(!text || textPosition === 'none' ||
226217
(calcBar.isBlank && (textPosition === 'auto' || textPosition === 'inside'))) {
227218
bar.select('text').remove();
228219
return;
229220
}
230221

222+
var layoutFont = fullLayout.font;
223+
var barColor = style.getBarColor(calcTrace[i], trace);
224+
var insideTextFont = style.getInsideTextFont(trace, i, layoutFont, barColor);
225+
var outsideTextFont = style.getOutsideTextFont(trace, i, layoutFont);
226+
227+
// padding excluded
228+
var barWidth = Math.abs(x1 - x0) - 2 * TEXTPAD;
229+
var barHeight = Math.abs(y1 - y0) - 2 * TEXTPAD;
230+
231231
var textSelection;
232232
var textBB;
233233
var textWidth;

test/jasmine/tests/bar_test.js

+14
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,20 @@ describe('A bar plot', function() {
18861886
.catch(failTest)
18871887
.then(done);
18881888
});
1889+
1890+
it('should not error out when *textfont* is set in traces w/o *text*', function(done) {
1891+
Plotly.plot(gd, [{
1892+
type: 'bar',
1893+
x: ['A', 'K', 'M', 'O', 'Q', 'S', 'T', 'V', 'X', 'Z', 'D', 'F', 'H'],
1894+
y: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25],
1895+
textfont: {color: 'red'}
1896+
}])
1897+
.then(function() {
1898+
expect(getAllBarNodes(gd).length).toBe(13, '# of bars');
1899+
})
1900+
.catch(failTest)
1901+
.then(done);
1902+
});
18891903
});
18901904

18911905
describe('bar visibility toggling:', function() {

0 commit comments

Comments
 (0)