|
| 1 | +import React, { |
| 2 | + useEffect, useRef, |
| 3 | +} from 'react'; |
| 4 | +import echarts from '../../echarts'; |
| 5 | + |
| 6 | +const frontMap = { |
| 7 | + type: 'map', |
| 8 | + map: 'china', |
| 9 | + label: { |
| 10 | + emphasis: { |
| 11 | + show: false, |
| 12 | + }, |
| 13 | + }, |
| 14 | + roam: false, |
| 15 | + itemStyle: { |
| 16 | + normal: { |
| 17 | + areaColor: '#091a5c', |
| 18 | + borderColor: '#17398e', |
| 19 | + borderWidth: 1, |
| 20 | + }, |
| 21 | + emphasis: { |
| 22 | + areaColor: '#2a333d', |
| 23 | + }, |
| 24 | + }, |
| 25 | + tooltip: { |
| 26 | + show: false, |
| 27 | + }, |
| 28 | +}; |
| 29 | + |
| 30 | +const convertData = (data, map) => { |
| 31 | + const res = []; |
| 32 | + for (let i = 0; i < data.length; i += 1) { |
| 33 | + const dataItem = data[i]; |
| 34 | + const fromCoord = map[dataItem[0].name]; |
| 35 | + const toCoord = map[dataItem[1].name]; |
| 36 | + if (fromCoord && toCoord) { |
| 37 | + res.push({ |
| 38 | + fromName: dataItem[0].name, |
| 39 | + toName: dataItem[1].name, |
| 40 | + coords: [fromCoord, toCoord], |
| 41 | + }); |
| 42 | + } |
| 43 | + } |
| 44 | + return res; |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * 初始化chart |
| 49 | + */ |
| 50 | +function initChart(el, opt={}) { |
| 51 | + if (!el) { |
| 52 | + return; |
| 53 | + } |
| 54 | + const option = { |
| 55 | + tooltip: { |
| 56 | + trigger: 'item', |
| 57 | + }, |
| 58 | + geo: { |
| 59 | + map: 'china', |
| 60 | + label: { |
| 61 | + emphasis: { |
| 62 | + show: false, |
| 63 | + }, |
| 64 | + }, |
| 65 | + roam: false, |
| 66 | + itemStyle: { |
| 67 | + normal: { |
| 68 | + shadowColor: 'rgba(31, 205, 255, 0.8)', |
| 69 | + shadowBlur: 30, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + series: [frontMap], |
| 74 | + }; |
| 75 | + el.setOption(option, opt); |
| 76 | +} |
| 77 | + |
| 78 | +function updateChart(el) { |
| 79 | + const location = {"id":null,"province":"广东省","city":"广州市","county":"番禺区","longitude":113.41239161249568,"latitude":23.067357458890267} |
| 80 | + const geoCoordMap = { |
| 81 | + [location.province]: [location.longitude, location.latitude], |
| 82 | + }; |
| 83 | + |
| 84 | + const GZData = []; |
| 85 | + |
| 86 | + const list = [ |
| 87 | + {"id":null,"province":"广东省","city":"广州市","county":"番禺区","longitude":113.3842,"latitude":22.9377,"projectCounts":5,"workerCounts":19}, |
| 88 | + {"id":null,"province":"湖南省","city":"邵阳市","county":"隆回县","longitude":111.0086,"latitude":27.1158,"projectCounts":1,"workerCounts":8}, |
| 89 | + {"id":null,"province":"北京市","city":"北京市","county":"西城区","longitude":116.3978,"latitude":39.9037,"projectCounts":1,"workerCounts":0} |
| 90 | + ] |
| 91 | + |
| 92 | + list.sort((a, b) => b.workerCounts - a.workerCounts); |
| 93 | + const maxWorkerCounts = list.length ? list[0].workerCounts : 0; |
| 94 | + const minWorkerCounts = list.length ? list[list.length - 1].workerCounts : 0; |
| 95 | + |
| 96 | + list.map((p) => { |
| 97 | + geoCoordMap[p.province] = [p.longitude, p.latitude]; |
| 98 | + GZData.push([ |
| 99 | + { name: location.province }, |
| 100 | + { name: p.province, value: p.workerCounts }, |
| 101 | + ]); |
| 102 | + return p; |
| 103 | + }); |
| 104 | + |
| 105 | + const series = []; |
| 106 | + [[location.province, GZData]].forEach((item) => { |
| 107 | + const tail = { |
| 108 | + // name: `${item[0]}`, |
| 109 | + type: 'lines', |
| 110 | + zlevel: 1, |
| 111 | + effect: { |
| 112 | + show: true, |
| 113 | + period: 3, |
| 114 | + trailLength: 0.6, |
| 115 | + color: '#fff', |
| 116 | + symbolSize: 3, |
| 117 | + }, |
| 118 | + lineStyle: { |
| 119 | + normal: { |
| 120 | + color: '#fff', |
| 121 | + width: 0, |
| 122 | + curveness: 0.2, |
| 123 | + }, |
| 124 | + }, |
| 125 | + data: convertData(item[1], geoCoordMap), |
| 126 | + }; |
| 127 | + const dot1 = { |
| 128 | + // name: `${item[0]}`, |
| 129 | + type: 'lines', |
| 130 | + zlevel: 2, |
| 131 | + effect: { |
| 132 | + show: true, |
| 133 | + period: 3, |
| 134 | + trailLength: 0, |
| 135 | + symbol: 'circle', |
| 136 | + symbolSize: 5, |
| 137 | + color: '#3cffcb', |
| 138 | + }, |
| 139 | + lineStyle: { |
| 140 | + normal: { |
| 141 | + color: '#fff', |
| 142 | + width: 2, |
| 143 | + opacity: 0.6, |
| 144 | + curveness: 0.2, |
| 145 | + }, |
| 146 | + }, |
| 147 | + data: convertData(item[1], geoCoordMap), |
| 148 | + }; |
| 149 | + const city = { |
| 150 | + // name: `${item[0]}`, |
| 151 | + type: 'effectScatter', |
| 152 | + coordinateSystem: 'geo', |
| 153 | + zlevel: 2, |
| 154 | + rippleEffect: { |
| 155 | + brushType: 'stroke', |
| 156 | + }, |
| 157 | + // label: { |
| 158 | + // normal: { |
| 159 | + // show: true, |
| 160 | + // position: 'right', |
| 161 | + // }, |
| 162 | + // formatter: '{b}', |
| 163 | + // }, |
| 164 | + symbolSize(dot) { |
| 165 | + console.log('dot', dot); |
| 166 | + const val = dot[2]; |
| 167 | + if (minWorkerCounts === maxWorkerCounts) { |
| 168 | + return 15; |
| 169 | + } |
| 170 | + return (val - minWorkerCounts) / (maxWorkerCounts - minWorkerCounts) * 5 + 10; |
| 171 | + }, |
| 172 | + itemStyle: { |
| 173 | + normal: { |
| 174 | + color: '#50febf', |
| 175 | + }, |
| 176 | + }, |
| 177 | + data: item[1].map((dataItem, idx) => ({ |
| 178 | + name: dataItem[1].name, |
| 179 | + value: geoCoordMap[dataItem[1].name].concat([dataItem[1].value]), |
| 180 | + tooltip: { |
| 181 | + formatter: `${dataItem[1].name}:${list[idx].projectCounts}个`, |
| 182 | + }, |
| 183 | + label: { |
| 184 | + normal: { |
| 185 | + show: true, |
| 186 | + position: 'right', |
| 187 | + formatter: '{b}', |
| 188 | + }, |
| 189 | + }, |
| 190 | + })), |
| 191 | + }; |
| 192 | + series.push(tail, dot1, city, frontMap); |
| 193 | + // series.push(tail, city, frontMap); |
| 194 | + console.log(series); |
| 195 | + }); |
| 196 | + el.setOption({ |
| 197 | + series, |
| 198 | + }); |
| 199 | +} |
| 200 | + |
| 201 | + |
| 202 | +function DataChart() { |
| 203 | + const myChart = useRef(null); |
| 204 | + useEffect(() => { |
| 205 | + myChart.current = echarts.init(myChart.current); |
| 206 | + initChart(myChart.current, { |
| 207 | + notMerge: true, |
| 208 | + }); |
| 209 | + updateChart(myChart.current); |
| 210 | + return () => { |
| 211 | + // console.log('销毁时'); |
| 212 | + myChart.current.dispose(); |
| 213 | + }; |
| 214 | + }, []); |
| 215 | + // const chartDOMRef = useCallback((el) => { |
| 216 | + // if (el) { |
| 217 | + // if (!myChart.current) { |
| 218 | + // myChart.current = echarts.init(el); |
| 219 | + // } |
| 220 | + // initChart(myChart.current, { |
| 221 | + // notMerge: true, |
| 222 | + // }); |
| 223 | + // } else { |
| 224 | + // if (!myChart.current) { |
| 225 | + // return; |
| 226 | + // } |
| 227 | + // myChart.current.dispose(); |
| 228 | + // } |
| 229 | + // }, []); |
| 230 | + |
| 231 | + return ( |
| 232 | + <> |
| 233 | + <div ref={myChart} style={{ height: 300, backgroundColor: '#dedede' }} /> |
| 234 | + </> |
| 235 | + ); |
| 236 | +} |
| 237 | + |
| 238 | + |
| 239 | +export default DataChart; |
0 commit comments