Skip to content

Commit

Permalink
fix(Axis): 坐标轴文本超界自动对齐 (#1918)
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue authored Jan 3, 2024
1 parent 095ce5f commit 7bb11e3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
17 changes: 13 additions & 4 deletions packages/f2/src/components/axis/rect/bottom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isArray } from '@antv/util';

export default (props: RectProps<'bottom'>, context) => {
const { ticks, coord, style, animation } = props;
const { px2hd } = context;
const { px2hd, measureText } = context;
const { left, right, bottom } = coord;
const { grid, tickLine, line, labelOffset, label, symbol } = style;
const filterTicks = ticks.filter((d) => !isNaN(d.value));
Expand Down Expand Up @@ -89,11 +89,11 @@ export default (props: RectProps<'bottom'>, context) => {
{label
? filterTicks.map((tick, index) => {
const { points, text, tickValue, labelStyle } = tick;
const start = points[0];
const { x, y } = points[0];
const { align = 'center' } = labelStyle || label || {};
const textAttrs: TextStyleProps = {
x: start.x,
y: start.y + labelOffset,
x,
y: y + labelOffset,
textBaseline: 'top',
text,
...label,
Expand All @@ -108,6 +108,15 @@ export default (props: RectProps<'bottom'>, context) => {
} else {
textAttrs.textAlign = 'center';
}
} else if (align === 'auto') {
textAttrs.textAlign = 'center';
const { width } = measureText(text, textAttrs);
const halfWidth = width / 2;
if (x - halfWidth < left) {
textAttrs.x = left + width / 2;
} else if (x + halfWidth > right) {
textAttrs.x = right - width / 2;
}
} else {
textAttrs.textAlign = align;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/f2/src/components/axis/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface TickLine extends LineStyleProps {
}

interface Text extends TextStyleProps {
align?: 'left' | 'right' | 'start' | 'center' | 'end' | 'between';
align?: 'left' | 'right' | 'start' | 'center' | 'end' | 'between' | 'auto';
text?: string;
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions packages/f2/test/components/candlestick/pan.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ describe('candlestick', () => {
type="timeCat"
tickCount={3}
style={{
label: {
align: 'auto',
},
tickLine: {
length: 10,
lineWidth: '10px',
Expand Down Expand Up @@ -297,6 +300,9 @@ describe('candlestick', () => {
type="timeCat"
tickCount={3}
style={{
label: {
align: 'auto',
},
tickLine: {
length: 10,
lineWidth: '10px',
Expand Down

0 comments on commit 7bb11e3

Please sign in to comment.