Skip to content

Commit a57404f

Browse files
committed
feat: upgrade g2plot to v2
1 parent b95f6ec commit a57404f

File tree

6 files changed

+68
-81
lines changed

6 files changed

+68
-81
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ $ npm install react-g2plot
2626
import ReactDOM from 'react-dom';
2727
import React from 'react';
2828

29-
import { Line } from '@antv/g2plot'
30-
import ReactG2Plot from 'react-g2plot';
29+
import { Line } from '@antv/g2plot'; // import plot from G2Plot
30+
import ReactG2Plot from 'react-g2plot'; // import React wrapper
3131

3232
ReactDOM.render(
3333
<ReactG2Plot
3434
className="your-classname"
3535
Ctor={Line}
36-
config={config}
36+
options={options}
3737
/>,
3838
mountNode,
3939
);
@@ -42,7 +42,7 @@ ReactDOM.render(
4242

4343
## Documents
4444

45-
All documents about `plot` and `config` are [here](https://g2plot.antv.vision/).
45+
All documents about `plot` and `options` are [here](https://g2plot.antv.vision/).
4646

4747

4848

@@ -61,6 +61,4 @@ $ npm run demo
6161

6262
## LICENSE
6363

64-
MIT@[hustcc](https://github.com/hustcc).
65-
66-
64+
MIT@[hustcc](https://github.com/hustcc).

__tests__/index.spec.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,15 @@ describe('ReactG2Plot', () => {
3131
];
3232

3333
// line chart config
34-
const config = {
35-
title: {
36-
visible: true,
37-
text: '曲线折线图',
38-
},
39-
description: {
40-
visible: true,
41-
text: '用平滑的曲线代替折线。',
42-
},
43-
padding: 'auto',
44-
forceFit: true,
34+
const options = {
4535
data,
4636
xField: 'year',
4737
yField: 'value',
4838
smooth: true,
4939
};
5040

5141
// render g2plot react component
52-
ReactDOM.render(<ReactG2Plot className="g2plot-for-react" Ctor={Line} config={config} />, createDiv());
42+
ReactDOM.render(<ReactG2Plot Ctor={Line} options={options} />, createDiv());
5343

5444
expect(document.querySelector('.g2plot-for-react')).not.toBeNull();
5545
});

demo/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
<script src="//unpkg.com/react@16/umd/react.production.min.js"></script>
4747
<script src="//unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
48-
<script src="//unpkg.com/@antv/g2plot"></script>
48+
<script src="//unpkg.com/@antv/g2plot@2.0.0-beta.6"></script>
4949
<script src="./demo.min.js" type="application/javascript"></script>
5050
</body>
5151
</html>

demo/index.tsx

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,68 +13,57 @@ const Data = [
1313
{ year: '1997', value: 7 },
1414
{ year: '1998', value: 9 },
1515
{ year: '1999', value: 13 },
16+
{ year: '2000', value: 4 },
17+
{ year: '2001', value: 7 },
18+
{ year: '2002', value: 2 },
19+
{ year: '2003', value: 20 },
1620
];
1721

18-
const LineConfig = {
19-
title: {
20-
visible: true,
21-
text: '曲线折线图',
22-
},
23-
description: {
24-
visible: true,
25-
text: '用平滑的曲线代替折线。',
26-
},
27-
padding: 'auto',
28-
forceFit: true,
29-
data: Data,
30-
xField: 'year',
31-
yField: 'value',
32-
smooth: true,
33-
};
34-
35-
const ColumnConfig = {
36-
title: {
37-
visible: true,
38-
text: '柱形图',
39-
},
40-
description: {
41-
visible: true,
42-
text: '使用柱形图展示每一年的对比情况。',
43-
},
44-
padding: 'auto',
45-
forceFit: true,
46-
data: Data,
47-
xField: 'year',
48-
yField: 'value',
49-
};
50-
5122
function Entry() {
5223
const [isLine, setIsLine] = useState<boolean>(true);
24+
const [data, setData] = useState<object[]>(Data);
5325

5426
function ref(plot) {
55-
console.log('G2Plot instance Ref', plot);
27+
console.log('G2Plot instance Ref', plot.type);
5628
}
5729

5830
function getCtor() {
5931
return isLine ? Line : Column;
6032
}
6133

62-
function getConfig() {
63-
return isLine ? LineConfig : ColumnConfig;
34+
function getOptions() {
35+
return isLine
36+
? {
37+
data,
38+
xField: 'year',
39+
yField: 'value',
40+
smooth: true,
41+
appendPadding: 12,
42+
}
43+
: {
44+
data,
45+
xField: 'year',
46+
yField: 'value',
47+
appendPadding: 12,
48+
};
6449
}
6550

6651
useEffect(() => {
6752
const id = setInterval(() => {
6853
// 切换
69-
setIsLine(!isLine);
54+
if (Math.random() > 0.5) {
55+
setIsLine(!isLine);
56+
} else {
57+
setData(data.slice(0, 6 + Math.floor(Math.random() * 5)));
58+
}
7059
}, 3000);
7160

7261
return () => {
7362
clearInterval(id);
7463
};
7564
}, [isLine]);
7665

77-
return <ReactG2Plot className="g2plot-for-react" Ctor={getCtor()} config={getConfig()} ref={ref} />;
66+
return <ReactG2Plot Ctor={getCtor()} options={getOptions()} ref={ref} />;
7867
}
7968

8069
ReactDOM.render(<Entry />, document.getElementById('root-container'));

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
22
"name": "react-g2plot",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "Unofficial react component wrapper for @antv/g2plot.",
55
"main": "lib/index.js",
66
"module": "esm/index.js",
7+
"types": "lib/index.d.ts",
78
"files": [
89
"lib",
9-
"esm"
10+
"esm",
11+
"src"
1012
],
1113
"scripts": {
1214
"lint-staged": "lint-staged",
@@ -41,7 +43,7 @@
4143
},
4244
"homepage": "https://github.com/hustcc/react-g2plot",
4345
"devDependencies": {
44-
"@antv/g2plot": "^1.0.0",
46+
"@antv/g2plot": "^2.0.0-beta.6",
4547
"@commitlint/cli": "^8.2.0",
4648
"@types/jest": "^24.0.19",
4749
"@types/react": "^16.9.9",
@@ -64,7 +66,7 @@
6466
"webpack-cli": "^3.3.11"
6567
},
6668
"peerDependencies": {
67-
"@antv/g2plot": "*",
69+
"@antv/g2plot": "^2.0.0",
6870
"react": ">= 16.8.0"
6971
},
7072
"lint-staged": {

src/index.tsx

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import React, { useEffect, useRef, forwardRef } from 'react';
22

3-
export type ReactG2PlotProps = {
3+
export type ReactG2PlotProps<O> = {
44
readonly className?: string;
55
readonly Ctor: any;
6-
readonly config: object;
6+
readonly options: O;
77
};
88

99
/**
10-
* 一个基本可用的 G2Plot React 组件
10+
* 一个业务可用的 G2Plot React 组件
1111
*/
12-
export default forwardRef((props: ReactG2PlotProps, ref: any) => {
13-
const { className, Ctor, config } = props;
12+
export default forwardRef(function<O = any>(props: ReactG2PlotProps<O>, ref: any) {
13+
const { className, Ctor, options } = props;
1414

1515
const $dom = useRef(undefined);
1616
const plotRef = useRef(undefined);
@@ -29,31 +29,39 @@ export default forwardRef((props: ReactG2PlotProps, ref: any) => {
2929
}
3030

3131
/**
32-
* 创建
32+
* 如果存在则更新,否则就创建
3333
*/
34-
function newPlot() {
34+
function renderPlot() {
3535
if (plotRef.current) {
36-
plotRef.current.destroy();
37-
plotRef.current = undefined;
36+
// update
37+
plotRef.current.update(options);
38+
} else {
39+
// new
40+
plotRef.current = new Ctor($dom.current, options);
41+
plotRef.current.render();
3842
}
3943

40-
plotRef.current = new Ctor($dom.current, config);
41-
4244
syncRef(plotRef, ref);
45+
}
4346

44-
plotRef.current.render();
47+
/**
48+
* 销毁
49+
*/
50+
function destoryPlot() {
51+
if (plotRef.current) {
52+
plotRef.current.destroy();
53+
plotRef.current = undefined;
54+
}
4555
}
4656

4757
useEffect(() => {
48-
newPlot();
58+
renderPlot();
4959

5060
return () => {
51-
if (plotRef.current) {
52-
plotRef.current.destroy();
53-
plotRef.current = undefined;
54-
}
61+
destoryPlot();
5562
};
56-
}, [Ctor, config]);
63+
}, [options, Ctor]);
5764

58-
return <div className={className} ref={$dom} />;
65+
// 默认添加 g2plot-for-react 的 class
66+
return <div className={`g2plot-for-react ${className || ''}`} ref={$dom} />;
5967
});

0 commit comments

Comments
 (0)