Skip to content

Commit 1d380dc

Browse files
authored
feat: pielabel支持展示指定records (#2116)
1 parent 2619cf0 commit 1d380dc

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export interface PieLabelProps {
6767
label1?: any;
6868
label2?: any;
6969
sidePadding?: string | number;
70+
/**
71+
* 指定要显示的数据记录
72+
*/
73+
records?: any[];
7074
/**
7175
* 触发的事件类型
7276
*/
@@ -317,11 +321,26 @@ export default (View) => {
317321
sidePadding,
318322
label1OffsetY,
319323
label2OffsetY,
324+
records: customRecords,
320325
} = props;
326+
const { adjust } = chart;
321327
const { measureText, px2hd } = this.context;
322328
const { center, radius, height: coordHeight, width: coordWidth } = coord;
323329
const geometry = chart.getGeometrys()[0];
324-
const records = geometry.flatRecords();
330+
const allRecords = geometry.flatRecords();
331+
332+
let records = allRecords;
333+
334+
if (customRecords) {
335+
const { xField, yField } = adjust.adjust;
336+
records = customRecords
337+
.map((record) => {
338+
return allRecords.find(
339+
(d) => d.origin[xField] === record[xField] && d.origin[yField] === record[yField]
340+
);
341+
})
342+
.filter(Boolean);
343+
}
325344

326345
// 高度计算,拿第一项数据作为计算依据
327346
const label1Text = isFunction(label1) ? label1(records[0]?.origin, records[0]) : label1;
6.69 KB
Loading

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,91 @@ describe('Spider PieLabel', () => {
432432
await delay(300);
433433
expect(context).toMatchImageSnapshot();
434434
});
435+
436+
it('指定展示指定数据', async () => {
437+
const context = createContext('指定展示指定数据', {
438+
width: '300px',
439+
height: '150px',
440+
});
441+
const data = [
442+
{
443+
amount: 20,
444+
memo: 'Eat',
445+
const: 'const',
446+
},
447+
{
448+
amount: 10,
449+
memo: 'Language',
450+
const: 'const',
451+
},
452+
{
453+
amount: 10,
454+
memo: 'Math',
455+
const: 'const',
456+
},
457+
{
458+
amount: 10,
459+
memo: 'Foreign',
460+
const: 'const',
461+
},
462+
463+
{
464+
amount: 30,
465+
memo: 'Sports',
466+
const: 'const',
467+
},
468+
{
469+
amount: 20,
470+
memo: 'Other',
471+
const: 'const',
472+
},
473+
];
474+
const { props } = (
475+
<Canvas context={context} animate={false} pixelRatio={1}>
476+
<Chart
477+
data={data}
478+
coord={{
479+
type: 'polar',
480+
transposed: true,
481+
innerRadius: 0.3,
482+
radius: 0.5,
483+
}}
484+
>
485+
<Interval x="const" y="amount" adjust="stack" color="memo" />
486+
<PieLabel
487+
type="spider"
488+
records={[
489+
{
490+
amount: 20,
491+
memo: 'Eat',
492+
const: 'const',
493+
},
494+
{
495+
amount: 10,
496+
memo: 'Language',
497+
const: 'const',
498+
},
499+
]}
500+
label1={(data) => {
501+
return {
502+
text: data.memo,
503+
};
504+
}}
505+
label2={(data) => {
506+
return {
507+
fill: '#000000',
508+
text: '$' + data.amount.toFixed(2),
509+
};
510+
}}
511+
/>
512+
</Chart>
513+
</Canvas>
514+
);
515+
516+
const canvas = new Canvas(props);
517+
await canvas.render();
518+
519+
await delay(300);
520+
expect(context).toMatchImageSnapshot();
521+
});
435522
});

0 commit comments

Comments
 (0)