Skip to content

Commit 14f564d

Browse files
liuzhenyingaiyin.lzy
and
aiyin.lzy
authored
feat: [V2_combo] 混合图表-双折线图 (#1331)
* feat: add baseCombo & dualline * fix: 部分lint 错误 * fix: npm run fix * fix: 删除无用变量 * feat: push geogemtry 拆分方式 * feat: lint * fix: lint * fix: github test 失败 * feat: 双轴图打平 * feat: line * feat: 更改双轴图引用 * feat: test 用例 * fix: lint * feat: fix lint * fix: fix 不必要的提交 * fix: merge * fix: typescript * feat: 更改 cr 问题 Co-authored-by: aiyin.lzy <[email protected]>
1 parent a5f900c commit 14f564d

File tree

16 files changed

+642
-34
lines changed

16 files changed

+642
-34
lines changed

__tests__/data/pv-uv.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export const PV_DATA = [
2+
{ date: '0601', pv: 123000 },
3+
{ date: '0602', pv: 234000 },
4+
{ date: '0603', pv: 123550 },
5+
{ date: '0604', pv: 365432 },
6+
{ date: '0605', pv: 634121 },
7+
{ date: '0606', pv: 396111 },
8+
{ date: '0607', pv: 127332 },
9+
{ date: '0608', pv: 159987 },
10+
{ date: '0609', pv: 121233 },
11+
];
12+
13+
export const UV_DATA = [
14+
{ date: '0601', uv: 1212 },
15+
{ date: '0602', uv: 4323 },
16+
{ date: '0603', uv: 5121 },
17+
{ date: '0604', uv: 5234 },
18+
{ date: '0605', uv: 7492 },
19+
{ date: '0606', uv: 4351 },
20+
{ date: '0607', uv: 7321 },
21+
{ date: '0608', uv: 1431 },
22+
{ date: '0609', uv: 2054 },
23+
];
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { Biax } from '../../../../src';
2+
import { PV_DATA, UV_DATA } from '../../../data/pv-uv';
3+
import { createDiv } from '../../../utils/dom';
4+
5+
describe('Biax meta', () => {
6+
it('hide axis', () => {
7+
document.body.append('hide axis');
8+
const biax = new Biax(createDiv(), {
9+
width: 400,
10+
height: 500,
11+
data: [PV_DATA, UV_DATA],
12+
xField: 'date',
13+
yField: ['pv', 'uv'],
14+
xAxis: false,
15+
yAxis: [false, false],
16+
});
17+
18+
biax.render();
19+
20+
const leftAxes = biax.chart.getComponents().filter((co) => co.type === 'axis');
21+
const rightAxes = biax.chart.views[0].getComponents().filter((co) => co.type === 'axis');
22+
expect(leftAxes.length + rightAxes.length).toBe(0);
23+
24+
// Biax.destroy();
25+
});
26+
27+
it('axis style', () => {
28+
document.body.append('axis');
29+
const biax = new Biax(createDiv(), {
30+
width: 400,
31+
height: 500,
32+
data: [PV_DATA, UV_DATA],
33+
xField: 'date',
34+
yField: ['pv', 'uv'],
35+
xAxis: {
36+
position: 'bottom',
37+
title: {
38+
offset: 10,
39+
style: {
40+
color: '#333',
41+
autoRotate: true,
42+
},
43+
},
44+
},
45+
yAxis: [
46+
{
47+
position: 'left',
48+
line: {
49+
style: {
50+
lineWidth: 2,
51+
},
52+
},
53+
tickLine: {
54+
style: {
55+
lineWidth: 2,
56+
},
57+
alignTick: true,
58+
},
59+
subTickLine: {
60+
style: {
61+
lineWidth: 1,
62+
count: 4,
63+
},
64+
},
65+
},
66+
{
67+
position: 'right',
68+
line: {
69+
style: {
70+
lineWidth: 2,
71+
},
72+
},
73+
tickLine: {
74+
style: {
75+
lineWidth: 2,
76+
},
77+
alignTick: true,
78+
},
79+
subTickLine: {
80+
style: {
81+
lineWidth: 1,
82+
count: 4,
83+
},
84+
},
85+
},
86+
],
87+
});
88+
89+
biax.render();
90+
91+
// TODO 补充用例
92+
});
93+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Biax } from '../../../../src';
2+
import { PV_DATA, UV_DATA } from '../../../data/pv-uv';
3+
import { createDiv } from '../../../utils/dom';
4+
5+
describe('Biax dualline', () => {
6+
it('Doubal Line', () => {
7+
document.body.append('test Biax doubal line');
8+
9+
const biax = new Biax(createDiv(), {
10+
width: 400,
11+
height: 500,
12+
data: [PV_DATA, UV_DATA],
13+
xField: 'date',
14+
yField: ['pv', 'uv'],
15+
xAxis: false,
16+
geometryConfigs: [
17+
{
18+
geometry: 'Line',
19+
connectNulls: false,
20+
smooth: true,
21+
style: {
22+
stroke: 'red',
23+
},
24+
point: {
25+
size: 3,
26+
shape: 'circle',
27+
},
28+
},
29+
],
30+
});
31+
32+
biax.render();
33+
34+
// line
35+
// const lineGeometrys = biax.chart.geometries.filter((g) => g.type === 'line');
36+
37+
// expect(lineGeometrys.length).toBe(2);
38+
39+
// expect(lineGeometrys[0].attributes.color.values).toEqual(['#180']);
40+
// expect(lineGeometrys[0].attributes.size.values).toEqual([5]);
41+
// // @ts-ignore
42+
// expect(lineGeometrys[0].connectNulls).toBe(false);
43+
// expect(lineGeometrys[0].attributes.shape.values).toEqual(['smooth']);
44+
45+
// expect(lineGeometrys[1].attributes.color.values).toEqual(['#e76c5e']);
46+
// expect(lineGeometrys[1].attributes.size.values).toEqual([2]);
47+
// // @ts-ignore
48+
// expect(lineGeometrys[1].connectNulls).toBe(true);
49+
// expect(lineGeometrys[1].attributes.shape.values).toEqual(['line']);
50+
51+
// // point
52+
// const pointGeometrys = biax.chart.geometries.filter((g) => g.type === 'point');
53+
// expect(pointGeometrys.length).toBe(1);
54+
// expect(pointGeometrys[0].attributes.size.values).toEqual([3]);
55+
// expect(pointGeometrys[0].attributes.shape.values).toEqual(['circle']);
56+
57+
// Biax.destroy();
58+
});
59+
});
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Biax } from '../../../../src';
2+
import { PV_DATA, UV_DATA } from '../../../data/pv-uv';
3+
import { createDiv } from '../../../utils/dom';
4+
5+
describe('Biax data', () => {
6+
it('data', () => {
7+
document.body.append('test Biax data');
8+
const yField = ['pv', 'uv'];
9+
const biax = new Biax(createDiv(), {
10+
width: 400,
11+
height: 500,
12+
data: [PV_DATA, UV_DATA],
13+
xField: 'date',
14+
yField,
15+
});
16+
17+
biax.render();
18+
const leftChart = biax.chart;
19+
const rightView = biax.chart.views[0];
20+
21+
expect(leftChart.geometries[0].data.length).toBe(PV_DATA.length);
22+
expect(rightView.geometries[0].data.length).toBe(UV_DATA.length);
23+
24+
const pvData = PV_DATA.map((item) => item.pv);
25+
26+
expect(leftChart.geometries[0].scales[yField[0]].max).toBe(Math.max(...pvData));
27+
expect(leftChart.geometries[0].scales[yField[0]].min).toBe(Math.min(...pvData));
28+
29+
const uvData = UV_DATA.map((item) => item.uv);
30+
expect(rightView.geometries[0].scales[yField[1]].max).toBe(Math.max(...uvData));
31+
expect(rightView.geometries[0].scales[yField[1]].min).toBe(Math.min(...uvData));
32+
});
33+
});

__tests__/unit/plots/biax/scale.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Biax } from '../../../../src';
2+
import { CountryEconomy } from '../../../data/country-economy';
3+
import { createDiv } from '../../../utils/dom';
4+
5+
describe('Biax meta', () => {
6+
it('meta(include some axis attribute)', () => {
7+
document.body.append('meta(include some axis attribute)');
8+
const biax = new Biax(createDiv(), {
9+
width: 400,
10+
height: 500,
11+
data: CountryEconomy,
12+
xField: 'Country',
13+
yField: ['GDP', 'Population'],
14+
yAxis: [
15+
{
16+
min: 0,
17+
max: 50000,
18+
nice: false,
19+
},
20+
{
21+
tickCount: 5,
22+
nice: false,
23+
},
24+
],
25+
});
26+
27+
biax.render();
28+
29+
const yScales = biax.chart.getYScales();
30+
expect(yScales[0].min).toBe(0);
31+
expect(yScales[0].max).toBe(50000);
32+
// @ts-ignore
33+
expect(yScales[0].nice).toBe(false);
34+
35+
expect(yScales[1].tickCount).toBe(5);
36+
// @ts-ignore
37+
expect(yScales[1].nice).toBe(false);
38+
39+
// biax.destroy();
40+
});
41+
});

