当前位置:   article > 正文

用echarts 中的地图来显示区域分布情况(vue),包括地理数据来源生成的制作注意事项_echarts 地域分布

echarts 地域分布

由于国家测绘部门的一些相关法律规定,现在版本的ecahrts 中的地图资源已经不能制作和直接使用,只能通过自己注册地图regiseterMap(name,json),来实现地图的区域,这些数据,学GIS的获取和制作应该都不是什么大问题,只需将相关区域的地理要素转换成geojson即可,如果不知道怎么转换,我的文章WebGIS常用的空间分析,地图发布,底图自定义,QGIS插件,地理矢量数据格式转换工具等资源汇总里面有相关的介绍,但是数据生成还是需要注意以下几点:

1.echarts 加载地图时注意geojson文件中的feature的propertis中的name字段名称一定要为name,
2.并且必须带有name的属性,不然echarts加载不到json中的地图,name不能大写,或者的单个字母大写,否则就会出错.

有了数据之后生成效果图就很方便了找到echarts的相关的配置和demo 进行设施即可,官方文档自己网上看就是了,我直接上我的效果地址):http://47.106.197.17/zhongshanUniversity/#/GIS/chartmap  和组件相关代码如下:

  1. <template>
  2. <div style="height:80%;width:100%;background:#1e1e1e"/>
  3. </template>
  4. <script>
  5. import echarts from "echarts";
  6. import $ from "jquery";
  7. import { debounce } from "@/utils";
  8. export default {
  9. props: {
  10. className: {
  11. type: String,
  12. default: "chart"
  13. },
  14. datalist: {
  15. type: [Object, Array],
  16. default: {
  17. gmq_count: 10,
  18. ftq_count: 20,
  19. dpxq_count: 30,
  20. lhq_count: 40,
  21. ytq_count: 1000,
  22. baq_count: 500,
  23. nsq_count: 60,
  24. lhuq_count: 90,
  25. lgq_count: 80,
  26. psq_count: 10
  27. }
  28. },
  29. name: {
  30. type: Array
  31. }
  32. },
  33. data() {
  34. return {
  35. chart: null,
  36. maxrangeV: ""
  37. };
  38. },
  39. mounted() {
  40. this.initChart();
  41. this.getMaxrange();
  42. this.__resizeHanlder = debounce(() => {
  43. if (this.chart) {
  44. this.chart.resize();
  45. }
  46. }, 100);
  47. window.addEventListener("resize", this.__resizeHanlder);
  48. },
  49. beforeDestroy() {
  50. if (!this.chart) {
  51. return;
  52. }
  53. window.removeEventListener("resize", this.__resizeHanlder);
  54. this.chart.dispose();
  55. this.chart = null;
  56. },
  57. watch: {
  58. datalist() {
  59. this.initChart();
  60. this.getMaxrange();
  61. }
  62. },
  63. methods: {
  64. initChart() {
  65. let self = this;
  66. this.chart = echarts.init(this.$el);
  67. this.chart.on("click", function(param) {
  68. self.$emit("clickfunc", param);
  69. // alert(param.name);
  70. });
  71. var uploadedDataURL = "static/map/map.geojson"; //"http://echarts.baidu.com/examples/data-gl/asset/data/buildings.json"; szdistrict
  72. // 显示加载动画效果,可以在加载数据前手动调用该接口显示加载动画,在数据加载完成后调用 hideLoading 隐藏加载动画。https://blog.csdn.net/GRAY_KEY/article/details/81295961
  73. this.chart.showLoading();
  74. $.get(uploadedDataURL, function(usaJson) {
  75. self.chart.hideLoading();
  76. echarts.registerMap("USA", usaJson);
  77. let option = {
  78. title: {
  79. text: "",
  80. subtext: "",
  81. sublink: "https://blog.csdn.net/qq_26991807",
  82. left: "right"
  83. },
  84. tooltip: {
  85. trigger: "item",
  86. showDelay: 0,
  87. transitionDuration: 0.2,
  88. formatter: function(params) {
  89. var value = (params.value + "").split(".");
  90. value = value[0].replace(/(\d{1,3})(?=(?:\d{3})+(?!\d))/g, "$1,");
  91. return params.seriesName + "<br/>" + params.name + ": " + value;
  92. }
  93. },
  94. visualMap: {
  95. left: "right",
  96. min: 0,
  97. max: self.maxrangeV,
  98. inRange: {
  99. color: [
  100. "#313695",
  101. "#4575b4",
  102. "#74add1",
  103. "#abd9e9",
  104. "#e0f3f8",
  105. "#ffffbf",
  106. "#fee090",
  107. "#fdae61",
  108. "#f46d43",
  109. "#d73027",
  110. "#a50026"
  111. /*
  112. '#29FF6C',
  113. '#29FF7E',
  114. '#29FF90',
  115. ' #29FFA2',
  116. '#29FFB4',
  117. '#29FFC6',
  118. '#29FFD7',
  119. '#29FFE9',
  120. '#29FFFB',
  121. '#29F1FF',
  122. '#29DFFF',
  123. '#29CDFF',
  124. '#29BCFF',
  125. '#29AAFF',
  126. '#2998FF',
  127. '#2986FF',
  128. ' #2974FF',
  129. '#2962FF',
  130. '#2950FF',
  131. '#293FFF',
  132. '#292DFF' */
  133. ]
  134. },
  135. text: ["High", "Low"], // 文本,默认为数值文本
  136. calculable: true,
  137. textStyle: {
  138. color: "#fff"
  139. }
  140. },
  141. toolbox: {
  142. show: true,
  143. //orient: 'vertical',
  144. left: "right",
  145. top: "top",
  146. feature: {
  147. // dataView: { readOnly: true },
  148. restore: {},
  149. saveAsImage: {}
  150. },
  151. iconStyle: {
  152. borderColor: "#fff"
  153. }
  154. },
  155. series: [
  156. {
  157. name: "深圳",
  158. type: "map",
  159. roam: true,
  160. map: "USA",
  161. center: [114.16315, 22.60894],
  162. zoom: 1.1,
  163. itemStyle: {
  164. emphasis: { label: { show: true } },
  165. normal: {
  166. label: {
  167. show: true,
  168. formatter: function(params) {
  169. var value = (params.value + "").split(".");
  170. value = value[0].replace(
  171. /(\d{1,3})(?=(?:\d{3})+(?!\d))/g,
  172. "$1,"
  173. );
  174. return params.name + ":" + value;
  175. },
  176. textStyle: {
  177. color: "#fff",
  178. align: "left",
  179. fontSize: 16
  180. }
  181. }
  182. }
  183. },
  184. // 文本位置修正
  185. textFixed: {
  186. // Alaska: [20, -20]
  187. },
  188. data: [
  189. { name: "福田区", value: self.datalist.ftq_count },
  190. { name: "罗湖区", value: self.datalist.lhuq_count },
  191. { name: "龙华区", value: self.datalist.lhq_count },
  192. { name: "龙岗区", value: self.datalist.lgq_count },
  193. { name: "南山区", value: self.datalist.nsq_count },
  194. { name: "坪山区", value: self.datalist.psq_count },
  195. { name: "盐田区", value: self.datalist.ytq_count },
  196. { name: "大鹏新区", value: self.datalist.dpxq_count },
  197. { name: "光明新区", value: self.datalist.gmq_count },
  198. { name: "宝安区", value: self.datalist.baq_count }
  199. ]
  200. }
  201. ]
  202. };
  203. self.chart.setOption(option);
  204. // self.chart.on("click", function(params) {
  205. // alert(params.name + params.value);
  206. // });
  207. });
  208. },
  209. getMaxrange() {
  210. let v1 = 0;
  211. for (let v in this.datalist) {
  212. if (this.datalist[v] > v1) {
  213. v1 = this.datalist[v];
  214. }
  215. }
  216. this.maxrangeV = v1;
  217. }
  218. }
  219. };
  220. </script>

地图的geojson格式示意如下:

  1. {"type":"Feature",
  2. "geometry":{"type":"Polygon","coordinates":[[[114.07240271810434,22.591215901890433],
  3. [114.07277218853474,22.59022699132922],[114.07345287044389,22.58930674419094],
  4. [114.07358415928655,22.589101366040097],...]]]},
  5. "properties":{"stroke":"#555555","stroke-width":2,"stroke-pacity":1,"fill":"#555555","fill-
  6. opacity":0.5,"name":"罗湖区","height":1.1}
  7. }
效果图
标题

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/109602
推荐阅读