1
0

mapComponent.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import * as echarts from 'echarts';
  2. import styles from './mapComponent.less';
  3. import React, { useEffect, useRef } from 'react';
  4. import { queryMapJson, queryProvinceData } from '@/services/dataBoardService';
  5. import { message } from 'antd';
  6. interface propsData {
  7. userData: number;
  8. deviceData: number;
  9. }
  10. /**
  11. * 地图组件
  12. * @constructor
  13. */
  14. const MapComponent: React.FC<propsData> = (props) => {
  15. const chartRef: any = useRef();
  16. const { userData, deviceData } = props;
  17. const renderMapData = (areaList: any) => {
  18. // 初始化统计图对象
  19. const myChart = echarts.init(chartRef.current);
  20. let options: any;
  21. queryMapJson().then((res) => {
  22. if (res && res.code === 0) {
  23. // 获取地图数据
  24. const uploadedDataURL = res.data.map_json;
  25. echarts.registerMap('myMap', uploadedDataURL);
  26. options = {
  27. tooltip: {
  28. show: false,
  29. },
  30. geo: {
  31. map: 'myMap',
  32. zoom: 1.1,
  33. show: true,
  34. roam: false,
  35. label: {
  36. emphasis: {
  37. show: false,
  38. },
  39. },
  40. itemStyle: {
  41. normal: {
  42. areaColor: '#091632',
  43. borderColor: '#1773c3',
  44. shadowColor: '#1773c3',
  45. shadowBlur: 20,
  46. },
  47. },
  48. },
  49. visualMap: {
  50. show: false,
  51. max: 1,
  52. inRange: {
  53. color: ['#ceb10b'],
  54. },
  55. seriesIndex: 0,
  56. },
  57. series: [
  58. {
  59. type: 'map',
  60. map: 'myMap',
  61. zoom: 1.1,
  62. geoIndex: 1,
  63. aspectScale: 0.75, //长宽比
  64. showLegendSymbol: true, // 存在legend时显示
  65. label: {
  66. normal: {
  67. show: true,
  68. textStyle: {
  69. color: '#d3d3d3',
  70. },
  71. },
  72. emphasis: {
  73. show: true,
  74. textStyle: {
  75. color: '#ff0000',
  76. },
  77. },
  78. },
  79. roam: false,
  80. itemStyle: {
  81. normal: {
  82. areaColor: '#031525',
  83. borderColor: '#3B5077',
  84. borderWidth: 1,
  85. },
  86. emphasis: {
  87. areaColor: '#0f2c70',
  88. },
  89. },
  90. data: areaList,
  91. },
  92. ],
  93. };
  94. myChart.setOption(options);
  95. } else {
  96. message.error(res?.message);
  97. }
  98. });
  99. // 组件卸载
  100. return () => {
  101. myChart.clear();
  102. };
  103. };
  104. // 获取数据
  105. const getAreaDataList = () => {
  106. queryProvinceData().then((res) => {
  107. if (res && res.code === 0) {
  108. const areaList = res.data.map((item: any) => {
  109. const nameWithoutCity = item.name.replace(/[省|市]$/, '');
  110. return {
  111. ...item,
  112. name: nameWithoutCity,
  113. };
  114. });
  115. renderMapData(areaList);
  116. }
  117. });
  118. };
  119. useEffect(() => {
  120. getAreaDataList();
  121. }, []);
  122. return (
  123. <div className={styles.container}>
  124. <div ref={chartRef} className={styles.mapContent} />
  125. <div className={styles.content_num}>
  126. <div className={styles.content}>
  127. <div style={{ display: 'flex' }}>
  128. <div className={styles.content_title}>用户数</div>
  129. <div className={styles.content_value}>{userData * 2}</div>
  130. </div>
  131. <div style={{ display: 'flex' }}>
  132. <div className={styles.content_title}>设备数</div>
  133. <div className={styles.content_value}>{deviceData * 6}</div>
  134. </div>
  135. </div>
  136. </div>
  137. </div>
  138. );
  139. };
  140. export default MapComponent;