Skip to content

Commit

Permalink
Enable multi-line axis titles (#8579)
Browse files Browse the repository at this point in the history
  • Loading branch information
etimberg committed Mar 6, 2021
1 parent a23f1de commit 279b6ae
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/docs/axes/labelling.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Namespace: `options.scales[scaleId].title`, it defines options for the scale tit
| ---- | ---- | ------- | -----------
| `display` | `boolean` | `false` | If true, display the axis title.
| `align` | `string` | `'center'` | Alignment of the axis title. Possible options are `'start'`, `'center'` and `'end'`
| `text` | `string` | `''` | The text for the title. (i.e. "# of People" or "Response Choices").
| `text` | `string`\|`string[]` | `''` | The text for the title. (i.e. "# of People" or "Response Choices").
| `color` | [`Color`](../general/colors.md) | `Chart.defaults.color` | Color of label.
| `font` | `Font` | `Chart.defaults.font` | See [Fonts](../general/fonts.md)
| `padding` | `number`\|`object` | `4` | Padding to apply around scale labels. Only `top` and `bottom` are implemented.
Expand Down
3 changes: 2 additions & 1 deletion src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,9 @@ function getTitleHeight(options, fallback) {

const font = toFont(options.font, fallback);
const padding = toPadding(options.padding);
const lines = isArray(options.text) ? options.text.length : 1;

return font.lineHeight + padding.height;
return (lines * font.lineHeight) + padding.height;
}

/**
Expand Down
32 changes: 32 additions & 0 deletions test/fixtures/core.scale/title-multi-line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
config: {
type: 'line',
data: {
datasets: [{
data: [1, -1, 3],
}],
labels: ['Label1', 'Label2', 'Label3']
},
options: {
scales: {
y: {
title: {
display: true,
text: [
'Line 1',
'Line 2',
'Line 3',
]
}
}
}
}
},
options: {
spriteText: true,
canvas: {
height: 256,
width: 512
}
}
};
Binary file added test/fixtures/core.scale/title-multi-line.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion types/index.esm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2698,7 +2698,7 @@ export interface CartesianScaleOptions extends CoreScaleOptions {

title: {
display: boolean;
text: string;
text: string | string[];
color: Color;
font: FontSpec;
padding: {
Expand Down

0 comments on commit 279b6ae

Please sign in to comment.