Skip to content

Commit 6346fcd

Browse files
authored
feat(plugin): add plugin plot for customize (#1519)
1 parent 471379b commit 6346fcd

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

__tests__/unit/plugin/index-spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { G2Plot } from '../../../src';
2+
import { createDiv } from '../../utils/dom';
3+
import { partySupport } from '../../data/party-support';
4+
import { StepLineAdaptor, StepLineOption } from './step-line';
5+
6+
describe('plugin - G2Plot', () => {
7+
it('StepLine', () => {
8+
const plot = new G2Plot<StepLineOption>(
9+
createDiv(),
10+
{
11+
width: 400,
12+
height: 300,
13+
appendPadding: 10,
14+
data: partySupport.filter((o) => o.type === 'FF'),
15+
xField: 'date',
16+
yField: 'value',
17+
stepType: 'hv',
18+
},
19+
StepLineAdaptor
20+
);
21+
22+
plot.render();
23+
24+
expect(plot.type).toBe('g2-plot');
25+
expect(plot.chart.geometries[0].type).toBe('line');
26+
});
27+
});

__tests__/unit/plugin/step-line.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Params, Options } from '../../../src';
2+
3+
export interface StepLineOption extends Options {
4+
readonly xField: string;
5+
readonly yField: string;
6+
readonly stepType?: 'vh' | 'hv' | 'vhv' | 'hvh';
7+
}
8+
9+
/** 这个方法作为一个包 export 出去 */
10+
export function StepLineAdaptor(params: Params<StepLineOption>): Params<StepLineOption> {
11+
const { chart, options } = params;
12+
const { xField, yField, stepType = 'vh', data } = options;
13+
14+
chart.line().position(`${xField}*${yField}`).shape(stepType);
15+
chart.data(data);
16+
17+
return params;
18+
}

src/core/plot.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getContainerSize, getAllElements } from '../utils';
88
import { Adaptor } from './adaptor';
99

1010
/** 单独 pick 出来的用于基类的类型定义 */
11-
type PickOptions = Pick<
11+
export type PickOptions = Pick<
1212
Options,
1313
'width' | 'height' | 'padding' | 'appendPadding' | 'renderer' | 'pixelRatio' | 'autoFit'
1414
>;

src/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,11 @@ export { Heatmap, HeatmapOptions } from './plots/heatmap';
6262

6363
// 箱线图及类型定义 | author by [BBSQQ](https://github.com/BBSQQ)
6464
export { Box, BoxOptions } from './plots/box';
65+
66+
// 以下开放自定义图表开发的能力(目前仅仅是孵化中)
67+
68+
/** 所有开放图表都使用 G2Plot 作为入口开发,理论上官方的所有图表都可以走 G2Plot 的入口(暂时不处理) */
69+
export { G2Plot } from './plugin';
70+
71+
/** 开发 adaptor 可能会用到 flow 方法,不强制使用 */
72+
export { flow } from './utils';

src/plugin/index.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Plot, PickOptions } from '../core/plot';
2+
import { Adaptor } from '../core/adaptor';
3+
/**
4+
* 给 G2Plot 提供非常简单的开放开发的机制。目的是能够让社区和业务上自己基于 G2Plot 开发自己的定制图表库。主要分成几类图表:
5+
* 1. 领域专业的图表,内部同学因为没有场景,不一定能做的完善。
6+
* 2. 定制业务的图表,不具备通用性
7+
* 3. 趣味性的可视化组件
8+
* 然后官方可以根据社区的情况,可以进行一些官方推荐和采纳。
9+
*
10+
* 如果使用?
11+
*
12+
* ```ts
13+
* import { G2Plot } from '@antv/g2plot';
14+
* import { GeoWorldMap, GeoWorldMapOptions } from 'g2plot-geo-world-map';
15+
*
16+
* const plot = new G2Plot('container', {
17+
* geoJson: '',
18+
* longitude: '',
19+
* latitude: '',
20+
* }, GeoWorldMap);
21+
*
22+
* plot.render();
23+
* ```
24+
*/
25+
export class G2Plot<O extends PickOptions> extends Plot<O> {
26+
/** 统一为 any plot */
27+
public readonly type = 'g2-plot';
28+
29+
/** 外部传入的 adaptor 函数 */
30+
private adaptor: Adaptor<O>;
31+
32+
/**
33+
* 相比普通图表增加 adaptor 参数。后续还可以考虑增加 defaultOptions
34+
* @param container
35+
* @param options
36+
* @param adaptor
37+
*/
38+
constructor(container: string | HTMLElement, options: O, adaptor: Adaptor<O>) {
39+
super(container, options);
40+
41+
this.adaptor = adaptor;
42+
}
43+
44+
/**
45+
* 实现父类方法,直接使用传入的
46+
*/
47+
protected getSchemaAdaptor(): Adaptor<O> {
48+
return this.adaptor;
49+
}
50+
}

0 commit comments

Comments
 (0)