Skip to content

Commit b8de33a

Browse files
authored
fix(#2203): crash when percent = NaN (#2211)
1 parent f539977 commit b8de33a

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

__tests__/bugs/issue-2203-spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { RingProgress, Progress } from '../../src';
2+
import { createDiv } from '../utils/dom';
3+
4+
describe('#2203', () => {
5+
it('ringProgress', () => {
6+
const ringProgress = new RingProgress(createDiv(), {
7+
height: 100,
8+
width: 100,
9+
autoFit: false,
10+
percent: NaN,
11+
color: ['#5B8FF9', '#E8EDF3'],
12+
});
13+
14+
ringProgress.render();
15+
16+
expect(ringProgress.chart.getOptions().data[0].percent).toBe(0);
17+
ringProgress.destroy();
18+
});
19+
20+
it('Progress', () => {
21+
const progress = new Progress(createDiv(), {
22+
height: 10,
23+
width: 100,
24+
autoFit: false,
25+
percent: NaN,
26+
color: ['#5B8FF9', '#E8EDF3'],
27+
});
28+
29+
progress.render();
30+
31+
expect(progress.chart.getOptions().data[0].percent).toBe(0);
32+
progress.destroy();
33+
});
34+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { getProgressData } from '../../../../src/plots/progress/utils';
2+
3+
describe('getProgressData', () => {
4+
it('getProgressData', () => {
5+
expect(getProgressData(0)).toEqual([
6+
{ type: 'current', percent: 0 },
7+
{ type: 'target', percent: 1 },
8+
]);
9+
10+
expect(getProgressData(1)).toEqual([
11+
{ type: 'current', percent: 1 },
12+
{ type: 'target', percent: 0 },
13+
]);
14+
15+
expect(getProgressData(0.4)).toEqual([
16+
{ type: 'current', percent: 0.4 },
17+
{ type: 'target', percent: 0.6 },
18+
]);
19+
20+
expect(getProgressData(-1)).toEqual([
21+
{ type: 'current', percent: 0 },
22+
{ type: 'target', percent: 1 },
23+
]);
24+
25+
expect(getProgressData(2)).toEqual([
26+
{ type: 'current', percent: 1 },
27+
{ type: 'target', percent: 0 },
28+
]);
29+
30+
expect(getProgressData(NaN)).toEqual([
31+
{ type: 'current', percent: 0 },
32+
{ type: 'target', percent: 1 },
33+
]);
34+
35+
expect(getProgressData(null)).toEqual([
36+
{ type: 'current', percent: 0 },
37+
{ type: 'target', percent: 1 },
38+
]);
39+
});
40+
});

src/plots/progress/utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { clamp } from '@antv/util';
2+
import { isRealNumber } from '../../utils/number';
23

34
/**
45
* 获取进度条数据
56
*/
67
export function getProgressData(percent: number) {
7-
const clampPercent = clamp(percent, 0, 1);
8+
const clampPercent = clamp(isRealNumber(percent) ? percent : 0, 0, 1);
89
return [
910
{
1011
type: 'current',

0 commit comments

Comments
 (0)