Skip to content

Commit

Permalink
test(pie): 添加饼图 utils& default-options 单测
Browse files Browse the repository at this point in the history
  • Loading branch information
visiky committed Jan 23, 2021
1 parent 3afaf71 commit 438b5b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 47 deletions.
5 changes: 5 additions & 0 deletions __tests__/unit/plots/pie/index-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Pie } from '../../../../src';
import { DEFAULT_OPTIONS } from '../../../../src/plots/pie/contants';
import { POSITIVE_NEGATIVE_DATA } from '../../../data/common';
import { createDiv } from '../../../utils/dom';

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

pie.destroy();
});

it('defaultOptions 保持从 constants 中获取', () => {
expect(Pie.getDefaultOptions()).toEqual(DEFAULT_OPTIONS);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { adaptOffset, getTotalValue } from '../../../../../src/plots/pie/utils';
import { adaptOffset, getTotalValue, isAllZero, processIllegalData } from '../../../../src/plots/pie/utils';

describe('utils of pie plot', () => {
const data = [
Expand Down Expand Up @@ -58,4 +58,28 @@ describe('utils of pie plot', () => {
expect(adaptOffset('spider', '30%')).toBe('30%');
expect(adaptOffset('spider', NaN)).toBe(NaN);
});

it('过滤非法数据', () => {
const data = [{ type: '1', value: 0 }];
expect(processIllegalData(data, 'value')).toEqual(data);
data.push({ type: '2', value: 1 });
expect(processIllegalData(data, 'value')).toEqual(data);
data.push({ type: '3', value: null });
expect(processIllegalData(data, 'value')).toEqual(data);
data.push({ type: '4', value: undefined });
expect(processIllegalData(data, 'value')).toEqual(data.slice(0, 3));
data.push({ type: '5', value: NaN });
expect(processIllegalData(data, 'value')).toEqual(data.slice(0, 3));
});

it('判断是否全 0', () => {
const data = [{ type: '1', value: 0 }];
expect(isAllZero(data, 'value')).toBe(true);
data.push({ type: '2', value: 0 });
expect(isAllZero(data, 'value')).toBe(true);
data.push({ type: '3', value: null });
expect(isAllZero(data, 'value')).toBe(false);
data.push({ type: '4', value: undefined });
expect(isAllZero(data, 'value')).toBe(false);
});
});
58 changes: 12 additions & 46 deletions src/plots/pie/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { Plot } from '../../core/plot';
import { deepAssign } from '../../utils';
import { Adaptor } from '../../core/adaptor';
import { adaptor, pieAnnotation } from './adaptor';
import { DEFAULT_OPTIONS } from './contants';
import { PieOptions } from './types';
import { isAllZero, processIllegalData } from './utils';
import { adaptor, pieAnnotation } from './adaptor';
import './interactions';

export { PieOptions };

export class Pie extends Plot<PieOptions> {
/**
* 获取 饼图 默认配置项
* @static 供外部使用
*/
static getDefaultOptions(): Partial<PieOptions> {
return DEFAULT_OPTIONS;
}

/** 图表类型 */
public type: string = 'pie';

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

/**
* 获取 饼图 默认配置项
* 获取 饼图 默认配置项, 供 base 获取
*/
protected getDefaultOptions(): Partial<PieOptions> {
return deepAssign({}, super.getDefaultOptions(), {
legend: {
position: 'right',
},
tooltip: {
shared: false,
showTitle: false,
showMarkers: false,
},
label: {
layout: { type: 'limit-in-plot', cfg: { action: 'ellipsis' } },
},
/** 饼图样式, 不影响暗黑主题 */
pieStyle: {
stroke: 'white',
lineWidth: 1,
},
/** 饼图中心文本默认样式 */
statistic: {
title: {
style: { fontWeight: 300, color: '#4B535E', textAlign: 'center', fontSize: '20px', lineHeight: 1 },
},
content: {
style: {
fontWeight: 'bold',
color: 'rgba(44,53,66,0.85)',
textAlign: 'center',
fontSize: '32px',
lineHeight: 1,
},
},
},
/** 默认关闭 text-annotation 动画 */
theme: {
components: {
annotation: {
text: {
animate: false,
},
},
},
},
});
return Pie.getDefaultOptions();
}

/**
Expand Down

0 comments on commit 438b5b9

Please sign in to comment.