map.vue 12 KB

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