|
| 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')); |
0 commit comments