Skip to content

Commit b443dbd

Browse files
liuzhenyingaiyin.lzy
and
aiyin.lzy
authored
fix: funnel undefined render (#2150)
Co-authored-by: aiyin.lzy <[email protected]>
1 parent c2bdba3 commit b443dbd

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

__tests__/bugs/issue-2124-spec.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const DATA1 = [
88
{ stage: '录取人数', number: 87 },
99
{ stage: '入职人数', number: null },
1010
{ stage: '离职人数', number: 10 },
11+
{ stage: '回流人数' },
1112
];
1213

1314
const DATA2 = [
@@ -17,16 +18,18 @@ const DATA2 = [
1718
{ stage: '录取人数', number: 87, company: 'A公司' },
1819
{ stage: '入职人数', number: null, company: 'A公司' },
1920
{ stage: '离职人数', number: 10, company: 'A公司' },
21+
{ stage: '回流人数', company: 'A公司' },
2022
{ stage: '简历筛选', number: 303, company: 'B公司' },
2123
{ stage: '初试人数', number: 0, company: 'B公司' },
2224
{ stage: '复试人数', number: 153, company: 'B公司' },
2325
{ stage: '录取人数', number: 117, company: 'B公司' },
2426
{ stage: '入职人数', number: 79, company: 'B公司' },
2527
{ stage: '离职人数', number: 15, company: 'B公司' },
28+
{ stage: '回流人数', company: 'B公司' },
2629
];
2730

2831
describe('#2124', () => {
29-
it('Funnel 数据为 null, 0 不能报错', async () => {
32+
it('Funnel 数据为 null, 0, undefined 不能报错', async () => {
3033
const plot = new Funnel(createDiv(), {
3134
data: DATA1,
3235
xField: 'stage',
@@ -42,7 +45,7 @@ describe('#2124', () => {
4245
.getController('annotation')
4346
.getComponents()
4447
.map((co) => co.component.cfg.text.content)
45-
).toEqual(['转化率: 59.68%', '转化率: -∞', '转化率: ∞', '转化率: -', '转化率: -']);
48+
).toEqual(['转化率: 59.68%', '转化率: -∞', '转化率: ∞', '转化率: -', '转化率: -', '转化率: -']);
4649

4750
plot.destroy();
4851
});
@@ -80,15 +83,37 @@ describe('#2124', () => {
8083
.getComponents()
8184
.filter((co) => co.component.cfg.type === 'line')
8285
.map((co) => co.component.cfg.text.content)
83-
).toEqual(['转化率: 59.68%', '转化率: -∞', '转化率: ∞', '转化率: -', '转化率: -']);
86+
).toEqual(['转化率: 59.68%', '转化率: -∞', '转化率: ∞', '转化率: -', '转化率: -', '转化率: -']);
8487

8588
expect(
8689
plot.chart.views[1]
8790
.getController('annotation')
8891
.getComponents()
8992
.filter((co) => co.component.cfg.type === 'line')
9093
.map((co) => co.component.cfg.text.content)
91-
).toEqual(['转化率: -∞', '转化率: ∞', '转化率: 76.47%', '转化率: 67.52%', '转化率: 18.99%']);
94+
).toEqual(['转化率: -∞', '转化率: ∞', '转化率: 76.47%', '转化率: 67.52%', '转化率: 18.99%', '转化率: -']);
95+
96+
plot.destroy();
97+
});
98+
99+
it('动态高度漏斗图', async () => {
100+
const plot = new Funnel(createDiv(), {
101+
data: DATA1,
102+
xField: 'stage',
103+
yField: 'number',
104+
dynamicHeight: true,
105+
legend: false,
106+
minSize: 0.1,
107+
});
108+
109+
plot.render();
110+
111+
expect(
112+
plot.chart
113+
.getController('annotation')
114+
.getComponents()
115+
.map((co) => co.component.cfg.text.content)
116+
).toEqual(['转化率: 59.68%', '转化率: -∞', '转化率: ∞', '转化率: -', '转化率: -', '转化率: -']);
92117

93118
plot.destroy();
94119
});

src/plots/funnel/geometries/common.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ export function transformData(
2222

2323
// format 数据
2424
formatData = map(data, (row, index) => {
25-
if (row[yField] !== undefined) {
26-
const percent = row[yField] / maxYFieldValue;
27-
row[FUNNEL_PERCENT] = percent;
28-
row[FUNNEL_MAPPING_VALUE] = (max - min) * percent + min;
29-
// 转化率数据存储前后数据
30-
row[FUNNEL_CONVERSATION] = [get(data, [index - 1, yField]), row[yField]];
31-
}
25+
const percent = (row[yField] || 0) / maxYFieldValue;
26+
row[FUNNEL_PERCENT] = percent;
27+
row[FUNNEL_MAPPING_VALUE] = (max - min) * percent + min;
28+
// 转化率数据存储前后数据
29+
row[FUNNEL_CONVERSATION] = [get(data, [index - 1, yField]), row[yField]];
3230
return row;
3331
});
3432

src/plots/funnel/geometries/dynamic-height.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function field(params: Params<FunnelOptions>): Params<FunnelOptions> {
3131
const sum = reduce(
3232
data,
3333
(total, item) => {
34-
return total + item[yField];
34+
return total + (item[yField] || 0);
3535
},
3636
0
3737
);
@@ -43,7 +43,7 @@ function field(params: Params<FunnelOptions>): Params<FunnelOptions> {
4343
const x = [];
4444
const y = [];
4545

46-
row[FUNNEL_TOTAL_PERCENT] = row[yField] / sum;
46+
row[FUNNEL_TOTAL_PERCENT] = (row[yField] || 0) / sum;
4747

4848
// 获取左上角,右上角坐标
4949
if (index) {
@@ -69,7 +69,7 @@ function field(params: Params<FunnelOptions>): Params<FunnelOptions> {
6969
// 赋值
7070
row[PLOYGON_X] = x;
7171
row[PLOYGON_Y] = y;
72-
row[FUNNEL_PERCENT] = row[yField] / max;
72+
row[FUNNEL_PERCENT] = (row[yField] || 0) / max;
7373
row[FUNNEL_CONVERSATION] = [get(data, [index - 1, yField]), row[yField]];
7474
return row;
7575
});

0 commit comments

Comments
 (0)