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 new align 'inner' for X axis #10106

Merged
merged 8 commits into from Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/axes/cartesian/_common_ticks.md
Expand Up @@ -4,7 +4,7 @@ Namespace: `options.scales[scaleId].ticks`

| Name | Type | Default | Description
| ---- | ---- | ------- | -----------
| `align` | `string` | `'center'` | The tick alignment along the axis. Can be `'start'`, `'center'`, or `'end'`.
| `align` | `string` | `'center'` | The tick alignment along the axis. Can be `'start'`, `'center'`, `'end'`, or `'inner'`. `inner` alignment means align `start` for first tick and `end` for the last tick of horizontal axis
| `crossAlign` | `string` | `'near'` | The tick alignment perpendicular to the axis. Can be `'near'`, `'center'`, or `'far'`. See [Tick Alignment](/axes/cartesian/#tick-alignment)
| `sampleSize` | `number` | `ticks.length` | The number of ticks to examine when deciding how many labels will fit. Setting a smaller value will be faster, but may be less accurate when there is large variability in label length.
| `autoSkip` | `boolean` | `true` | If true, automatically calculates how many labels can be shown and hides labels accordingly. Labels will be rotated up to `maxRotation` before skipping any. Turn `autoSkip` off to show all labels no matter what.
Expand Down
14 changes: 13 additions & 1 deletion src/core/core.scale.js
Expand Up @@ -1206,9 +1206,19 @@ export default class Scale extends Element {
const color = optsAtIndex.color;
const strokeColor = optsAtIndex.textStrokeColor;
const strokeWidth = optsAtIndex.textStrokeWidth;
let tickTextAlign = textAlign;

if (isHorizontal) {
x = pixel;

if (textAlign === 'inner') {
if (i === ilen - 1) {
tickTextAlign = 'right';
} else if (i === 0) {
tickTextAlign = 'left';
}
}
kurkle marked this conversation as resolved.
Show resolved Hide resolved

if (position === 'top') {
if (crossAlign === 'near' || rotation !== 0) {
textOffset = -lineCount * lineHeight + lineHeight / 2;
Expand Down Expand Up @@ -1285,7 +1295,7 @@ export default class Scale extends Element {
strokeColor,
strokeWidth,
textOffset,
textAlign,
textAlign: tickTextAlign,
textBaseline,
translation: [x, y],
backdrop,
Expand All @@ -1309,6 +1319,8 @@ export default class Scale extends Element {
align = 'left';
} else if (ticks.align === 'end') {
align = 'right';
} else if (ticks.align === 'inner') {
align = 'inner';
}

return align;
Expand Down
34 changes: 34 additions & 0 deletions test/fixtures/core.scale/label-align-inner-rotate.js
@@ -0,0 +1,34 @@
module.exports = {
config: {
type: 'line',
data: {
datasets: [{
data: [1, 2, 3],
}],
labels: ['Label1', 'Label2', 'Label3']
},
options: {
scales: {
x: {
ticks: {
align: 'inner',
maxRotation: 45,
minRotation: 45
},
},
y: {
ticks: {
align: 'inner',
}
}
}
}
},
options: {
spriteText: true,
canvas: {
height: 256,
width: 512
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions test/fixtures/core.scale/label-align-inner.js
@@ -0,0 +1,32 @@
module.exports = {
config: {
type: 'line',
data: {
datasets: [{
data: [1, 2, 3],
}],
labels: ['Label1', 'Label2', 'Label3']
},
options: {
scales: {
x: {
ticks: {
align: 'inner',
},
},
y: {
ticks: {
align: 'inner',
}
}
Talla2XLC marked this conversation as resolved.
Show resolved Hide resolved
}
}
},
options: {
spriteText: true,
canvas: {
height: 256,
width: 512
}
}
};
Binary file added test/fixtures/core.scale/label-align-inner.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions types/index.esm.d.ts
Expand Up @@ -1654,7 +1654,7 @@ export interface FontSpec {
lineHeight: number | string;
}

export type TextAlign = 'left' | 'center' | 'right';
export type TextAlign = 'left' | 'center' | 'right' | 'inner';
Talla2XLC marked this conversation as resolved.
Show resolved Hide resolved

export interface VisualElement {
draw(ctx: CanvasRenderingContext2D, area?: ChartArea): void;
Expand Down Expand Up @@ -3027,7 +3027,7 @@ export interface CartesianScaleOptions extends CoreScaleOptions {
* The label alignment
* @default 'center'
*/
align: 'start' | 'center' | 'end';
align: 'start' | 'center' | 'end' | 'inner';
Talla2XLC marked this conversation as resolved.
Show resolved Hide resolved
/**
* If true, automatically calculates how many labels can be shown and hides labels accordingly. Labels will be rotated up to maxRotation before skipping any. Turn autoSkip off to show all labels no matter what.
* @default true
Expand Down