Skip to content

Commit ecadba6

Browse files
author
吴磊
committed
feat: 升级eChart到5.3.0解决横向图表overflow之后,宽度更新失败问题,补充部分单元测试
1 parent 2a3ba4e commit ecadba6

File tree

5 files changed

+187
-5
lines changed

5 files changed

+187
-5
lines changed

frontend/craco.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ module.exports = {
139139
setupFiles: ['jest-canvas-mock'],
140140
});
141141
},
142+
modulePaths: ['../'],
142143
},
143144

144145
devServer: {

frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"axios": "^0.21.1",
9999
"classnames": "^2.3.1",
100100
"debounce-promise": "3.1.2",
101-
"echarts": "^5.1.1",
101+
"echarts": "^5.3.0",
102102
"echarts-wordcloud": "^2.0.0",
103103
"file-saver": "^2.0.5",
104104
"flexlayout-react": "^0.5.12",

frontend/src/app/components/ChartGraph/BasicFunnelChart/BasicFunnelChart.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from 'app/types/ChartConfig';
2424
import ChartDataSetDTO, { IChartDataSet } from 'app/types/ChartDataSet';
2525
import {
26-
getAutoFunnelPosition,
26+
getAutoFunnelTopPosition,
2727
getColumnRenderName,
2828
getExtraSeriesDataFormat,
2929
getExtraSeriesRowData,
@@ -217,7 +217,7 @@ class BasicFunnelChart extends Chart {
217217
let positions = {};
218218
let orient = {};
219219

220-
const top = getAutoFunnelPosition({
220+
const top = getAutoFunnelTopPosition({
221221
chart: this.chart,
222222
sort,
223223
legendPos,
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/**
2+
* Datart
3+
*
4+
* Copyright 2021
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import {
20+
getAutoFunnelTopPosition,
21+
getIntervalShow,
22+
hadAxisLabelOverflowConfig,
23+
} from '../chartHelper';
24+
25+
describe('test getIntervalShow return boolean', () => {
26+
it('getIntervalShow return true when arg is number', () => {
27+
expect(getIntervalShow(0)).toBeTruthy();
28+
});
29+
30+
it('1. getIntervalShow return true when arg is string', () => {
31+
expect(getIntervalShow('0')).toBeTruthy();
32+
});
33+
34+
it('getIntervalShow return false when arg is "auto"', () => {
35+
expect(getIntervalShow('auto')).toBeFalsy();
36+
});
37+
38+
// Interval 已经经过处理,不可能为 undefined
39+
it('getIntervalShow return false when arg is null', () => {
40+
expect(getIntervalShow(null)).toBeFalsy();
41+
});
42+
});
43+
44+
describe('test hadAxisLabelOverflowConfig return boolean', () => {
45+
const axisLabel = {
46+
overflow: 'break',
47+
interval: 0,
48+
show: true,
49+
};
50+
51+
const getOptions = (label: any = null, horizon = false) => ({
52+
[horizon ? 'yAxis' : 'xAxis']: label ? [label] : null,
53+
});
54+
55+
it('hadAxisLabelOverflowConfig return false when options is null', () => {
56+
expect(hadAxisLabelOverflowConfig(getOptions())).toBeFalsy();
57+
});
58+
59+
it('hadAxisLabelOverflowConfig return false when options.xAxis[0].axisLabel is null', () => {
60+
expect(hadAxisLabelOverflowConfig(getOptions({}))).toBeFalsy();
61+
});
62+
63+
it('hadAxisLabelOverflowConfig return false when get options.yAxis opts', () => {
64+
expect(hadAxisLabelOverflowConfig(getOptions({}, true))).toBeFalsy();
65+
});
66+
67+
it('hadAxisLabelOverflowConfig return true when get options.xAxis', () => {
68+
expect(hadAxisLabelOverflowConfig(getOptions({ axisLabel }))).toBeTruthy();
69+
});
70+
71+
it('hadAxisLabelOverflowConfig return false when get options.yAxis opts', () => {
72+
expect(hadAxisLabelOverflowConfig(getOptions({}, true))).toBeFalsy();
73+
});
74+
75+
it('hadAxisLabelOverflowConfig return true when get options.yAxis', () => {
76+
expect(
77+
hadAxisLabelOverflowConfig(getOptions({ axisLabel }, true), true),
78+
).toBeTruthy();
79+
});
80+
81+
it('hadAxisLabelOverflowConfig return false when show false', () => {
82+
expect(
83+
hadAxisLabelOverflowConfig(
84+
getOptions({
85+
axisLabel: {
86+
axisLabel,
87+
show: false,
88+
},
89+
}),
90+
),
91+
).toBeFalsy();
92+
});
93+
94+
it('hadAxisLabelOverflowConfig return false when interval "auto"', () => {
95+
expect(
96+
hadAxisLabelOverflowConfig(
97+
getOptions({
98+
axisLabel: {
99+
axisLabel,
100+
interval: 'auto',
101+
},
102+
}),
103+
),
104+
).toBeFalsy();
105+
});
106+
107+
it('hadAxisLabelOverflowConfig return false when overflow null"', () => {
108+
expect(
109+
hadAxisLabelOverflowConfig(
110+
getOptions({
111+
axisLabel: {
112+
axisLabel,
113+
overflow: null,
114+
},
115+
}),
116+
),
117+
).toBeFalsy();
118+
});
119+
});
120+
121+
describe('test getAutoFunnelTopPosition return number', () => {
122+
it('getAutoFunnelTopPosition return 8 when legendPos is not left or right', () => {
123+
expect(
124+
getAutoFunnelTopPosition({
125+
legendPos: 'top',
126+
} as any),
127+
).toEqual(8);
128+
});
129+
it('getAutoFunnelTopPosition return 16 when legendPos is left or right and height is 0 or null', () => {
130+
expect(
131+
getAutoFunnelTopPosition({
132+
legendPos: 'left',
133+
} as any),
134+
).toEqual(16);
135+
136+
expect(
137+
getAutoFunnelTopPosition({
138+
legendPos: 'left',
139+
height: 0,
140+
} as any),
141+
).toEqual(16);
142+
});
143+
144+
it('getAutoFunnelTopPosition return 16 when sort is ascending', () => {
145+
expect(
146+
getAutoFunnelTopPosition({
147+
legendPos: 'left',
148+
height: 32,
149+
sort: 'ascending',
150+
} as any),
151+
).toEqual(16);
152+
});
153+
154+
it('getAutoFunnelTopPosition return 16 when chart.getHeight return null/0', () => {
155+
expect(
156+
getAutoFunnelTopPosition({
157+
legendPos: 'left',
158+
height: 32,
159+
sort: 'none',
160+
chart: {
161+
getHeight: () => 0,
162+
},
163+
} as any),
164+
).toEqual(16);
165+
});
166+
167+
it('getAutoFunnelTopPosition return chart.getHeight - height - marginBottom', () => {
168+
expect(
169+
getAutoFunnelTopPosition({
170+
legendPos: 'left',
171+
height: 100,
172+
sort: 'none',
173+
chart: {
174+
getHeight: () => 800,
175+
},
176+
} as any),
177+
).toEqual(800 - 100 - 24);
178+
});
179+
});

frontend/src/app/utils/chartHelper.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,6 @@ export function setOptionsByAxisLabelOverflow(config: CommonChartConfig) {
14681468
const axisOpts = !horizon ? xAxis : yAxis;
14691469
const axisName = !horizon ? 'xAxis' : 'yAxis';
14701470

1471-
//
14721471
const data = axisOpts.data || [];
14731472

14741473
const dataLength = data.length;
@@ -1503,6 +1502,8 @@ export function setOptionsByAxisLabelOverflow(config: CommonChartConfig) {
15031502

15041503
// 处理 每个刻度宽度
15051504
const setWidth = width => {
1505+
// 水平图表使用默认宽度
1506+
if (horizon) return 40;
15061507
return parseInt(String((width - dataLength * 8) / dataLength));
15071508
};
15081509
// model 渲染未完成的兼容性方案,一般只在图表初始化阶段,还没有拿到model。
@@ -1537,7 +1538,7 @@ export function setOptionsByAxisLabelOverflow(config: CommonChartConfig) {
15371538
return commonOpts;
15381539
}
15391540

1540-
export const getAutoFunnelPosition = (config: {
1541+
export const getAutoFunnelTopPosition = (config: {
15411542
chart: ECharts;
15421543
height: number;
15431544
sort: 'ascending' | 'descending' | 'none';
@@ -1551,5 +1552,6 @@ export const getAutoFunnelPosition = (config: {
15511552

15521553
const chartHeight = chart.getHeight();
15531554
if (!chartHeight) return 16;
1555+
// 24 marginBottom
15541556
return chartHeight - 24 - height;
15551557
};

0 commit comments

Comments
 (0)