Skip to content

Commit 160bebb

Browse files
lxfu1liufu.lf
and
liufu.lf
authored
feat: 基于 dumi 新增临时 docs (#1283)
* feat: add docs * feat(docs): add dumi docs for dev Co-authored-by: liufu.lf <[email protected]>
1 parent ec33cd6 commit 160bebb

File tree

13 files changed

+393
-4
lines changed

13 files changed

+393
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@ demos/assets/screenshots
7979

8080
.vscode
8181
/stats.json
82+
.umi

.umirc.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defineConfig } from 'dumi';
2+
3+
export default defineConfig({
4+
mode: 'site',
5+
title: 'G2Plot',
6+
base: '/',
7+
publicPath: '/',
8+
exportStatic: {},
9+
logo: 'https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg',
10+
navs: [null, { title: 'GitHub', path: 'https://github.com/antvis/G2Plot' }],
11+
analytics: {
12+
ga: 'UA-72788897-12',
13+
},
14+
chainWebpack(memo) {
15+
memo.plugins.delete('copy');
16+
},
17+
// more config: https://d.umijs.org/config
18+
});

docs/demos/scatter.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: 散点图
3+
order: 17
4+
---
5+
6+
# 散点图
7+
8+
## Scatter
9+
10+
```tsx
11+
import React, { useEffect, useState, useRef } from 'react';
12+
import { Scatter } from '@antv/g2plot';
13+
14+
const Page: React.FC = () => {
15+
const [data, setData] = useState([]);
16+
const chart = useRef();
17+
const fetchData = () => {
18+
fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/scatter.json')
19+
.then((response) => response.json())
20+
.then((json) => setData(json))
21+
.catch((error) => {
22+
console.log('fetch data failed', error);
23+
});
24+
};
25+
26+
useEffect(() => {
27+
chart.current = new Scatter('container', {
28+
width: 400,
29+
height: 300,
30+
appendPadding: 10,
31+
data,
32+
xField: 'weight',
33+
yField: 'height',
34+
seriesField: 'gender',
35+
shape: ['circle', 'square'],
36+
symbolSize: 5,
37+
tooltip: {
38+
showCrosshairs: true,
39+
crosshairs: {
40+
type: 'xy',
41+
},
42+
},
43+
});
44+
chart.current.render();
45+
46+
fetchData();
47+
}, []);
48+
49+
useEffect(() => {
50+
if (data.length) {
51+
chart.current.changeData(data);
52+
}
53+
}, [data]);
54+
55+
return <div id="container" />;
56+
};
57+
export default Page;
58+
```

docs/index.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: G2Plot - 简单好用的图表库
3+
order: 10
4+
hero:
5+
title: G2Plot
6+
desc: 简单好用的图表库
7+
actions:
8+
- text: 快速上手
9+
link: /demos/scatter
10+
features:
11+
- icon: https://gw.alipayobjects.com/zos/bmw-prod/881dc458-f20b-407b-947a-95104b5ec82b/k79dm8ih_w144_h144.png
12+
title: 开箱即用
13+
desc: 一个简单的配置就能呈现优雅、标准的图表
14+
- icon: https://gw.alipayobjects.com/zos/antfincdn/oyqsrPh0Kg/houyuan.png
15+
title: 后援强大
16+
desc: AntV团队支持,基于 G2 实现,简单方便、专业可靠、无限可能
17+
- icon: https://gw.alipayobjects.com/zos/antfincdn/aKCFl7vDAB/tubiao.png
18+
title: 图表完善
19+
desc: 支持全量的 G2 图表,几乎做到同步升级更新
20+
footer: Open-source MIT Licensed | Copyright © 2019-present
21+
---
22+
23+
## 反馈
24+
25+
请访问 [GitHub](https://github.com/antvis/G2Plot/issues)

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"url": "https://github.com/antvis/g2plot"
2424
},
2525
"scripts": {
26+
"start": "dumi dev",
2627
"fix": "eslint --ext .ts ./src ./__tests__ --fix && prettier --write ./src ./__tests__",
2728
"build": "run-s clean lib dist",
2829
"clean": "rimraf lib esm dist",
@@ -61,6 +62,7 @@
6162
"@typescript-eslint/parser": "^2.0.0",
6263
"babel-loader": "^8.1.0",
6364
"conventional-changelog-cli": "^2.0.28",
65+
"dumi": "^1.0.33",
6466
"eslint": "^6.1.0",
6567
"eslint-config-prettier": "^6.0.0",
6668
"eslint-plugin-prettier": "^3.1.0",

src/core/plot.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Chart } from '@antv/g2';
22
import { bind } from 'size-sensor';
33
import { Adaptor } from './adaptor';
4-
import { Options } from '../types';
4+
import { Options, Data } from '../types';
55

