mapTwo.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <div style="width: 680px">
  3. <div ref="charts" style="width: 100%;height: 920px;margin-top: -80px"></div>
  4. <img
  5. id="mapBackground"
  6. src="@/assets/images/main/map_bg.jpg"
  7. alt=""
  8. style="display: none"
  9. />
  10. <img
  11. v-if="showBack"
  12. @click="beBack"
  13. src="@/assets/images/main/back.png"
  14. style="width: 34px;height: 30px;cursor: pointer;position: absolute;right: 50px;bottom: 50px;"
  15. />
  16. </div>
  17. </template>
  18. <script>
  19. import * as echarts from "echarts";
  20. import shanxi from "./shanxi.json"
  21. import 大同市 from '@/views/screen/map/大同市.json'
  22. import 朔州市 from '@/views/screen/map/朔州市.json'
  23. import 忻州市 from '@/views/screen/map/忻州市.json'
  24. import 吕梁市 from '@/views/screen/map/吕梁市.json'
  25. import 太原市 from '@/views/screen/map/太原市.json'
  26. import 阳泉市 from '@/views/screen/map/阳泉市.json'
  27. import 晋中市 from '@/views/screen/map/晋中市.json'
  28. import 临汾市 from '@/views/screen/map/临汾市.json'
  29. import 长治市 from '@/views/screen/map/长治市.json'
  30. import 运城市 from '@/views/screen/map/运城市.json'
  31. import 晋城市 from '@/views/screen/map/晋城市.json'
  32. let imgElement
  33. export default {
  34. props: {
  35. mapData: Array
  36. },
  37. data() {
  38. return {
  39. appOrg: "10001",
  40. showBack: false,
  41. dataSource: {
  42. 大同市: 大同市,
  43. 朔州市: 朔州市,
  44. 忻州市: 忻州市,
  45. 吕梁市: 吕梁市,
  46. 太原市: 太原市,
  47. 阳泉市: 阳泉市,
  48. 晋中市: 晋中市,
  49. 临汾市: 临汾市,
  50. 长治市: 长治市,
  51. 运城市: 运城市,
  52. 晋城市: 晋城市,
  53. },
  54. points:[
  55. { name: "太原市", coordinate: [112.3493, 37.7570], appOrg: "1A003", x: "399", y: "2" },
  56. { name: "大同市", coordinate: [113.3002, 40.0768], appOrg: "1B001", x: "399", y: "2" },
  57. { name: "阳泉市", coordinate: [113.6805, 37.7863], appOrg: "1C001", x: "399", y: "2" },
  58. { name: "长治市", coordinate: [113.1163, 36.1954], appOrg: "1D001", x: "399", y: "2" },
  59. { name: "晋城市", coordinate: [113.1513, 35.5907], appOrg: "1E001", x: "399", y: "2" },
  60. { name: "朔州市", coordinate: [112.4329, 39.5312], appOrg: "1F001", x: "399", y: "2" },
  61. { name: "晋中市", coordinate: [113.2527, 37.3870], appOrg: "1K001", x: "399", y: "2" },
  62. { name: "临汾市", coordinate: [111.5186, 35.8582], appOrg: "1L001", x: "399", y: "2" },
  63. { name: "忻州市", coordinate: [112.7341, 38.6167], appOrg: "1H001", x: "399", y: "2" },
  64. { name: "运城市", coordinate: [110.9069, 34.9264], appOrg: "1M001", x: "399", y: "2" },
  65. { name: "吕梁市", coordinate: [111.1443, 37.4183], appOrg: "1J001", x: "399", y: "2" }
  66. ]
  67. }
  68. },
  69. created () {},
  70. mounted() {},
  71. methods: {
  72. start(){
  73. this.$nextTick(() => {
  74. let i = 0
  75. for (let it of this.points){
  76. it.x = this.mapData[i]
  77. i++
  78. }
  79. imgElement = document.getElementById("mapBackground");
  80. setTimeout(() => {
  81. this.initCharts();
  82. }, 500);
  83. });
  84. },
  85. initCharts() {
  86. const area = [
  87. { name: "太原市", appOrg: "1A003" },
  88. { name: "大同市", appOrg: "1B001" },
  89. { name: "阳泉市", appOrg: "1C001" },
  90. { name: "长治市", appOrg: "1D001" },
  91. { name: "晋城市", appOrg: "1E001" },
  92. { name: "朔州市", appOrg: "1F001" },
  93. { name: "晋中市", appOrg: "1K001" },
  94. { name: "临汾市", appOrg: "1L001" },
  95. { name: "忻州市", appOrg: "1H001" },
  96. { name: "运城市", appOrg: "1M001" },
  97. { name: "吕梁市", appOrg: "1J001" }
  98. ];
  99. const charts = echarts.init(this.$refs["charts"]);
  100. const option = {
  101. // 设置背景颜色,"transparent" 表示透明背景
  102. backgroundColor: "transparent",
  103. // 提示浮窗(Tooltip)配置
  104. tooltip: {
  105. show: true, // 是否显示 tooltip
  106. trigger: "item", // 触发类型,"item" 表示鼠标悬浮在图形上时触发
  107. formatter: `{b}`,
  108. backgroundColor: "rgba(5,13,21,0.25)", // 提示框背景颜色(半透明黑色)
  109. borderColor: "rgba(0, 0, 0, 0.16)", // 边框颜色
  110. triggerOn: "mousemove", // 鼠标移动时触发
  111. enterable: true, // 允许鼠标移动到 tooltip 内部(适用于可点击 tooltip)
  112. textStyle: {
  113. color: "#fff", // 文字颜色
  114. fontSize: 12, // 字体大小
  115. overflow: "break", // 文字超出时换行
  116. },
  117. padding: 3, // 提示框内边距
  118. },
  119. // 地理坐标系组件(用于地图绘制)
  120. geo: {
  121. map: "shanxi", // 使用 "shanxi" 地图
  122. zoom: 1.1, // 初始缩放比例(可调整地图大小)
  123. itemStyle: {
  124. borderColor: "#11F6F2", // 地图区域边界颜色
  125. borderWidth: 5, // 边界宽度
  126. shadowColor: "#0E7683", // 地图阴影颜色
  127. shadowBlur: 0, // 阴影模糊度
  128. shadowOffsetX: 5, // 阴影水平偏移
  129. shadowOffsetY: 13, // 阴影垂直偏移
  130. },
  131. layoutCenter: ["50%", "50%"], // 控制地图的中心位置
  132. },
  133. // 图表数据系列(series)
  134. series: [
  135. {
  136. type: "map", // 地图类型
  137. map: "shanxi", // 绑定的地图名称(与 `geo` 对应)
  138. zoom: 1.1, // 缩放级别
  139. itemStyle: {
  140. areaColor: { image: imgElement, repeat: "no-repeat" }, // 地图区域背景颜色(使用图片填充)
  141. borderColor: "#11F4F0", // 地图边界颜色
  142. borderWidth: 1, // 地图边界宽度
  143. },
  144. emphasis: {
  145. itemStyle: {
  146. areaColor: "#0D7481", // 鼠标悬浮时的区域颜色
  147. },
  148. },
  149. select: { disabled: true }, // 禁用地图的选中状态,防止点击后颜色锁定
  150. label: {
  151. show: true, // 显示区域名称
  152. color: "#FFF", // 文字颜色
  153. fontSize: 16, // 文字大小
  154. fontWeight: 600, // 字重
  155. emphasis: { color: "#fff" }, // 悬浮时的文字颜色
  156. },
  157. data: area
  158. },
  159. {
  160. type: "scatter", // 散点图(用于标注特殊位置)
  161. coordinateSystem: "geo", // 使用 `geo` 作为坐标系
  162. symbol: "image://" + require("@/assets/zhang/myMap/carPosition.png"), // 自定义标记(图片)
  163. symbolSize: [20, 30], // 标记大小(宽 30,高 20)
  164. label: {
  165. show: true, // 显示数字
  166. position: "right", // 数字显示在图标的正上方
  167. color: "#fff", // 文字颜色(白色)
  168. fontSize: 14, // 字体大小
  169. fontWeight: "bold", // 加粗
  170. backgroundColor: "rgba(75,117,117,0.59)", // 文字背景色(可选)
  171. borderColor: "rgba(56,236,250)", // 边框颜色
  172. borderWidth: 1,
  173. padding: [6, 7], // 文字内边距(可选)
  174. borderRadius: 2, // 文字框圆角(可选)
  175. },
  176. data: this.points.map(({ name, coordinate, appOrg, x, y }) => ({
  177. name, // 点名称
  178. value: coordinate, // 经纬度坐标
  179. label: { formatter: `${x}` }, // 显示 `x` 数据(可换成 y)
  180. appOrg,
  181. x, // 额外数据(在建项目数)
  182. y, // 额外数据(施工项目数)
  183. })),
  184. },
  185. ],
  186. };
  187. // 地图注册,第一个参数的名字必须和option.geo.map一致
  188. echarts.registerMap("shanxi",shanxi)
  189. charts.setOption(option);
  190. // 监听点击事件
  191. charts.on("click", (params) => {
  192. if (params.componentType === "geo") {
  193. // 点击的是地图区域
  194. console.log("Clicked map area: ", params.name);
  195. } else if (params.componentType === "series") {
  196. this.appOrg = params.data.appOrg
  197. this.$emit('transmit', this.appOrg)
  198. // 点击的是自定义图标(scatter)
  199. console.log("Clicked scatter point: ", params.name, params.data.appOrg);
  200. // 销毁当前图表实例
  201. charts.dispose();
  202. // 重新初始化并设置新的配置项
  203. this.$nextTick(() => {
  204. this.initChildren(params.name) // 重新初始化图表
  205. this.showBack = true
  206. });
  207. }
  208. });
  209. },
  210. initChildren(cityName){
  211. const charts = echarts.init(this.$refs["charts"]);
  212. const option = {
  213. // 设置背景颜色,"transparent" 表示透明背景
  214. backgroundColor: "transparent",
  215. // 提示浮窗(Tooltip)配置
  216. tooltip: {
  217. show: true, // 是否显示 tooltip
  218. trigger: "item", // 触发类型,"item" 表示鼠标悬浮在图形上时触发
  219. formatter: `{b}`,
  220. backgroundColor: "rgba(5,13,21,0.25)", // 提示框背景颜色(半透明黑色)
  221. borderColor: "rgba(0, 0, 0, 0.16)", // 边框颜色
  222. triggerOn: "mousemove", // 鼠标移动时触发
  223. enterable: true, // 允许鼠标移动到 tooltip 内部(适用于可点击 tooltip)
  224. textStyle: {
  225. color: "#fff", // 文字颜色
  226. fontSize: 12, // 字体大小
  227. overflow: "break", // 文字超出时换行
  228. },
  229. padding: 3, // 提示框内边距
  230. },
  231. // 地理坐标系组件(用于地图绘制)
  232. geo: {
  233. map: "child", // 使用 "shanxi" 地图
  234. zoom: 1.1, // 初始缩放比例(可调整地图大小)
  235. itemStyle: {
  236. borderColor: "#11F6F2", // 地图区域边界颜色
  237. borderWidth: 5, // 边界宽度
  238. shadowColor: "#0E7683", // 地图阴影颜色
  239. shadowBlur: 0, // 阴影模糊度
  240. shadowOffsetX: 5, // 阴影水平偏移
  241. shadowOffsetY: 13, // 阴影垂直偏移
  242. },
  243. layoutCenter: ["50%", "50%"], // 控制地图的中心位置
  244. },
  245. // 图表数据系列(series)
  246. series: [
  247. {
  248. type: "map", // 地图类型
  249. map: "child", // 绑定的地图名称(与 `geo` 对应)
  250. zoom: 1.1, // 缩放级别
  251. itemStyle: {
  252. areaColor: { image: imgElement, repeat: "no-repeat" }, // 地图区域背景颜色(使用图片填充)
  253. borderColor: "#11F4F0", // 地图边界颜色
  254. borderWidth: 1, // 地图边界宽度
  255. },
  256. emphasis: {
  257. itemStyle: {
  258. areaColor: "#0D7481", // 鼠标悬浮时的区域颜色
  259. },
  260. },
  261. select: { disabled: true }, // 禁用地图的选中状态,防止点击后颜色锁定
  262. label: {
  263. show: true, // 显示区域名称
  264. color: "#FFF", // 文字颜色
  265. fontSize: 16, // 文字大小
  266. fontWeight: 600, // 字重
  267. emphasis: { color: "#fff" }, // 悬浮时的文字颜色
  268. },
  269. }
  270. ]
  271. };
  272. echarts.registerMap("child", this.dataSource[cityName])
  273. charts.setOption(option);
  274. },
  275. beBack(){
  276. this.initCharts()
  277. this.showBack = false
  278. this.appOrg = "10001"
  279. this.$emit('transmit', this.appOrg)
  280. }
  281. },
  282. };
  283. </script>
  284. <style scoped>
  285. </style>