Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 6, 2022. It is now read-only.

Commit 98377fc

Browse files
authored
Merge pull request plotly#1931 from plotly/bar-text-contraints
Bar text constraint configuration
2 parents 4e86a36 + 30bba26 commit 98377fc

File tree

6 files changed

+32
-19
lines changed

6 files changed

+32
-19
lines changed

src/traces/bar/attributes.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ 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 inside or outside a bar to be no',
90+
'larger than the bar itself.'
91+
].join(' ')
92+
},
93+
8394
orientation: {
8495
valType: 'enumerated',
8596
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: 18 additions & 19 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,25 @@ 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 scale = 1;
354+
if(constrained) {
352355
scale = (orientation === 'h') ?
353356
Math.min(1, barWidth / textBB.height) :
354357
Math.min(1, barWidth / textBB.width);
358+
}
355359

356360
// compute text and target positions
357361
var textX = (textBB.left + textBB.right) / 2,
@@ -360,14 +364,9 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
360364
targetHeight,
361365
targetX,
362366
targetY;
363-
if(rotate) {
364-
targetWidth = scale * textBB.height;
365-
targetHeight = scale * textBB.width;
366-
}
367-
else {
368-
targetWidth = scale * textBB.width;
369-
targetHeight = scale * textBB.height;
370-
}
367+
368+
targetWidth = scale * textBB.width;
369+
targetHeight = scale * textBB.height;
371370

372371
if(orientation === 'h') {
373372
if(x1 < x0) {
@@ -392,7 +391,7 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
392391
}
393392
}
394393

395-
return getTransform(textX, textY, targetX, targetY, scale, rotate);
394+
return getTransform(textX, textY, targetX, targetY, scale, false);
396395
}
397396

398397
function getTransform(textX, textY, targetX, targetY, scale, rotate) {
313 Bytes
Loading
71 Bytes
Loading

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).toBe('both');
119121
});
120122

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

0 commit comments

Comments
 (0)