|
| 1 | +import React from 'react'; |
| 2 | +import ReactDOM from 'react-dom'; |
| 3 | +import { Area } from '@ant-design/plots'; |
| 4 | +import { useRef } from 'react'; |
| 5 | +import { useEffect } from 'react'; |
| 6 | + |
| 7 | +document.getElementById('container').innerHTML = ` |
| 8 | +<div id="focus" ></div> |
| 9 | +<div id="context"></div> |
| 10 | +`; |
| 11 | + |
| 12 | +const commonConfig = { |
| 13 | + data: { |
| 14 | + type: 'fetch', |
| 15 | + value: 'https://gw.alipayobjects.com/os/bmw-prod/551d80c6-a6be-4f3c-a82a-abd739e12977.csv', |
| 16 | + }, |
| 17 | + xField: 'date', |
| 18 | + yField: 'close', |
| 19 | + animate: false, |
| 20 | +}; |
| 21 | + |
| 22 | +function createPathRender(compute) { |
| 23 | + return (group, options, document) => { |
| 24 | + if (!group.handle) { |
| 25 | + const path = document.createElement('path'); |
| 26 | + group.handle = path; |
| 27 | + group.appendChild(group.handle); |
| 28 | + } |
| 29 | + const { handle } = group; |
| 30 | + const { width, height, ...rest } = options; |
| 31 | + if (width === undefined || height === undefined) return handle; |
| 32 | + handle.attr({ ...compute(width, height), ...rest }); |
| 33 | + return handle; |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +const DemoInteraction = () => { |
| 38 | + const focusRef = useRef(); |
| 39 | + const contextRef = useRef(); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + const focusChart = focusRef.current.chart; |
| 43 | + const contextChart = contextRef.current.chart; |
| 44 | + |
| 45 | + focusChart.on('brush:filter', (e) => { |
| 46 | + const { nativeEvent } = e; |
| 47 | + if (!nativeEvent) return; |
| 48 | + const { selection } = e.data; |
| 49 | + const { x: scaleX } = focusChart.getScale(); |
| 50 | + const [[x1, x2]] = selection; |
| 51 | + const domainX = scaleX.getOptions().domain; |
| 52 | + if (x1 === domainX[0] && x2 === domainX[1]) { |
| 53 | + contextChart.emit('brush:remove', {}); |
| 54 | + } else { |
| 55 | + contextChart.emit('brush:highlight', { data: { selection } }); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + contextChart.on('brush:highlight', (e) => { |
| 60 | + const { nativeEvent, data } = e; |
| 61 | + if (!nativeEvent) return; |
| 62 | + const { selection } = data; |
| 63 | + focusChart.emit('brush:filter', { data: { selection } }); |
| 64 | + }); |
| 65 | + |
| 66 | + contextChart.on('brush:remove', (e) => { |
| 67 | + const { nativeEvent } = e; |
| 68 | + if (!nativeEvent) return; |
| 69 | + const { x: scaleX, y: scaleY } = contextChart.getScale(); |
| 70 | + const selection = [scaleX.getOptions().domain, scaleY.getOptions().domain]; |
| 71 | + focusChart.emit('brush:filter', { data: { selection } }); |
| 72 | + }); |
| 73 | + }, []); |
| 74 | + |
| 75 | + const focusConfig = { |
| 76 | + ...commonConfig, |
| 77 | + autoFit: false, |
| 78 | + height: 360, |
| 79 | + paddingLeft: 60, |
| 80 | + interaction: { |
| 81 | + tooltip: false, |
| 82 | + brushXFilter: true, |
| 83 | + }, |
| 84 | + }; |
| 85 | + |
| 86 | + const contextConfig = { |
| 87 | + ...commonConfig, |
| 88 | + autoFit: false, |
| 89 | + paddingTop: 0, |
| 90 | + paddingBottom: 0, |
| 91 | + height: 90, |
| 92 | + paddingLeft: 60, |
| 93 | + axis: false, |
| 94 | + interaction: { |
| 95 | + tooltip: false, |
| 96 | + brushXHighlight: { |
| 97 | + series: true, |
| 98 | + maskOpacity: 0.3, |
| 99 | + maskFill: '#777', |
| 100 | + maskHandleWRender: createPathRender((width, height) => ({ |
| 101 | + d: 'M-0.5,31.5c-2.5,0,-4.5,2,-4.5,4.5v30c0,2.5,2,4.5,4.5,4.5V31.5z', |
| 102 | + transform: `translate(${width / 2}, ${-height / 2})`, |
| 103 | + })), |
| 104 | + maskHandleERender: createPathRender((width, height) => ({ |
| 105 | + d: 'M0.5,31.5c2.5,0,4.5,2,4.5,4.5v30c0,2.5,-2,4.5,-4.5,4.5V31.5z', |
| 106 | + transform: `translate(${width / 2}, ${-height / 2})`, |
| 107 | + })), |
| 108 | + maskHandleEFill: '#D3D8E0', |
| 109 | + maskHandleWFill: '#D3D8E0', |
| 110 | + }, |
| 111 | + }, |
| 112 | + }; |
| 113 | + |
| 114 | + return ( |
| 115 | + <div style={{ display: 'flex', flexDirection: 'column' }}> |
| 116 | + <Area {...focusConfig} getElementById="focus" onReady={(plot) => (focusRef.current = plot)} /> |
| 117 | + <Area {...contextConfig} getElementById="context" onReady={(plot) => (contextRef.current = plot)} /> |
| 118 | + </div> |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
| 122 | +ReactDOM.render(<DemoInteraction />, document.getElementById('container')); |
0 commit comments