1
0

mapComponent.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import * as echarts from 'echarts';
  2. import styles from './mapComponent.less';
  3. import React, { useEffect, useRef } from 'react';
  4. import chinaMap from '../../mapJson/chinaMapData.json';
  5. import border from '../../../public/assets/decoration_six.png';
  6. import * as datav from '@jiaminghi/data-view-react';
  7. interface propsData {
  8. userData: object;
  9. areaList: any;
  10. }
  11. /**
  12. * 地图组件
  13. * @constructor
  14. */
  15. const MapComponent: React.FC<propsData> = (props) => {
  16. const chartRef: any = useRef();
  17. const { userData, areaList } = props;
  18. const data: any = chinaMap; //地图的数据来自之前引入的json文件
  19. const geoCoordMap = {
  20. 济南市: [117, 36.65],
  21. 青岛市: [120.33, 36.07],
  22. 淄博市: [118.05, 36.78],
  23. 枣庄市: [117.57, 34.86],
  24. 东营市: [118.49, 37.46],
  25. 烟台市: [121.39, 37.52],
  26. 潍坊市: [119.1, 36.62],
  27. 济宁市: [116.59, 35.38],
  28. 泰安市: [117.13, 36.18],
  29. 威海市: [122.1, 37.5],
  30. 日照市: [119.46, 35.42],
  31. 临沂市: [118.35, 35.05],
  32. 德州市: [116.29, 37.45],
  33. 聊城市: [115.97, 36.45],
  34. 滨州市: [118.03, 37.36],
  35. 菏泽市: [115.47, 35.25],
  36. };
  37. const convertData = (mapData: string | any[]) => {
  38. const res = [];
  39. for (let i = 0; i < mapData.length; i++) {
  40. const geoCoord = geoCoordMap[mapData[i].name];
  41. if (geoCoord) {
  42. res.push({
  43. name: mapData[i].name,
  44. value: geoCoord.concat(mapData[i].value),
  45. });
  46. }
  47. }
  48. return res;
  49. };
  50. useEffect(() => {
  51. const myChart = echarts.init(chartRef.current);
  52. echarts.registerMap('china', data); //此步不可省略,要想展示一个地图,先需要注册,巨坑(官方根本无文档,全靠瞎猜,气死我了快)
  53. const options = {
  54. geo: {
  55. map: 'china',
  56. show: true,
  57. roam: false,
  58. zoom: 1.4,
  59. top: 130,
  60. label: {
  61. emphasis: {
  62. show: false,
  63. },
  64. },
  65. itemStyle: {
  66. normal: {
  67. areaColor: '#091632',
  68. borderColor: '#1773c3',
  69. shadowColor: '#1773c3',
  70. shadowBlur: 20,
  71. },
  72. },
  73. regions: [
  74. {
  75. name: '山东省',
  76. itemStyle: {
  77. areaColor: '#146637',
  78. },
  79. },
  80. ],
  81. },
  82. series: [
  83. {
  84. type: 'map',
  85. map: 'china',
  86. zoom: 1.4,
  87. top: 130,
  88. label: {
  89. normal: {
  90. show: false,
  91. },
  92. emphasis: {
  93. show: false,
  94. textStyle: {
  95. color: '#fff',
  96. },
  97. },
  98. },
  99. roam: false,
  100. itemStyle: {
  101. normal: {
  102. areaColor: '#031525',
  103. borderColor: '#3B5077',
  104. borderWidth: 1,
  105. },
  106. emphasis: {
  107. areaColor: '#0f2c70',
  108. },
  109. },
  110. data: [],
  111. },
  112. {
  113. type: 'scatter',
  114. coordinateSystem: 'geo',
  115. data: convertData(areaList),
  116. symbolSize: function (val: number[]) {
  117. return val[2] / 20;
  118. },
  119. label: {
  120. normal: {
  121. formatter: '{b}',
  122. position: 'right',
  123. show: false,
  124. },
  125. emphasis: {
  126. show: true,
  127. },
  128. },
  129. itemStyle: {
  130. normal: {
  131. color: '#ddb926',
  132. },
  133. },
  134. },
  135. ],
  136. };
  137. myChart.setOption(options, true);
  138. // 组件卸载
  139. return () => {
  140. myChart.dispose();
  141. };
  142. }, []);
  143. return (
  144. <div className={styles.container}>
  145. <div ref={chartRef} className={styles.mapContent} />
  146. <div className={styles.content_num}>
  147. <div style={{ position: 'relative' }}>
  148. <img src={border} alt="装饰线条" />
  149. <div className={styles.content}>
  150. <datav.DigitalFlop
  151. config={userData}
  152. className={styles.content_value}
  153. style={{ width: '200px', height: '50px' }}
  154. />
  155. <div className={styles.content_title}>用户数</div>
  156. </div>
  157. </div>
  158. </div>
  159. </div>
  160. );
  161. };
  162. export default MapComponent;