连续视觉映射

https://echarts.apache.org/zh/option.html#visualMap
image.png

分段式视觉映射

分段式视觉映射, 根据数据大小,各省显示不同颜色
https://echarts.apache.org/zh/option.html#visualMap-piecewise.pieces

image.png
image.png

ChinaMap.jsx

地图按需引入参考 https://echarts.apache.org/examples/zh/editor.html?c=map-usa

  1. import React, { useEffect, useState } from 'react';
  2. import {array, number, string, oneOfType} from 'prop-types';
  3. import {
  4. TooltipComponent,
  5. GridComponent,
  6. LegendComponent,
  7. VisualMapComponent,
  8. GeoComponent
  9. } from 'echarts/components';
  10. import { MapChart } from 'echarts/charts';
  11. import { UniversalTransition } from 'echarts/features'
  12. import ECharts from '../../ECharts';
  13. import chinaOption from './chinaOption'
  14. ChinaMap.propTypes = {
  15. data: array,
  16. xdata: array,
  17. height: oneOfType([number, string]),
  18. className: string,
  19. theme: string,
  20. };
  21. ChinaMap.defaultProps = {
  22. height: '100%',
  23. }
  24. function ChinaMap({data, xData, height, className, theme}) {
  25. const [options, setOptions] = useState({})
  26. useEffect(update, [data, xData])
  27. function update() {
  28. const config = chinaOption({data, xData, theme});
  29. setOptions(config);
  30. }
  31. const attrs = {
  32. // renderType: 'svg',
  33. className,
  34. height,
  35. options,
  36. components: [
  37. VisualMapComponent,
  38. GeoComponent,
  39. TooltipComponent,
  40. GridComponent,
  41. LegendComponent,
  42. MapChart,
  43. UniversalTransition
  44. ]
  45. }
  46. return (
  47. <ECharts {...attrs} />
  48. );
  49. }
  50. export default ChinaMap;

