Skip to content

Commit 546b359

Browse files
authored
fix: auto-rotate当label为function失效 (#2099)
1 parent c57ce48 commit 546b359

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export default (View) => {
241241
});
242242

243243
let labelRightEdge = lastTickPoint.x;
244-
const align = label.align || 'center';
244+
const { align = 'center' } = { ...label, ...labelStyle };
245245

246246
if (align === 'center') {
247247
labelRightEdge += tickBBox.width / 2;
@@ -355,9 +355,12 @@ export default (View) => {
355355
const ceiledAngle = Math.ceil(theoreticalAngle);
356356

357357
if (ceiledAngle > 0 && ceiledAngle <= 90) {
358-
this.axisStyle.label.align = 'start';
359-
this.axisStyle.label.transform = `rotate(${ceiledAngle}deg)`;
360-
this.axisStyle.label.transformOrigin = `0 50%`;
358+
ticks.forEach((tick) => {
359+
tick.labelStyle = tick.labelStyle || {};
360+
tick.labelStyle.align = 'start';
361+
tick.labelStyle.transform = `rotate(${ceiledAngle}deg)`;
362+
tick.labelStyle.transformOrigin = '0 50%';
363+
});
361364
}
362365
}
363366

21.4 KB
Loading

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,41 @@ describe('Axis labelAutoRotate', () => {
172172
await delay(1000);
173173
expect(context).toMatchImageSnapshot();
174174
});
175+
176+
it('启用标签自动旋转(labelAutoRotate=true)但label为函数', async () => {
177+
const context = createContext('启用标签自动旋转但label为函数');
178+
179+
const { props } = (
180+
<Canvas context={context} pixelRatio={1} width={350} height={250}>
181+
<Chart
182+
data={overlappingData}
183+
style={{
184+
padding: [0, 0, 0, 0],
185+
}}
186+
>
187+
<Axis
188+
field="category"
189+
labelAutoRotate={true}
190+
style={{
191+
label: (text, index, ticks) => {
192+
return {
193+
textAlign: 'center',
194+
fill: index % 2 === 0 ? '#1890FF' : '#333333',
195+
fontWeight: 'bold',
196+
};
197+
},
198+
}}
199+
/>
200+
<Axis field="value" />
201+
<Interval x="category" y="value" color="#2FC25B" />
202+
</Chart>
203+
</Canvas>
204+
);
205+
206+
const canvas = new Canvas(props);
207+
await canvas.render();
208+
209+
await delay(1000);
210+
expect(context).toMatchImageSnapshot();
211+
});
175212
});

0 commit comments

Comments
 (0)