src/adaptor/geometries/area.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ import { Params } from '../../core/adaptor';
33
import { Options } from '../../types';
44
import { ShapeStyle } from '../../types/style';
55

6+
type AreaOption = {
7+
/** 是否平滑 */
8+
readonly smooth?: boolean;
9+
/** point color 映射, 提供回调的方式, 不开放 field 映射配置 */
10+
readonly color?: string | string[] | ((series: any) => string);
11+
/** 样式映射 */
12+
readonly style?: ShapeStyle | ((x: any, y: any, series?: any) => ShapeStyle);
13+
};
14+
615
export interface AreaGeometryOptions extends Options {
716
/** x 轴字段 */
817
readonly xField?: string;
918
/** y 轴字段 */
1019
readonly yField?: string;
1120
/** 分组字段 */
1221
readonly seriesField?: string;
13-
readonly area?: {
14-
/** 是否平滑 */
15-
readonly smooth?: boolean;
16-
/** point color 映射, 提供回调的方式, 不开放 field 映射配置 */
17-
readonly color?: string | string[] | ((series: any) => string);
18-
/** 样式映射 */
19-
readonly style?: ShapeStyle | ((x: any, y: any, series?: any) => ShapeStyle);
20-
};
22+
readonly area?: AreaOption;
2123
}
2224

