Skip to content

Commit 438b5b9

Browse files
committed
test(pie): 添加饼图 utils& default-options 单测
1 parent 3afaf71 commit 438b5b9

File tree

3 files changed

+42
-47
lines changed

3 files changed

+42
-47
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Pie } from '../../../../src';
2+
import { DEFAULT_OPTIONS } from '../../../../src/plots/pie/contants';
23
import { POSITIVE_NEGATIVE_DATA } from '../../../data/common';
34
import { createDiv } from '../../../utils/dom';
45

@@ -167,4 +168,8 @@ describe('pie', () => {
167168

168169
pie.destroy();
169170
});
171+
172+
it('defaultOptions 保持从 constants 中获取', () => {
173+
expect(Pie.getDefaultOptions()).toEqual(DEFAULT_OPTIONS);
174+
});
170175
});

__tests__/unit/plots/pie/utils/index-spec.ts renamed to __tests__/unit/plots/pie/utils-spec.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { adaptOffset, getTotalValue } from '../../../../../src/plots/pie/utils';
1+
import { adaptOffset, getTotalValue, isAllZero, processIllegalData } from '../../../../src/plots/pie/utils';
22

33
describe('utils of pie plot', () => {
44
const data = [
@@ -58,4 +58,28 @@ describe('utils of pie plot', () => {
5858
expect(adaptOffset('spider', '30%')).toBe('30%');
5959
expect(adaptOffset('spider', NaN)).toBe(NaN);
6060
});
61+
62+
it('过滤非法数据', () => {
63+
const data = [{ type: '1', value: 0 }];
64+
expect(processIllegalData(data, 'value')).toEqual(data);
65+
data.push({ type: '2', value: 1 });
66+
expect(processIllegalData(data, 'value')).toEqual(data);
67+
data.push({ type: '3', value: null });
68+
expect(processIllegalData(data, 'value')).toEqual(data);
69+
data.push({ type: '4', value: undefined });
70+
expect(processIllegalData(data, 'value')).toEqual(data.slice(0, 3));
71+
data.push({ type: '5', value: NaN });
72+
expect(processIllegalData(data, 'value')).toEqual(data.slice(0, 3));
73+
});
74+
75+
it('判断是否全 0', () => {
76+
const data = [{ type: '1', value: 0 }];
77+
expect(isAllZero(data, 'value')).toBe(true);
78+
data.push({ type: '2', value: 0 });
79+
expect(isAllZero(data, 'value')).toBe(true);
80+
data.push({ type: '3', value: null });
81+
expect(isAllZero(data, 'value')).toBe(false);
82+
data.push({ type: '4', value: undefined });
83+
expect(isAllZero(data, 'value')).toBe(false);
84+
});
6185
});

src/plots/pie/index.ts

+12-46
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import { Plot } from '../../core/plot';
2-
import { deepAssign } from '../../utils';
32
import { Adaptor } from '../../core/adaptor';
3+
import { adaptor, pieAnnotation } from './adaptor';
4+
import { DEFAULT_OPTIONS } from './contants';
45
import { PieOptions } from './types';
56
import { isAllZero, processIllegalData } from './utils';
6-
import { adaptor, pieAnnotation } from './adaptor';
77
import './interactions';
88

99
export { PieOptions };
1010

1111
export class Pie extends Plot<PieOptions> {
12+
/**
13+
* 获取 饼图 默认配置项
14+
* @static 供外部使用
15+
*/
16+
static getDefaultOptions(): Partial<PieOptions> {
17+
return DEFAULT_OPTIONS;
18+
}
19+
1220
/** 图表类型 */
1321
public type: string = 'pie';
1422

@@ -34,52 +42,10 @@ export class Pie extends Plot<PieOptions> {
3442
}
3543

3644
/**
37-
* 获取 饼图 默认配置项
45+
* 获取 饼图 默认配置项, 供 base 获取
3846
*/
3947
protected getDefaultOptions(): Partial<PieOptions> {
40-
return deepAssign({}, super.getDefaultOptions(), {
41-
legend: {
42-
position: 'right',
43-
},
44-
tooltip: {
45-
shared: false,
46-
showTitle: false,
47-
showMarkers: false,
48-
},
49-
label: {
50-
layout: { type: 'limit-in-plot', cfg: { action: 'ellipsis' } },
51-
},
52-
/** 饼图样式, 不影响暗黑主题 */
53-
pieStyle: {
54-
stroke: 'white',
55-
lineWidth: 1,
56-
},
57-
/** 饼图中心文本默认样式 */
58-
statistic: {
59-
title: {
60-
style: { fontWeight: 300, color: '#4B535E', textAlign: 'center', fontSize: '20px', lineHeight: 1 },
61-
},
62-
content: {
63-
style: {
64-
fontWeight: 'bold',
65-
color: 'rgba(44,53,66,0.85)',
66-
textAlign: 'center',
67-
fontSize: '32px',
68-
lineHeight: 1,
69-
},
70-
},
71-
},
72-
/** 默认关闭 text-annotation 动画 */
73-
theme: {
74-
components: {
75-
annotation: {
76-
text: {
77-
animate: false,
78-
},
79-
},
80-
},
81-
},
82-
});
48+
return Pie.getDefaultOptions();
8349
}
8450

8551
/**

0 commit comments

Comments
 (0)