Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: funnel undefined render #2150

Merged
merged 1 commit into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions __tests__/bugs/issue-2124-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const DATA1 = [
{ stage: '录取人数', number: 87 },
{ stage: '入职人数', number: null },
{ stage: '离职人数', number: 10 },
{ stage: '回流人数' },
];

const DATA2 = [
Expand All @@ -17,16 +18,18 @@ const DATA2 = [
{ stage: '录取人数', number: 87, company: 'A公司' },
{ stage: '入职人数', number: null, company: 'A公司' },
{ stage: '离职人数', number: 10, company: 'A公司' },
{ stage: '回流人数', company: 'A公司' },
{ stage: '简历筛选', number: 303, company: 'B公司' },
{ stage: '初试人数', number: 0, company: 'B公司' },
{ stage: '复试人数', number: 153, company: 'B公司' },
{ stage: '录取人数', number: 117, company: 'B公司' },
{ stage: '入职人数', number: 79, company: 'B公司' },
{ stage: '离职人数', number: 15, company: 'B公司' },
{ stage: '回流人数', company: 'B公司' },
];

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

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

expect(
plot.chart.views[1]
.getController('annotation')
.getComponents()
.filter((co) => co.component.cfg.type === 'line')
.map((co) => co.component.cfg.text.content)
).toEqual(['转化率: -∞', '转化率: ∞', '转化率: 76.47%', '转化率: 67.52%', '转化率: 18.99%']);
).toEqual(['转化率: -∞', '转化率: ∞', '转化率: 76.47%', '转化率: 67.52%', '转化率: 18.99%', '转化率: -']);

plot.destroy();
});

it('动态高度漏斗图', async () => {
const plot = new Funnel(createDiv(), {
data: DATA1,
xField: 'stage',
yField: 'number',
dynamicHeight: true,
legend: false,
minSize: 0.1,
});

plot.render();

expect(
plot.chart
.getController('annotation')
.getComponents()
.map((co) => co.component.cfg.text.content)
).toEqual(['转化率: 59.68%', '转化率: -∞', '转化率: ∞', '转化率: -', '转化率: -', '转化率: -']);

plot.destroy();
});
Expand Down
12 changes: 5 additions & 7 deletions src/plots/funnel/geometries/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ export function transformData(

// format 数据
formatData = map(data, (row, index) => {
if (row[yField] !== undefined) {
const percent = row[yField] / maxYFieldValue;
row[FUNNEL_PERCENT] = percent;
row[FUNNEL_MAPPING_VALUE] = (max - min) * percent + min;
// 转化率数据存储前后数据
row[FUNNEL_CONVERSATION] = [get(data, [index - 1, yField]), row[yField]];
}
const percent = (row[yField] || 0) / maxYFieldValue;
row[FUNNEL_PERCENT] = percent;
row[FUNNEL_MAPPING_VALUE] = (max - min) * percent + min;
// 转化率数据存储前后数据
row[FUNNEL_CONVERSATION] = [get(data, [index - 1, yField]), row[yField]];
return row;
});

Expand Down
6 changes: 3 additions & 3 deletions src/plots/funnel/geometries/dynamic-height.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function field(params: Params<FunnelOptions>): Params<FunnelOptions> {
const sum = reduce(
data,
(total, item) => {
return total + item[yField];
return total + (item[yField] || 0);
},
0
);
Expand All @@ -43,7 +43,7 @@ function field(params: Params<FunnelOptions>): Params<FunnelOptions> {
const x = [];
const y = [];

row[FUNNEL_TOTAL_PERCENT] = row[yField] / sum;
row[FUNNEL_TOTAL_PERCENT] = (row[yField] || 0) / sum;

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