Skip to content

Commit 2f75603

Browse files
authored
fix: label2为空的情况下Spider布局 (#2129)
1 parent 6f9104f commit 2f75603

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

packages/f2/src/components/pieLabel/spider.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
export const adjustPosition = (half, showSide, props, labelWidth) => {
2-
const { coord, sidePadding, adjustOffset } = props;
2+
const { coord, sidePadding, adjustOffset, adjustRatio } = props;
33
const { left: coordLeft, right: coordRight } = coord;
44
const labels = [];
55
let lastY = 0;
66
let delta;
77

88
half.forEach((label) => {
9-
const { anchor, inflection, y, height } = label;
9+
const { anchor, inflection, y, height, label2 } = label;
1010

1111
const points = [anchor, inflection];
1212
const endX = showSide === 'left' ? coordLeft + sidePadding : coordRight - sidePadding;
1313
let endY = y;
1414

15-
delta = y - lastY - (lastY === 0 ? 0.5 * height : height);
15+
delta = y - lastY - (lastY === 0 && label2 ? 0.5 * height : height);
1616

1717
if (delta < 0) {
1818
// 文本调整下去了 需要添加折线
@@ -28,7 +28,8 @@ export const adjustPosition = (half, showSide, props, labelWidth) => {
2828
};
2929

3030
if (
31-
Math.abs(delta) < height * props.adjustRatio ||
31+
Math.abs(delta) < height * adjustRatio ||
32+
point2.y < lastY ||
3233
(showSide === 'right' && point2.x < inflection.x) ||
3334
(showSide === 'left' && point2.x > inflection.x)
3435
) {

packages/f2/src/components/pieLabel/withPieLabel.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ export default (View) => {
273273
// 折线的点
274274
const points = [anchor, inflection];
275275
const endX = coordRight - sidePadding;
276+
276277
const endY = coordTop + halfLabelHeight + labelHeight * index;
277278

278279
// 文本开始点
@@ -337,7 +338,10 @@ export default (View) => {
337338
records = customRecords
338339
.map((record) => {
339340
return allRecords.find(
340-
(d) => d.origin[colorField] === record[colorField] && d.origin[xField] === record[xField] && d.origin[yField] === record[yField]
341+
(d) =>
342+
d.origin[colorField] === record[colorField] &&
343+
d.origin[xField] === record[xField] &&
344+
d.origin[yField] === record[yField]
341345
);
342346
})
343347
.filter(Boolean);
33.2 KB
Loading

packages/f2/test/components/pieLabel/spider.test.tsx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,4 +524,82 @@ describe('Spider PieLabel', () => {
524524
await delay(300);
525525
expect(context).toMatchImageSnapshot();
526526
});
527+
528+
it('label2为空的情况', async () => {
529+
const context = createContext('label2为空的情况', {
530+
width: '300px',
531+
height: '120px',
532+
});
533+
const data = [
534+
{
535+
amount: 5,
536+
memo: '其他',
537+
const: 'const',
538+
},
539+
{
540+
amount: 6,
541+
memo: '消费',
542+
const: 'const',
543+
},
544+
{
545+
amount: 7,
546+
memo: '黄金',
547+
const: 'const',
548+
},
549+
{
550+
amount: 8,
551+
memo: '海外债',
552+
const: 'const',
553+
},
554+
{
555+
amount: 20,
556+
memo: '成长',
557+
const: 'const',
558+
},
559+
{
560+
amount: 54,
561+
memo: '固收',
562+
const: 'const',
563+
},
564+
];
565+
const { props } = (
566+
<Canvas context={context} pixelRatio={2}>
567+
<Chart
568+
data={data}
569+
coord={{
570+
type: 'polar',
571+
transposed: true,
572+
radius: 0.8,
573+
}}
574+
>
575+
<Interval
576+
x="const"
577+
y="amount"
578+
adjust="stack"
579+
color="memo"
580+
style={{
581+
stroke: '#fff', // 描边颜色(border)
582+
lineWidth: 0.5,
583+
}}
584+
/>
585+
<PieLabel
586+
type="spider"
587+
label1={(data) => {
588+
return {
589+
text: `${data.memo} ${data.amount}%`,
590+
fill: '#333',
591+
};
592+
}}
593+
label2=""
594+
/>
595+
</Chart>
596+
</Canvas>
597+
);
598+
599+
const canvas = new Canvas(props);
600+
await canvas.render();
601+
602+
await delay(500);
603+
expect(context).toMatchImageSnapshot();
604+
});
527605
});

0 commit comments

Comments
 (0)