Skip to content

Commit cd3be27

Browse files
authored
feat(v2/pie-label): add pie options (#1296)
Co-authored-by: kasmine<[email protected]>
1 parent e2ee437 commit cd3be27

File tree

3 files changed

+122
-4
lines changed

3 files changed

+122
-4
lines changed

__tests__/unit/plots/pie/index-spec.ts

-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ describe('pie', () => {
6262
const polarRadius = coordinate.getRadius();
6363
expect(radius).toBeUndefined();
6464
expect(polarRadius).toBeGreaterThan(0);
65-
66-
const geometry = pie.chart.geometries[0];
67-
const elements = geometry.elements;
6865
});
6966

7067
it('innerRadius', () => {
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { Pie } from '../../../../src';
2+
import { POSITIVE_NEGATIVE_DATA } from '../../../data/common';
3+
import { createDiv } from '../../../utils/dom';
4+
import { IGroup } from '@antv/g2/lib/dependents';
5+
6+
describe('pie label', () => {
7+
const data = POSITIVE_NEGATIVE_DATA.filter((o) => o.value > 0).map((d, idx) =>
8+
idx === 1 ? { ...d, type: 'item1' } : d
9+
);
10+
11+
const config = {
12+
width: 400,
13+
height: 300,
14+
data,
15+
angleField: 'value',
16+
colorField: 'type',
17+
radius: 0.8,
18+
label: {},
19+
};
20+
21+
it('label: visible', () => {
22+
const pie = new Pie(createDiv(), config);
23+
24+
pie.render();
25+
26+
const geometry = pie.chart.geometries[0];
27+
const elements = geometry.elements;
28+
const labelGroups = geometry.labelsContainer.getChildren();
29+
30+
expect(elements.length).toBe(data.length);
31+
expect(labelGroups.length).toBe(data.length);
32+
});
33+
34+
it.skip('label: single color(todo-hustcc: 没有 color 字段,label 显示错误)', () => {
35+
const pie = new Pie(createDiv(), {
36+
...config,
37+
colorField: null,
38+
});
39+
40+
pie.render();
41+
42+
const geometry = pie.chart.geometries[0];
43+
const labelGroups = geometry.labelsContainer.getChildren();
44+
45+
expect(labelGroups.length).toBe(data.length);
46+
});
47+
48+
it('label: custom content', () => {
49+
const pie = new Pie(createDiv(), {
50+
...config,
51+
label: {
52+
content: (data, item, idx) => {
53+
if (idx === 0 || idx === 1) {
54+
return 'hello';
55+
}
56+
const { type, value } = data;
57+
return `${type}: ${value}`;
58+
},
59+
},
60+
});
61+
62+
pie.render();
63+
64+
const geometry = pie.chart.geometries[0];
65+
const labelGroups = geometry.labelsContainer.getChildren();
66+
expect(labelGroups.length).toBe(data.length);
67+
const label1 = (labelGroups[0] as IGroup).getChildren();
68+
expect(label1[0].get('type')).toBe('text');
69+
expect(label1[1].get('type')).toBe('path');
70+
expect(label1[0].attr('text')).toBe('hello');
71+
const label2 = (labelGroups[1] as IGroup).getChildren();
72+
expect(label2[0].attr('text')).toBe('hello');
73+
});
74+
75+
it('label: custom callback', () => {
76+
const pie = new Pie(createDiv(), {
77+
...config,
78+
label: {
79+
callback: (value, type) => {
80+
return {
81+
content: `${type}: ${value}`,
82+
};
83+
},
84+
},
85+
});
86+
87+
pie.render();
88+
89+
const geometry = pie.chart.geometries[0];
90+
const labelGroups = geometry.labelsContainer.getChildren();
91+
92+
expect(labelGroups.length).toBe(data.length);
93+
const label1 = (labelGroups[0] as IGroup).getChildren();
94+
const label3 = (labelGroups[2] as IGroup).getChildren();
95+
expect(label1[0].attr('text')).toBe(`${data[0].type}: ${data[0].value}`);
96+
expect(label3[0].attr('text')).toBe(`${data[2].type}: ${data[2].value}`);
97+
});
98+
});

src/plots/pie/adaptor.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,29 @@ function legend(params: Params<PieOptions>): Params<PieOptions> {
7474
return params;
7575
}
7676

77+
/**
78+
* label 配置
79+
* @param params
80+
*/
81+
function label(params: Params<PieOptions>): Params<PieOptions> {
82+
const { chart, options } = params;
83+
const { label, colorField, angleField } = options;
84+
85+
const geometry = chart.geometries[0];
86+
// label 为 false, 空 则不显示 label
87+
if (!label) {
88+
geometry.label(false);
89+
} else {
90+
const { callback, ...cfg } = label;
91+
geometry.label({
92+
fields: [angleField, colorField],
93+
callback,
94+
cfg,
95+
});
96+
}
97+
return params;
98+
}
99+
77100
/**
78101
* style 配置
79102
* @param params
@@ -102,5 +125,5 @@ function style(params: Params<PieOptions>): Params<PieOptions> {
102125
*/
103126
export function adaptor(params: Params<PieOptions>) {
104127
// flow 的方式处理所有的配置到 G2 API
105-
flow(field, meta, coord, legend, tooltip, style)(params);
128+
flow(field, meta, coord, legend, tooltip, label, style)(params);
106129
}

0 commit comments

Comments
 (0)