Skip to content

Commit a40865d

Browse files
committed
fix(v2/pie): 饼图、环图字段配置错误 可正常绘制
angleField 对应的数据为number类型或为空时,正常显示,其他进行筛选过滤,从而避免触发 out of memory
1 parent d827312 commit a40865d

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed

__tests__/bugs/issue-1245-spec.ts

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { Pie } from '../../src';
2+
import { createDiv } from '.././utils/dom';
3+
import { delay } from '.././utils/delay';
4+
5+
describe('donut plot', () => {
6+
test('angleField & colorField 配置交换, 不会触发 out-of-memory, 但是坐标为 NaN', () => {
7+
const data = [
8+
{
9+
type: '分类一',
10+
value: 27,
11+
},
12+
{
13+
type: '分类二',
14+
value: 25,
15+
},
16+
{
17+
type: '分类三',
18+
value: 18,
19+
},
20+
{
21+
type: '分类四',
22+
value: 15,
23+
},
24+
{
25+
type: '分类五',
26+
value: 10,
27+
},
28+
{
29+
type: '其它',
30+
value: 5,
31+
},
32+
];
33+
34+
const donutPlot = new Pie(createDiv(), {
35+
width: 640,
36+
height: 400,
37+
radius: 0.8,
38+
innerRadius: 0.64,
39+
padding: 'auto',
40+
data,
41+
angleField: 'type',
42+
colorField: 'value',
43+
});
44+
45+
donutPlot.render();
46+
47+
expect(donutPlot).toBeDefined();
48+
expect(donutPlot.chart.geometries[0].elements.length).toBe(0);
49+
});
50+
51+
test('value 数据出现字母或其他不合法情况,不会触发 out-of-memory', () => {
52+
const data = [
53+
{
54+
type: '分类一',
55+
value: 27,
56+
},
57+
{
58+
type: '分类二',
59+
value: 25,
60+
},
61+
{
62+
type: '分类三',
63+
value: 18,
64+
},
65+
{
66+
type: '分类四',
67+
value: 15,
68+
},
69+
{
70+
type: '分类五',
71+
value: 10,
72+
},
73+
{
74+
type: '其它',
75+
value: '11a',
76+
},
77+
];
78+
79+
const piePlot = new Pie(createDiv(), {
80+
width: 640,
81+
height: 400,
82+
radius: 0.8,
83+
padding: 'auto',
84+
data,
85+
angleField: 'value',
86+
colorField: 'type',
87+
});
88+
89+
piePlot.render();
90+
expect(piePlot).toBeDefined();
91+
expect(piePlot.chart.geometries[0].elements.length).toBe(data.length - 1);
92+
93+
delay(3000).then(() => {
94+
piePlot.update({
95+
...piePlot.options,
96+
data: data.map((d, idx) => (idx !== 0 ? { ...d, value: null } : d)),
97+
});
98+
expect(piePlot.chart.geometries[0].elements.length).toBe(data.length);
99+
});
100+
});
101+
});

src/plots/pie/adaptor.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deepMix, each, get, isFunction } from '@antv/util';
1+
import { deepMix, each, filter, get, isFunction, isNil } from '@antv/util';
22
import { Params } from '../../core/adaptor';
33
import { tooltip, interaction, animation, theme } from '../../common/adaptor';
44
import { flow } from '../../utils';
@@ -14,7 +14,14 @@ function field(params: Params<PieOptions>): Params<PieOptions> {
1414
const { chart, options } = params;
1515
const { data, angleField, colorField, color } = options;
1616

17-
chart.data(data);
17+
// 处理不合法的数据
18+
const progressData = filter(data, (d) => typeof d[angleField] === 'number' || isNil(d[angleField]));
19+
if (progressData.length !== data.length) {
20+
// 先打印出来,后面统一界面提示
21+
console.error('数据源存在不合适的数据,请检查');
22+
}
23+
24+
chart.data(progressData);
1825
const geometry = chart.interval().position(`1*${angleField}`).adjust({ type: 'stack' });
1926

2027
if (colorField) {

0 commit comments

Comments
 (0)