mapComponent.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import * as echarts from 'echarts';
  2. import styles from './mapComponent.less';
  3. import React, { useEffect, useRef } from 'react';
  4. import chinaMap from '../../mapJson/shandongMapData.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. // 处理数据结构
  38. const convertData = (mapData: string | any[]) => {
  39. const res = [];
  40. for (let i = 0; i < mapData.length; i++) {
  41. const geoCoord = geoCoordMap[mapData[i].name];
  42. if (geoCoord) {
  43. res.push({
  44. name: mapData[i].name,
  45. value: geoCoord.concat(mapData[i].value),
  46. });
  47. }
  48. }
  49. return res;
  50. };
  51. useEffect(() => {
  52. const myChart = echarts.init(chartRef.current);
  53. echarts.registerMap('shandong', data); //此步不可省略,要想展示一个地图,先需要注册,巨坑(官方根本无文档,全靠瞎猜,气死我了快)
  54. const options = {
  55. geo: {
  56. show: true,
  57. map: 'shandong',
  58. roam: false,
  59. zoom: 1.2,
  60. label: {
  61. normal: {
  62. show: false,
  63. },
  64. emphasis: {
  65. show: false,
  66. },
  67. },
  68. itemStyle: {
  69. normal: {
  70. areaColor: '#091632',
  71. borderColor: '#1773c3',
  72. shadowColor: '#1773c3',
  73. shadowBlur: 20,
  74. },
  75. },
  76. },
  77. series: [
  78. {
  79. name: 'light',
  80. type: 'scatter',
  81. coordinateSystem: 'geo',
  82. data: convertData(areaList),
  83. symbolSize: function (val: number[]) {
  84. return val[2];
  85. },
  86. label: {
  87. normal: {
  88. formatter: '{b}',
  89. position: 'right',
  90. show: true,
  91. },
  92. emphasis: {
  93. show: true,
  94. },
  95. },
  96. itemStyle: {
  97. normal: {
  98. color: '#ddb926',
  99. },
  100. },
  101. },
  102. {
  103. type: 'map',
  104. map: 'shandong',
  105. zoom: 1.2,
  106. label: {
  107. normal: {
  108. show: false,
  109. },
  110. emphasis: {
  111. show: false,
  112. textStyle: {
  113. color: '#fff',
  114. },
  115. },
  116. },
  117. roam: false,
  118. itemStyle: {
  119. normal: {
  120. areaColor: '#031525',
  121. borderColor: '#3B5077',
  122. borderWidth: 1,
  123. },
  124. emphasis: {
  125. areaColor: '#0f2c70',
  126. },
  127. },
  128. data: [],
  129. },
  130. ],
  131. };
  132. myChart.setOption(options, true);
  133. // 组件卸载
  134. return () => {
  135. myChart.clear();
  136. };
  137. }, []);
  138. return (
  139. <div className={styles.container}>
  140. <div ref={chartRef} className={styles.mapContent} />
  141. <div className={styles.content_num}>
  142. <div style={{ position: 'relative' }}>
  143. <img src={border} alt="装饰线条" />
  144. <div className={styles.content}>
  145. <datav.DigitalFlop
  146. config={userData}
  147. className={styles.content_value}
  148. style={{ width: '200px', height: '50px' }}
  149. />
  150. <div className={styles.content_title}>用户数</div>
  151. </div>
  152. </div>
  153. </div>
  154. </div>
  155. );
  156. };
  157. export default MapComponent;