Skip to content

Commit 536adf9

Browse files
authored
fix(issue-2161): 修复数据项存在非法 undefined 时,获取数据 max 为 NaN,导致玉珏图渲染页面崩溃 (#2168)
1 parent 2f401dc commit 536adf9

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

__tests__/bugs/issue-2161-spec.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { RadialBar } from '../../src';
2+
import { createDiv } from '../utils/dom';
3+
4+
describe('#2161', () => {
5+
it('[玉珏图] 当某一数据项 value 为 undefined 时,页面崩溃', () => {
6+
const data = [
7+
{ name: 'X6', star: 297 },
8+
{ name: 'G' },
9+
{ name: 'AVA', star: 805 },
10+
{ name: 'G2Plot', star: 1478 },
11+
{ name: 'L7', star: 2029 },
12+
{ name: 'G6', star: 7100 },
13+
{ name: 'F2', star: 7346 },
14+
{ name: 'G2', star: 10178 },
15+
];
16+
17+
const bar = new RadialBar(createDiv(), {
18+
width: 400,
19+
height: 300,
20+
data,
21+
xField: 'name',
22+
yField: 'star',
23+
radius: 0.8,
24+
innerRadius: 0.2,
25+
tooltip: {
26+
formatter: (datum) => {
27+
return { name: 'star数', value: datum.star };
28+
},
29+
},
30+
});
31+
bar.render();
32+
33+
expect(bar.chart).toBeDefined();
34+
35+
bar.destroy();
36+
});
37+
});

__tests__/unit/plots/radial-bar/utils-spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ describe('utils of radial-bar', () => {
1212
expect(getScaleMax(-300, yField, antvStar)).toBe((maxValue * 360) / 300);
1313
expect(getScaleMax(660, yField, antvStar)).toBe((maxValue * 360) / 300);
1414
});
15+
16+
it('getScaleMax: existed nil value', () => {
17+
expect(getScaleMax(-300, yField, [...antvStar, { name: 'c', star: undefined }])).toBe((maxValue * 360) / 300);
18+
expect(getScaleMax(-300, yField, [...antvStar, { name: 'c', star: null }])).toBe((maxValue * 360) / 300);
19+
});
1520
});

src/plots/radial-bar/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Data } from '../../types';
22

33
export function getScaleMax(maxAngle: number, yField: string, data: Data): number {
44
const yData = data.map((item) => item[yField]);
5-
const maxValue = Math.max(...yData);
5+
const maxValue = Math.max(...yData.filter((v) => v !== undefined));
66
const formatRadian = Math.abs(maxAngle) % 360;
77
if (!formatRadian) {
88
return maxValue;

0 commit comments

Comments
 (0)