Skip to content

Commit

Permalink
fix(v2/pie): 饼图、环图字段配置错误 可正常绘制
Browse files Browse the repository at this point in the history
angleField 对应的数据为number类型或为空时,正常显示,其他进行筛选过滤,从而避免触发 out of memory
  • Loading branch information
visiky authored and hustcc committed Jul 22, 2020
1 parent 83b67f4 commit a64ae03
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
101 changes: 101 additions & 0 deletions __tests__/bugs/issue-1245-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Pie } from '../../src';
import { createDiv } from '.././utils/dom';
import { delay } from '.././utils/delay';

describe('donut plot', () => {
test('angleField & colorField 配置交换, 不会触发 out-of-memory, 但是坐标为 NaN', () => {
const data = [
{
type: '分类一',
value: 27,
},
{
type: '分类二',
value: 25,
},
{
type: '分类三',
value: 18,
},
{
type: '分类四',
value: 15,
},
{
type: '分类五',
value: 10,
},
{
type: '其它',
value: 5,
},
];

const donutPlot = new Pie(createDiv(), {
width: 640,
height: 400,
radius: 0.8,
innerRadius: 0.64,
padding: 'auto',
data,
angleField: 'type',
colorField: 'value',
});

donutPlot.render();

expect(donutPlot).toBeDefined();
expect(donutPlot.chart.geometries[0].elements.length).toBe(0);
});

test('value 数据出现字母或其他不合法情况,不会触发 out-of-memory', () => {
const data = [
{
type: '分类一',
value: 27,
},
{
type: '分类二',
value: 25,
},
{
type: '分类三',
value: 18,
},
{
type: '分类四',
value: 15,
},
{
type: '分类五',
value: 10,
},
{
type: '其它',
value: '11a',
},
];

const piePlot = new Pie(createDiv(), {
width: 640,
height: 400,
radius: 0.8,
padding: 'auto',
data,
angleField: 'value',
colorField: 'type',
});

piePlot.render();
expect(piePlot).toBeDefined();
expect(piePlot.chart.geometries[0].elements.length).toBe(data.length - 1);

delay(3000).then(() => {
piePlot.update({
...piePlot.options,
data: data.map((d, idx) => (idx !== 0 ? { ...d, value: null } : d)),
});
expect(piePlot.chart.geometries[0].elements.length).toBe(data.length);
});
});
});
11 changes: 9 additions & 2 deletions src/plots/pie/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deepMix, each, get, isFunction } from '@antv/util';
import { deepMix, each, filter, get, isFunction, isNil } from '@antv/util';
import { Params } from '../../core/adaptor';
import { tooltip, interaction, animation, theme } from '../../common/adaptor';
import { flow } from '../../utils';
Expand All @@ -14,7 +14,14 @@ function field(params: Params<PieOptions>): Params<PieOptions> {
const { chart, options } = params;
const { data, angleField, colorField, color } = options;

chart.data(data);
// 处理不合法的数据
const progressData = filter(data, (d) => typeof d[angleField] === 'number' || isNil(d[angleField]));
if (progressData.length !== data.length) {
// 先打印出来,后面统一界面提示
console.error('数据源存在不合适的数据,请检查');
}

chart.data(progressData);
const geometry = chart.interval().position(`1*${angleField}`).adjust({ type: 'stack' });

if (colorField) {
Expand Down

0 comments on commit a64ae03

Please sign in to comment.