2325
/**

src/adaptor/geometries/line.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ import { Params } from '../../core/adaptor';
33
import { Options } from '../../types';
44
import { ShapeStyle } from '../../types/style';
55

6+
type LineOption = {
7+
/** line color 映射, 提供回调的方式, 不开放 field 映射配置 */
8+
readonly color?: string | string[] | ((series: any) => string);
9+
/** 是否平滑 */
10+
readonly smooth?: boolean;
11+
/** 是否连接空数据 */
12+
readonly connectNulls?: boolean;
13+
/** 样式映射 */
14+
readonly style?: ShapeStyle | ((x: any, y: any, series?: any) => ShapeStyle);
15+
/** 折线宽度 */
16+
readonly size?: number;
17+
};
18+
619
export interface LineGeometryOptions extends Options {
720
/** x 轴字段 */
821
readonly xField?: string;
922
/** y 轴字段 */
1023
readonly yField?: string;
1124
/** 分组字段 */
1225
readonly seriesField?: string;
13-
readonly line?: {
14-
/** line color 映射, 提供回调的方式, 不开放 field 映射配置 */
15-
readonly color?: string | string[] | ((series: any) => string);
16-
/** 是否平滑 */
17-
readonly smooth?: boolean;
18-
/** 是否连接空数据 */
19-
readonly connectNulls?: boolean;
20-
/** 样式映射 */
21-
readonly style?: ShapeStyle | ((x: any, y: any, series?: any) => ShapeStyle);
22-
/** 折线宽度 */
23-
readonly size?: number;
24-
};
26+
readonly line?: LineOption;
2527
}
2628

2729
/**

src/adaptor/geometries/point.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@ import { Params } from '../../core/adaptor';
33
import { Options } from '../../types';
44
import { ShapeStyle } from '../../types/style';
55

6+
type PointOption = {
7+
/** point color 映射, 提供回调的方式, 不开放 field 映射配置 */
8+
readonly color?: string | string[] | ((series: any) => string);
9+
/** point shape 映射, 提供回调的方式, 不开放 field 映射配置 */
10+
readonly shape?: string | ((x: any, y: any, series?: any) => string);
11+
/** 大小映射, 提供回调的方式, 不开放 field 映射配置 */
12+
readonly size?: number | ((x: any, y: any, series?: any) => number);
13+
/** 样式映射 */
14+
readonly style?: ShapeStyle | ((x: any, y: any, series?: any) => ShapeStyle);
15+
};
16+
617
export interface PointGeometryOptions extends Options {
718
/** x 轴字段 */
819
readonly xField?: string;
920
/** y 轴字段 */
1021
readonly yField?: string;
1122
/** 分组字段 */
1223
readonly seriesField?: string;
13-
readonly point?: {
14-
/** point color 映射, 提供回调的方式, 不开放 field 映射配置 */
15-
readonly color?: string | string[] | ((series: any) => string);
16-
/** point shape 映射, 提供回调的方式, 不开放 field 映射配置 */
17-
readonly shape?: string | ((x: any, y: any, series?: any) => string);
18-
/** 大小映射, 提供回调的方式, 不开放 field 映射配置 */
19-
readonly size?: number | ((x: any, y: any, series?: any) => number);
20-
/** 样式映射 */
21-
readonly style?: ShapeStyle | ((x: any, y: any, series?: any) => ShapeStyle);
22-
};
24+
readonly point?: PointOption;
2325
}
2426

2527
/**

src/common/helper.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Chart, Geometry } from '@antv/g2';
1+
import { Geometry, View } from '@antv/g2';
22

33
/**
44
* 在 Chart 中查找第一个指定 type 类型的 geometry
55
* @param chart
66
* @param type
77
*/
8-
export function findGeometry(chart: Chart, type: string): Geometry {
8+
export function findGeometry(chart: View, type: string): Geometry {
99
return chart.geometries.find((g: Geometry) => g.type === type);
1010
}

src/core/adaptor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Chart } from '@antv/g2';
1+
import { View } from '@antv/g2';
22

33
/**
44
* adaptor flow 的参数
55
*/
66
export type Params<O> = {
7-
readonly chart: Chart;
7+
readonly chart: View;
88
readonly options: O;
99
};
1010

src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export { Pie, PieOptions } from './plots/pie';
2121
// 散点图及类型定义
2222
export { Scatter, ScatterOptions } from './plots/scatter';
2323

24+
// 混合图形
25+
export { Biax, BiaxOption } from './plots/biax';
26+
2427
// 迷你折线图及类型定义
2528
export { TinyLine, TinyLineOptions } from './plots/tiny-line';
2629

0 commit comments

Comments
 (0)