66
/**
77
* 所有 plot 的基类
@@ -19,7 +19,7 @@ export abstract class Plot<O extends Options> {
1919
private unbind: () => void;
2020

2121
constructor(container: string | HTMLElement, options: O) {
22-
this.container = typeof container === 'string' ? document.querySelector(container) : container;
22+
this.container = typeof container === 'string' ? document.getElementById(container) : container;
2323
this.options = options;
2424

2525
this.createG2();
@@ -77,6 +77,15 @@ export abstract class Plot<O extends Options> {
7777
this.render();
7878
}
7979

80+
/**
81+
* 更新数据
82+
* @param options
83+
*/
84+
public changeData(data: Data) {
85+
this.chart.changeData(data);
86+
this.chart.render();
87+
}
88+
8089
/**
8190
* 修改画布大小
8291
* @param width

src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ export * from './types';
55

66
// 折线图及类型定义
77
export { Line, LineOptions } from './plots/line';
8+
9+
//散点图及类型定义
10+
export { Scatter, ScatterOptions } from './plots/scatter';

src/plots/scatter/adaptor.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Params } from '../../core/adaptor';
2+
import { flow } from '../../utils';
3+
import { ScatterOptions } from './types';
4+
5+
/**
6+
* 字段
7+
* @param params
8+
*/
9+
function field(params: Params<ScatterOptions>): Params<ScatterOptions> {
10+
const { chart, options } = params;
11+
const { data, xField, yField, seriesField, color, symbolSize, shape } = options;
12+
13+
// 散点图操作逻辑
14+
chart.data(data);
15+
const geometry = chart.point().position(`${xField}*${yField}`);
16+
17+
// 颜色映射
18+
if (seriesField) {
19+
geometry.color(seriesField, color);
20+
geometry.shape(seriesField, shape);
21+
}
22+
23+
// 大小映射
24+
if (symbolSize) {
25+
const size = typeof symbolSize === 'number' ? ([symbolSize, symbolSize] as [number, number]) : symbolSize;
26+
geometry.size(yField, size);
27+
}
28+
return params;
29+
}
30+
31+
/**
32+
* meta 配置
33+
* @param params
34+
*/
35+
function meta(params: Params<ScatterOptions>): Params<ScatterOptions> {
36+
// TODO
37+
return params;
38+
}
39+
40+
/**
41+
* axis 配置
42+
* @param params
43+
*/
44+
function axis(params: Params<ScatterOptions>): Params<ScatterOptions> {
45+
// TODO
46+
return params;
47+
}
48+
49+
/**
50+
* legend 配置
51+
* @param params
52+
*/
53+
function legend(params: Params<ScatterOptions>): Params<ScatterOptions> {
54+
// TODO
55+
return params;
56+
}
57+
58+
/**
59+
* tooltip 配置
60+
* @param params
61+
*/
62+
function tooltip(params: Params<ScatterOptions>): Params<ScatterOptions> {
63+
const {
64+
chart,
65+
options: { tooltip },
66+
} = params;
67+
68+
if (tooltip) {
69+
chart.tooltip({ ...tooltip });
70+
}
71+
return params;
72+
}
73+
74+
/**
75+
* 散点图适配器
76+
* @param chart
77+
* @param options
78+
*/
79+
export function adaptor(params: Params<ScatterOptions>) {
80+
// flow 的方式处理所有的配置到 G2 API
81+
flow(field, meta, axis, legend, tooltip)(params);
82+
}