options

  1. import * as echarts from 'echarts';
  2. import {chinaJson} from '../index';
  3. // name 一定要和 chinaJson中的 name保持一致,否则渲染错误
  4. // name 一定要和 chinaJson中的 name保持一致,否则渲染错误
  5. const mockData = [
  6. {
  7. name: "南海诸岛",
  8. value: 100,
  9. },
  10. {
  11. name: "北京市",
  12. value: 540
  13. },
  14. {
  15. name: "天津市",
  16. value: 130
  17. },
  18. {
  19. name: "上海市",
  20. value: 400
  21. },
  22. {
  23. name: "重庆市",
  24. value: 750
  25. },
  26. {
  27. name: "河北省",
  28. value: 130
  29. },
  30. {
  31. name: "河南省",
  32. value: 830
  33. },
  34. {
  35. name: "云南省",
  36. value: 110
  37. },
  38. {
  39. name: "辽宁省",
  40. value: 19
  41. },
  42. {
  43. name: "黑龙江省",
  44. value: 150
  45. },
  46. {
  47. name: "湖南省",
  48. value: 690
  49. },
  50. {
  51. name: "安徽省",
  52. value: 60
  53. },
  54. {
  55. name: "山东省",
  56. value: 39
  57. },
  58. {
  59. name: "新疆维吾尔自治区",
  60. value: 4
  61. },
  62. {
  63. name: "江苏省",
  64. value: 31
  65. },
  66. {
  67. name: "浙江省",
  68. value: 104
  69. },
  70. {
  71. name: "江西省",
  72. value: 36
  73. },
  74. {
  75. name: "湖北省",
  76. value: 52
  77. },
  78. {
  79. name: "广西壮族自治区",
  80. value: 33
  81. },
  82. {
  83. name: "甘肃省",
  84. value: 7
  85. },
  86. {
  87. name: "山西省",
  88. value: 5
  89. },
  90. {
  91. name: "内蒙古自治区",
  92. value: 778
  93. },
  94. {
  95. name: "陕西省",
  96. value: 22
  97. },
  98. {
  99. name: "吉林省",
  100. value: 4
  101. },
  102. {
  103. name: "福建省",
  104. value: 18
  105. },
  106. {
  107. name: "贵州省",
  108. value: 5
  109. },
  110. {
  111. name: "广东省",
  112. value: 398
  113. },
  114. {
  115. name: "青海省",
  116. value: 1
  117. },
  118. {
  119. name: "西藏自治区",
  120. value: 2
  121. },
  122. {
  123. name: "四川省",
  124. value: 44
  125. },
  126. {
  127. name: "宁夏回族自治区",
  128. value: 4
  129. },
  130. {
  131. name: "海南省",
  132. value: 22
  133. },
  134. {
  135. name: "台湾省",
  136. value: 3
  137. },
  138. {
  139. name: "香港特别行政区",
  140. value: 5
  141. },
  142. {
  143. name: "澳门特别行政区",
  144. value: 355
  145. }
  146. ];
  147. function chinaOption() {
  148. echarts.registerMap('china', chinaJson);
  149. return {
  150. visualMap: {
  151. show: true,
  152. // type: 'continuous',
  153. bottom: 24,
  154. left: 24,
  155. showLabel: true,
  156. // 连续性视觉映射的参数
  157. // text: ['高', '低'],// 取值范围的文字
  158. // inRange: {
  159. // color: ['#e0ffff', '#006edd'] // 取值范围的颜色
  160. // },
  161. // 分段式视觉映射, 根据数据大小,各省显示不同颜色
  162. // https://echarts.apache.org/zh/option.html#visualMap-piecewise.pieces
  163. pieces: [
  164. {
  165. gte: 100,
  166. label: "1000+",
  167. color: "#1f307b"
  168. },
  169. {
  170. gte: 500,
  171. lt: 999,
  172. label: "500-999",
  173. color: "#3c57ce"
  174. },
  175. {
  176. gte: 100,
  177. lt: 499,
  178. label: "100-499",
  179. color: "#6f83db"
  180. },
  181. {
  182. gte: 10,
  183. lt: 99,
  184. label: "10-99",
  185. color: "#9face7"
  186. },
  187. {
  188. lt: 10,
  189. label: '10-',
  190. color: "#bcc5ee"
  191. }
  192. ]
  193. },
  194. // 中国地图设置
  195. geo: {
  196. map: "china",
  197. roam: true,
  198. zoom: 1.3,
  199. scaleLimit: {
  200. min: 1, max: 8
  201. },
  202. top: 120,
  203. label: {
  204. normal: {
  205. show: false,
  206. fontSize: 12,
  207. color: "rgba(0,0,0,0.65)"
  208. }
  209. },
  210. itemStyle: {
  211. normal: {
  212. borderColor: "rgba(0, 0, 0, 0.2)"
  213. },
  214. emphasis: {
  215. areaColor: "rgba(0,110,221,0.85)", // 鼠标选择区域颜色
  216. color: '#fff',
  217. shadowOffsetX: 0,
  218. shadowOffsetY: 0,
  219. borderWidth: 0
  220. }
  221. },
  222. // 高亮状态下
  223. // emphasis: {
  224. // label: {
  225. // color: '#fff',
  226. // }
  227. // }
  228. },
  229. series: [
  230. {
  231. name: "人均GDP",
  232. type: "map",
  233. data: mockData,
  234. geoIndex: 0,
  235. // map: 'china',
  236. // emphasis: {
  237. // label: {
  238. // show: true
  239. // }
  240. // },
  241. }
  242. ],
  243. tooltip: {
  244. triggerOn: "mousemove",
  245. padding: 8,
  246. borderWidth: 1,
  247. borderColor: '#409eff',
  248. backgroundColor: 'rgba(255,255,255,0.7)',
  249. textStyle: {
  250. color: 'rgba(0,0,0,.5)',
  251. },
  252. },
  253. }
  254. }
  255. export default chinaOption;