Skip to content
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

Add label alignment option to axis label title #6521

Merged
merged 11 commits into from
Oct 27, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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 docs/axes/labelling.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The scale label configuration is nested under the scale configuration in the `sc
| Name | Type | Default | Description
| ---- | ---- | ------- | -----------
| `display` | `boolean` | `false` | If true, display the axis title.
| `align` | `string` | `'center'` | Alignment of the axis title. Possible options are `'start'`, `'center'` and `'end'`
| `labelString` | `string` | `''` | The text for the title. (i.e. "# of People" or "Response Choices").
| `lineHeight` | <code>number&#124;string</code> | `1.2` | Height of an individual line of text (see [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/line-height)).
| `fontColor` | `Color` | `'#666'` | Font color for scale title.
Expand Down
41 changes: 34 additions & 7 deletions src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -1205,28 +1205,55 @@ var Scale = Element.extend({
var scaleLabelFont = helpers.options._parseFont(scaleLabel);
var scaleLabelPadding = helpers.options.toPadding(scaleLabel.padding);
var halfLineHeight = scaleLabelFont.lineHeight / 2;
var scaleLabelAlign = scaleLabel.align;
var position = options.position;
var rotation = 0;
var scaleLabelX, scaleLabelY;
var scaleLabelX, scaleLabelY, textAlign;
var isReverse = me.options.ticks.reverse;
el marked this conversation as resolved.
Show resolved Hide resolved

if (me.isHorizontal()) {
scaleLabelX = me.left + me.width / 2; // midpoint of the width
scaleLabelY = position === 'bottom'
? me.bottom - halfLineHeight - scaleLabelPadding.bottom
: me.top + halfLineHeight + scaleLabelPadding.top;
switch (scaleLabelAlign) {
case 'start':
scaleLabelX = me.left;
el marked this conversation as resolved.
Show resolved Hide resolved
textAlign = isReverse ? 'right' : 'left';
break;
case 'end':
scaleLabelX = me.left + me.width;
el marked this conversation as resolved.
Show resolved Hide resolved
textAlign = isReverse ? 'left' : 'right';
break;
default:
scaleLabelX = me.left + me.width / 2;
textAlign = 'center';
}
scaleLabelY = position === 'top'
? me.top + halfLineHeight + scaleLabelPadding.top
: me.bottom - halfLineHeight - scaleLabelPadding.bottom;
} else {
var isLeft = position === 'left';
var startsAtBottom = !isReverse;
scaleLabelX = isLeft
? me.left + halfLineHeight + scaleLabelPadding.top
: me.right - halfLineHeight - scaleLabelPadding.top;
scaleLabelY = me.top + me.height / 2;
switch (scaleLabelAlign) {
case 'start':
scaleLabelY = me.top + (startsAtBottom ? me.height : 0);
textAlign = (startsAtBottom ^ !isLeft) ? 'left' : 'right';
el marked this conversation as resolved.
Show resolved Hide resolved
break;
case 'end':
scaleLabelY = me.top + (startsAtBottom ? 0 : me.height);
textAlign = (startsAtBottom ^ !isLeft) ? 'right' : 'left';
break;
default:
scaleLabelY = me.top + me.height / 2;
textAlign = 'center';
}
rotation = isLeft ? -0.5 * Math.PI : 0.5 * Math.PI;
}

ctx.save();
ctx.translate(scaleLabelX, scaleLabelY);
ctx.rotate(rotation);
ctx.textAlign = 'center';
ctx.textAlign = textAlign;
ctx.textBaseline = 'middle';
ctx.fillStyle = scaleLabelFontColor; // render in correct colour
ctx.font = scaleLabelFont.string;
Expand Down