Skip to content

Commit aad83bc

Browse files
authored
docs: 周常丰富文档和 demo (#2976)
* fix(test): 修复测试问题 * docs(bar): 条形图增加 shape 配置文档 * docs: 增加使用 react-tooltip 的 demo 案例
1 parent b5fcf12 commit aad83bc

File tree

9 files changed

+119
-4
lines changed

9 files changed

+119
-4
lines changed

docs/api/plots/line.zh.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const data = [
127127

128128
`markdown:docs/common/theme.zh.md`
129129

130-
### 自定义
130+
### 定制使用
131131

132132
#### customInfo
133133

examples/column/grouped/demo/meta.json

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@
5959
"en": "IntervalPadding of grouped column plot"
6060
},
6161
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/lRokVhHkrr/61d333f8-9d15-4327-b70f-8e026453cf70.png"
62+
},
63+
{
64+
"filename": "rc-tooltip.jsx",
65+
"title": {
66+
"zh": "自定义 react tooltip",
67+
"en": "Customize React Tooltip"
68+
},
69+
"new": true,
70+
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/djrdqmlrYh/rc-tooltip.png"
6271
}
6372
]
6473
}
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import { Column } from '@antv/g2plot';
4+
import { get } from '@antv/util';
5+
import { Tooltip } from 'rc-for-plots';
6+
7+
const Plot = () => {
8+
const $container = React.useRef();
9+
const $plot = React.useRef();
10+
let $tooltipId;
11+
const [data, changeData] = React.useState([]);
12+
13+
React.useEffect(() => {
14+
// 建议使用 uniqueId
15+
$tooltipId = `tooltip${Math.random()}`;
16+
const div = document.createElement('div');
17+
div.id = $tooltipId;
18+
document.body.appendChild(div);
19+
20+
return () => {
21+
document.body.removeChild(div);
22+
};
23+
}, []);
24+
25+
// 初始化加载,fetch 数据
26+
React.useEffect(() => {
27+
fetch('https://gw.alipayobjects.com/os/antfincdn/PC3daFYjNw/column-data.json')
28+
.then((data) => data.json())
29+
.then((data) => {
30+
changeData(data);
31+
});
32+
}, []);
33+
34+
const options = React.useMemo(() => {
35+
const seriesField = 'type';
36+
const yField = 'value';
37+
38+
return {
39+
data: [],
40+
isGroup: true,
41+
xField: 'city',
42+
yField,
43+
seriesField: 'type',
44+
tooltip: {
45+
// 使用该方式,无法设置 enterable. 若有的话,可以考虑监听 tooltip show/hide/change 事件,进行展示
46+
customContent: (title, items) => {
47+
let container = document.getElementById($tooltipId);
48+
if (!container) {
49+
container = document.createElement('div');
50+
container.id = $tooltipId;
51+
}
52+
const plotBox = $container.current?.getBoundingClientRect();
53+
if (items.length && plotBox) {
54+
const position = { x: items[0].x, y: items[0].y };
55+
const total = items.reduce((sum, d) => sum + (d.data[yField] || null), 0);
56+
const portal = ReactDOM.createPortal(
57+
// 可以使用自定义的 react tooltip 组件
58+
<Tooltip
59+
x={position.x + plotBox.x}
60+
y={position.y + plotBox.y - 36}
61+
header={false}
62+
details={
63+
<Tooltip.Detail
64+
data={[
65+
{ label: '城市', value: title },
66+
...items.map((item) => ({
67+
label: get(item, ['data', seriesField]),
68+
value: get(item, ['data', yField]),
69+
})),
70+
]}
71+
/>
72+
}
73+
footer={<Tooltip.Footer tip={`总计: ${total}`} />}
74+
/>,
75+
container
76+
);
77+
ReactDOM.render(portal, container);
78+
}
79+
80+
return container;
81+
},
82+
},
83+
};
84+
}, []);
85+
86+
React.useEffect(() => {
87+
if (!$container?.current) return;
88+
const newOptions = { ...options, data };
89+
if (!$plot?.current) {
90+
$plot.current = new Column($container.current, newOptions);
91+
$plot.current.render();
92+
} else {
93+
$plot.current.update(newOptions);
94+
}
95+
}, [options]);
96+
97+
React.useEffect(() => {
98+
if ($plot?.current) {
99+
$plot.current.changeData(data);
100+
}
101+
}, [data]);
102+
103+
return <div ref={$container} />;
104+
};
105+
106+
ReactDOM.render(<Plot />, document.getElementById('container'));

examples/line/multiple/demo/meta.json

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
"zh": "折线趋势填充",
3535
"en": "Line with area fill"
3636
},
37-
"new": true,
3837
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/iQezVzeT%24x/line-area.gif"
3938
},
4039
{

examples/more-plots/box/demo/meta.json

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"zh": "设置箱线图 label",
4343
"en": "Box plot label"
4444
},
45-
"new": true,
4645
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/swwLHN2Br0/4fce6597-3c82-44c4-b72c-d73560b18ac4.png"
4746
},
4847
{

examples/more-plots/sunburst/demo/meta.json

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
"zh": "配置激活展示的层级数",
8383
"en": "Config active display depth"
8484
},
85-
"new": true,
8685
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/y9BD5CSBrF/sunburst-active-level.gif"
8786
}
8887
]

gatsby-browser.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ window.react = require('react');
66
window.reactDom = require('react-dom');
77
window.antd = require('antd');
88
window.chromaJs = require('chroma-js');
9+
window.rcForPlots = require('rc-for-plots');
910

1011
require('antd/lib/alert/style/index.css');

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"miz": "^1.0.1",
101101
"npm-run-all": "^4.1.5",
102102
"prettier": "^2.0.1",
103+
"rc-for-plots": "^0.0.1",
103104
"react": "^16.11.0",
104105
"react-dom": "^16.11.0",
105106
"react-i18next": "^11.7.0",

src/utils/data.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { pick } from './pick';
1010
* @param field
1111
*/
1212
export function adjustYMetaByZero(data: Data, field: string): Meta {
13+
if (!data) return {};
1314
// 过滤出数字数据
1415
const numberData = data.filter((datum: Datum) => {
1516
const v = get(datum, [field]);

0 commit comments

Comments
 (0)