src/plots/scatter/index.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Plot } from '../../core/plot';
2+
import { ScatterOptions } from './types';
3+
import { adaptor } from './adaptor';
4+
import { Adaptor } from '../../core/adaptor';
5+
6+
export { ScatterOptions };
7+
8+
export class Scatter extends Plot<ScatterOptions> {
9+
/** 图表类型 */
10+
public type: string = 'point';
11+
12+
/**
13+
* 获取散点图的适配器
14+
*/
15+
protected getSchemaAdaptor(): Adaptor<ScatterOptions> {
16+
return adaptor;
17+
}
18+
}

src/plots/scatter/types.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Options } from '../../types';
2+
3+
export interface ScatterOptions extends Options {
4+
/** x 轴字段 */
5+
readonly xField: string;
6+
7+
/** y 轴字段 */
8+
readonly yField: string;
9+
10+
/** 分组字段 */
11+
readonly seriesField?: string;
12+
13+
/** 散点图大小 */
14+
readonly symbolSize?: number | [number, number] | ((value: number) => number);
15+
16+
/** 散点图形状 */
17+
readonly shape?: string[] | ((item: any[]) => string | string[]);
18+
}

src/types/axis.ts

+98-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,98 @@
1-
export type Axis = {};
1+
import { TextStyle } from './common';
2+
3+
export type Axis = {
4+
/**
5+
* 是否可见
6+
*/
7+
readonly visible?: boolean;
8+
9+
/**
10+
* 期望的坐标轴刻度数量,非最终结果
11+
*/
12+
readonly tickCount?: number;
13+
14+
/**
15+
* 坐标轴刻度间隔
16+
*/
17+
readonly tickInterval?: number;
18+
19+
/**
20+
* 坐标轴最小值
21+
*/
22+
readonly min?: number;
23+
24+
/**
25+
* 坐标轴最大值
26+
*/
27+
readonly max?: number;
28+
29+
/**
30+
* 网格线
31+
*/
32+
readonly grid?: {
33+
/** 是否可见 */
34+
readonly visible?: boolean;
35+
/** 网格线样式 */
36+
readonly style?: TextStyle;
37+
};
38+
39+
/**
40+
* 坐标轴轴线
41+
*/
42+
readonly line?: {
43+
/** 是否可见 */
44+
readonly visible?: boolean;
45+
/** 轴线样式 */
46+
readonly style?: TextStyle;
47+
};
48+
49+
/**
50+
* 坐标轴刻度
51+
*/
52+
readonly tickLine?: {
53+
/** 是否可见 */
54+
readonly visible?: boolean;
55+
/** 刻度样式 */
56+
readonly style?: TextStyle;
57+
};
58+
59+
/**
60+
* 坐标轴标签
61+
*/
62+
readonly label?: {
63+
/** 是否可见 */
64+
readonly visible?: boolean;
65+
/** 标签格式化 */
66+
readonly formatter?: (label: string) => string;
67+
/** 后缀 */
68+
readonly suffix?: string;
69+
/** 前缀 */
70+
readonly prefix?: string;
71+
/** 标签精度 */
72+
readonly precision?: number;
73+
/** 在 x 方向上的偏移量*/
74+
readonly offsetX?: number;
75+
/** 在 y 方向上的偏移量 */
76+
readonly offsetY?: number;
77+
/** 是否自动隐藏 */
78+
readonly autoHide?: boolean;
79+
/** 是否自动旋转 */
80+
readonly autoRotate?: boolean;
81+
/** 标签样式 */
82+
readonly style?: TextStyle;
83+
};
84+
85+
/**
86+
* 坐标轴标题
87+
*/
88+
readonly title?: {
89+
/** 是否可见 */
90+
readonly visible?: boolean;
91+
/** 标题文字 */
92+
readonly text?: string;
93+
/** 偏移量 */
94+
readonly offset?: number;
95+
/** 标题样式 */
96+
readonly style?: TextStyle;
97+
};
98+
};

0 commit comments

Comments
 (0)