Skip to content

Bar text constraint configuration #1931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 5, 2017
Merged
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
11 changes: 11 additions & 0 deletions src/traces/bar/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ module.exports = {
description: 'Sets the font used for `text` lying outside the bar.'
}),

constraintext: {
valType: 'enumerated',
values: ['inside', 'outside', 'both', 'none'],
role: 'info',
dflt: 'both',
description: [
'Constrain the size of text inside or outside a bar to be no',
'larger than the bar itself.'
].join(' ')
},

orientation: {
valType: 'enumerated',
role: 'info',
Expand Down
1 change: 1 addition & 0 deletions src/traces/bar/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
var textFont = coerceFont(coerce, 'textfont', layout.font);
if(hasInside) coerceFont(coerce, 'insidetextfont', textFont);
if(hasOutside) coerceFont(coerce, 'outsidetextfont', textFont);
coerce('constraintext');
}

handleStyleDefaults(traceIn, traceOut, coerce, defaultColor, layout);
Expand Down
37 changes: 18 additions & 19 deletions src/traces/bar/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,22 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
}

// compute text transform
var transform;
var transform, constrained;
if(textPosition === 'outside') {
constrained = trace.constraintext === 'both' || trace.constraintext === 'outside';
transform = getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB,
orientation);
orientation, constrained);
}
else {
constrained = trace.constraintext === 'both' || trace.constraintext === 'inside';
transform = getTransformToMoveInsideBar(x0, x1, y0, y1, textBB,
orientation);
orientation, constrained);
}

textSelection.attr('transform', transform);
}

function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation, constrained) {
// compute text and target positions
var textWidth = textBB.width,
textHeight = textBB.height,
Expand Down Expand Up @@ -289,12 +291,12 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
else if((textWidth < textHeight) === (barWidth < barHeight)) {
// only scale is required
rotate = false;
scale = Math.min(barWidth / textWidth, barHeight / textHeight);
scale = constrained ? Math.min(barWidth / textWidth, barHeight / textHeight) : 1;
}
else {
// both scale and rotation are required
rotate = true;
scale = Math.min(barHeight / textWidth, barWidth / textHeight);
scale = constrained ? Math.min(barHeight / textWidth, barWidth / textHeight) : 1;
}

if(rotate) rotate = 90; // rotate clockwise
Expand Down Expand Up @@ -335,23 +337,25 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
return getTransform(textX, textY, targetX, targetY, scale, rotate);
}

function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation, constrained) {
var barWidth = (orientation === 'h') ?
Math.abs(y1 - y0) :
Math.abs(x1 - x0),
textpad;

// apply text padding if possible
// Keep the padding so the text doesn't sit right against
// the bars, but don't factor it into barWidth
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good call.

if(barWidth > 2 * TEXTPAD) {
textpad = TEXTPAD;
barWidth -= 2 * textpad;
}

// compute rotation and scale
var rotate = false,
var scale = 1;
if(constrained) {
scale = (orientation === 'h') ?
Math.min(1, barWidth / textBB.height) :
Math.min(1, barWidth / textBB.width);
}

// compute text and target positions
var textX = (textBB.left + textBB.right) / 2,
Expand All @@ -360,14 +364,9 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
targetHeight,
targetX,
targetY;
if(rotate) {
targetWidth = scale * textBB.height;
targetHeight = scale * textBB.width;
}
else {
targetWidth = scale * textBB.width;
targetHeight = scale * textBB.height;
}

targetWidth = scale * textBB.width;
targetHeight = scale * textBB.height;

if(orientation === 'h') {
if(x1 < x0) {
Expand All @@ -392,7 +391,7 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
}
}

return getTransform(textX, textY, targetX, targetY, scale, rotate);
return getTransform(textX, textY, targetX, targetY, scale, false);
}

function getTransform(textX, textY, targetX, targetY, scale, rotate) {
Expand Down
Binary file modified test/image/baselines/bar_attrs_group_norm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/bar_attrs_relative.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions test/jasmine/tests/bar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ describe('Bar.supplyDefaults', function() {
expect(traceOut.texfont).toBeUndefined();
expect(traceOut.insidetexfont).toBeUndefined();
expect(traceOut.outsidetexfont).toBeUndefined();
expect(traceOut.constraintext).toBeUndefined();
});

it('should default textfont to layout.font', function() {
Expand All @@ -116,6 +117,7 @@ describe('Bar.supplyDefaults', function() {
expect(traceOut.insidetextfont).not.toBe(layout.font);
expect(traceOut.insidetextfont).not.toBe(traceOut.textfont);
expect(traceOut.outsidetexfont).toBeUndefined();
expect(traceOut.constraintext).toBe('both');
});

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