Skip to content

Commit e48e5ae

Browse files
authored
fix: 增加初次检测是否重叠 (#2103)
1 parent 135a91f commit e48e5ae

5 files changed

+59
-14
lines changed

packages/f2/src/components/axis/withAxis.tsx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default (View) => {
2727
IProps extends AxisProps<TRecord> = AxisProps<TRecord>
2828
> extends Component<IProps & ChartChildProps, {}> {
2929
axisStyle: Style = {};
30+
maxLabelWidth: number = 0;
3031
ticks: Tick[];
3132

3233
constructor(props: IProps & ChartChildProps) {
@@ -383,25 +384,31 @@ export default (View) => {
383384
return false;
384385
}
385386

386-
findLabelsToHide(ticks) {
387-
const { props, context } = this;
388-
const { coord } = props;
387+
hasOverlap(ticks) {
388+
const { context } = this;
389389
const { measureText } = context;
390390

391391
const tickCount = ticks.length;
392392

393393
const { label } = this.axisStyle;
394394

395-
let maxLabelWidth = 0;
396395
for (let i = 0; i < tickCount; i++) {
397396
const tick = ticks[i];
398397
const { labelStyle = {}, text } = tick;
399398
const bbox = measureText(labelStyle.text || text, { ...label, ...labelStyle });
400399
tick.labelWidth = bbox.width;
401-
maxLabelWidth = Math.max(maxLabelWidth, bbox.width);
400+
this.maxLabelWidth = Math.max(this.maxLabelWidth, bbox.width);
402401
}
402+
return this.hasOverlapAtSeq(ticks, 1);
403+
}
403404

404-
const initialSeq = Math.floor(maxLabelWidth / (coord.width / (tickCount - 1)));
405+
findLabelsToHide(ticks) {
406+
const { props } = this;
407+
const { coord } = props;
408+
409+
const tickCount = ticks.length;
410+
411+
const initialSeq = Math.floor(this.maxLabelWidth / (coord.width / (tickCount - 1)));
405412

406413
const range = tickCount - 1;
407414
const maxSeq = Math.floor(range / 2);
@@ -432,24 +439,29 @@ export default (View) => {
432439
}
433440
}
434441

435-
// 主要是计算coord的布局
442+
// 计算坐标轴布局并更新坐标系
436443
updateCoord() {
437444
const { props } = this;
438445
const { chart, labelAutoRotate = false, labelAutoHide = false } = props;
439446
const dimType = this._getDimType();
440447
const ticks = this.getTicks();
441448

442-
if (labelAutoRotate && dimType === 'x') {
443-
this.findSuitableRotation(ticks);
444-
this.ticks = ticks;
445-
}
446-
if (labelAutoHide && dimType === 'x') {
447-
this.findLabelsToHide(ticks);
449+
if ((labelAutoRotate || labelAutoHide) && dimType === 'x' && this.hasOverlap(ticks)) {
450+
if (labelAutoRotate) {
451+
this.findSuitableRotation(ticks);
452+
}
453+
454+
if (labelAutoHide) {
455+
this.findLabelsToHide(ticks);
456+
}
457+
448458
this.ticks = ticks;
449459
}
450460

461+
// 测量并获取布局信息
451462
const layout = this.measureLayout(ticks);
452463

464+
// 更新图表的坐标系
453465
chart.updateCoordFor(this, layout);
454466
}
455467

Loading
Loading
Loading

packages/f2/test/components/axis/labelAuto.test.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,39 @@ describe('Axis labelAutoRotate', () => {
5050
expect(context).toMatchImageSnapshot();
5151
});
5252

53+
it('启用标签自动旋转不用旋转', async () => {
54+
const context = createContext('启用标签自动旋转不用旋转');
55+
56+
const { props } = (
57+
<Canvas context={context} pixelRatio={1} width={350} height={250}>
58+
<Chart
59+
data={[
60+
{ category: '科创AI指数', value: 10 },
61+
{ category: '科创50指数', value: 15 },
62+
{ category: '沪深300指数', value: 20 },
63+
{ category: '芯片指数', value: 25 },
64+
]}
65+
style={{
66+
padding: [0, 0, 0, 0],
67+
}}
68+
>
69+
<Axis
70+
field="category"
71+
labelAutoRotate={true} // 启用标签自动旋转
72+
/>
73+
<Axis field="value" />
74+
<Interval x="category" y="value" color="#2FC25B" />
75+
</Chart>
76+
</Canvas>
77+
);
78+
79+
const canvas = new Canvas(props);
80+
await canvas.render();
81+
82+
await delay(1000);
83+
expect(context).toMatchImageSnapshot();
84+
});
85+
5386
it('启用标签自动旋转角度小', async () => {
5487
const context = createContext('启用标签自动旋转角度小');
5588

@@ -60,7 +93,7 @@ describe('Axis labelAutoRotate', () => {
6093
{ category: '科创AI指数', value: 10 },
6194
{ category: '科创50指数', value: 15 },
6295
{ category: '沪深300指数', value: 20 },
63-
{ category: '中华半导体芯片指数', value: 25 },
96+
{ category: '测中华半导体芯片指数', value: 25 },
6497
]}
6598
>
6699
<Axis field="category" labelAutoRotate={true} />

0 commit comments

Comments
 (0)