Skip to content

Commit 013c871

Browse files
committed
Add bar text constraint configuration
1 parent 969cac6 commit 013c871

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/traces/bar/attributes.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ module.exports = {
8080
description: 'Sets the font used for `text` lying outside the bar.'
8181
}),
8282

83+
constraintext: {
84+
valType: 'enumerated',
85+
values: ['inside', 'outside', 'both', 'none'],
86+
role: 'info',
87+
dflt: 'both',
88+
description: [
89+
'Constrain the size of text the bar to be no larger than the bar itself.'
90+
].join(' ')
91+
},
92+
8393
orientation: {
8494
valType: 'enumerated',
8595
role: 'info',

src/traces/bar/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
4848
var textFont = coerceFont(coerce, 'textfont', layout.font);
4949
if(hasInside) coerceFont(coerce, 'insidetextfont', textFont);
5050
if(hasOutside) coerceFont(coerce, 'outsidetextfont', textFont);
51+
coerce('constraintext');
5152
}
5253

5354
handleStyleDefaults(traceIn, traceOut, coerce, defaultColor, layout);

src/traces/bar/plot.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -237,20 +237,22 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
237237
}
238238

239239
// compute text transform
240-
var transform;
240+
var transform, constrained;
241241
if(textPosition === 'outside') {
242+
constrained = trace.constraintext === 'both' || trace.constraintext === 'outside';
242243
transform = getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB,
243-
orientation);
244+
orientation, constrained);
244245
}
245246
else {
247+
constrained = trace.constraintext === 'both' || trace.constraintext === 'inside';
246248
transform = getTransformToMoveInsideBar(x0, x1, y0, y1, textBB,
247-
orientation);
249+
orientation, constrained);
248250
}
249251

250252
textSelection.attr('transform', transform);
251253
}
252254

253-
function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
255+
function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation, constrained) {
254256
// compute text and target positions
255257
var textWidth = textBB.width,
256258
textHeight = textBB.height,
@@ -289,12 +291,12 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
289291
else if((textWidth < textHeight) === (barWidth < barHeight)) {
290292
// only scale is required
291293
rotate = false;
292-
scale = Math.min(barWidth / textWidth, barHeight / textHeight);
294+
scale = constrained ? Math.min(barWidth / textWidth, barHeight / textHeight) : 1;
293295
}
294296
else {
295297
// both scale and rotation are required
296298
rotate = true;
297-
scale = Math.min(barHeight / textWidth, barWidth / textHeight);
299+
scale = constrained ? Math.min(barHeight / textWidth, barWidth / textHeight) : 1;
298300
}
299301

300302
if(rotate) rotate = 90; // rotate clockwise
@@ -335,23 +337,26 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
335337
return getTransform(textX, textY, targetX, targetY, scale, rotate);
336338
}
337339

338-
function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
340+
function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation, constrained) {
339341
var barWidth = (orientation === 'h') ?
340342
Math.abs(y1 - y0) :
341343
Math.abs(x1 - x0),
342344
textpad;
343345

344-
// apply text padding if possible
346+
// Keep the padding so the text doesn't sit right against
347+
// the bars, but don't factor it into barWidth
345348
if(barWidth > 2 * TEXTPAD) {
346349
textpad = TEXTPAD;
347-
barWidth -= 2 * textpad;
348350
}
349351

350352
// compute rotation and scale
351-
var rotate = false,
353+
var rotate = false;
354+
var scale = 1;
355+
if(constrained) {
352356
scale = (orientation === 'h') ?
353357
Math.min(1, barWidth / textBB.height) :
354358
Math.min(1, barWidth / textBB.width);
359+
}
355360

356361
// compute text and target positions
357362
var textX = (textBB.left + textBB.right) / 2,

test/jasmine/tests/bar_test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ describe('Bar.supplyDefaults', function() {
9595
expect(traceOut.texfont).toBeUndefined();
9696
expect(traceOut.insidetexfont).toBeUndefined();
9797
expect(traceOut.outsidetexfont).toBeUndefined();
98+
expect(traceOut.constraintext).toBeUndefined();
9899
});
99100

100101
it('should default textfont to layout.font', function() {
@@ -116,6 +117,7 @@ describe('Bar.supplyDefaults', function() {
116117
expect(traceOut.insidetextfont).not.toBe(layout.font);
117118
expect(traceOut.insidetextfont).not.toBe(traceOut.textfont);
118119
expect(traceOut.outsidetexfont).toBeUndefined();
120+
expect(traceOut.constraintext).toBeUndefined();
119121
});
120122

121123
it('should inherit layout.calendar', function() {

0 commit comments

Comments
 (0)