vue2中使用echarts實(shí)現(xiàn)中國地圖、在中國地圖上標(biāo)注坐標(biāo)散點(diǎn)圖的操作代碼
1、 npm 安裝 echarts4.9(全局引入不支持5.0)
npm install echarts@4.9.0
2、 main.js中全局引入echarts:
//main.js import echarts from 'echarts' Vue.prototype.$echarts = echarts
3、在頁面中導(dǎo)入地圖的 json 文件(可以使用echarts的文件,也可以使用本地的 json 文件)
import chinamap from "echarts/map/json/china.json"; //兩個導(dǎo)入效果一樣 // import chinamap from '../assets/map/china.json';
4、在頁面中使用
<template> <div id="app"> <div id="echart_china" ref="echart_china"></div> </div> </template> <script> import chinamap from "echarts/map/json/china.json"; export default { data() { return { myChart: null, }; }, mounted() { // 1. 創(chuàng)建一個 ECharts 實(shí)例,返回 echartsInstance,不能在單個容器上初始化多個 ECharts 實(shí)例 this.myChart = this.$echarts.init(this.$refs.echart_china); this.init(); }, methods: { /* 顯示中國地圖 */ init() { // 2. 注冊可用的地圖,只在 geo 組件或者map圖表類型中使用 this.$echarts.registerMap("china", chinamap); //用導(dǎo)入的json文件注冊一個name:china的地圖組件 // 3. 設(shè)置圖表 option var option = { geo: { type: "map", map: "china", //使用 registerMap 注冊的地圖名稱 }, }; console.log("option1:", option); // 只顯示一個地圖的時候,用option,option2都可以。如果要在地圖上加散點(diǎn)圖,用 option var option2 = { series: [ { type: "map", map: "china", //使用 registerMap 注冊的地圖名稱 }, ], }; console.log("option2:", option2); // 4. 顯示地圖 this.myChart.setOption(option); // 用 option 和 option2 效果一樣 }, }, }; </script> <style scoped> #echart_china { width: 100%; height: 500px; background-color: #f1f3f4; } </style>
名字引用關(guān)系如圖:
實(shí)現(xiàn)效果如下:
5、在中國地圖上顯示散點(diǎn)圖(在geo地理坐標(biāo)系中顯示散點(diǎn)圖)
<template> <div id="app"> <div id="echart_china" ref="echart_china"></div> </div> </template> <script> import chinamap from "echarts/map/json/china.json"; export default { data() { return { myChart: null, }; }, mounted() { // 1. 創(chuàng)建一個 ECharts 實(shí)例,返回 echartsInstance,不能在單個容器上初始化多個 ECharts 實(shí)例 this.myChart = this.$echarts.init(this.$refs.echart_china); this.showScatterInGeo(); }, methods: { /* geo:地理坐標(biāo)系組件( https://echarts.apache.org/zh/option.html#geo) 地理坐標(biāo)系組件用于地圖的繪制,支持在地理坐標(biāo)系上繪制散點(diǎn)圖 */ showScatterInGeo() { // 2. 注冊可用的地圖,只在 geo 組件或者map圖表類型中使用 this.$echarts.registerMap("china", chinamap); //用導(dǎo)入的json文件注冊一個name:china的地圖組件 // 3. 設(shè)置圖表 option var option = { geo: { type: "map", map: "china", label: { // label 設(shè)置文本標(biāo)簽的顯示格式,去掉不影響顯示地圖 normal: { color: "#000000", show: true, //顯示省份名稱 }, }, }, series: [ { name: "在地圖中顯示散點(diǎn)圖", type: "scatter", coordinateSystem: "geo", //設(shè)置坐標(biāo)系為 geo data: [ //這里放標(biāo)注點(diǎn)的坐標(biāo)[{name: "北京",value: [116.46, 39.92]}] { name: "北京", value: [116.41995, 40.18994] }, { name: "鄭州", value: [113.665412, 34.757975] }, { name: "天津", value: [117.205126, 39.034933] }, { name: "昆明", value: [102.81844, 24.906231] }, { name: "廣州", value: [113.26453, 23.155008] }, ], }, ], }; // 4. myChart.setOption this.myChart.setOption(option); }, }, }; </script> <style scoped> #echart_china { width: 100%; height: 500px; background-color: #f1f3f4; } </style>
效果如下:
到此這篇關(guān)于vue2中使用echarts實(shí)現(xiàn)中國地圖、在中國地圖上標(biāo)注坐標(biāo)散點(diǎn)圖的操作代碼的文章就介紹到這了,更多相關(guān)vue2 echarts地圖標(biāo)注坐標(biāo)散點(diǎn)圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue源碼學(xué)習(xí)之Object.defineProperty 對數(shù)組監(jiān)聽
這篇文章主要介紹了vue源碼學(xué)習(xí)之Object.defineProperty 對數(shù)組監(jiān)聽,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Vue v-for循環(huán)之@click點(diǎn)擊事件獲取元素示例
今天小編就為大家分享一篇Vue v-for循環(huán)之@click點(diǎn)擊事件獲取元素示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11vue+element-ui:使用el-dialog時彈框不出現(xiàn)的解決
這篇文章主要介紹了vue+element-ui:使用el-dialog時彈框不出現(xiàn)的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10vue3中使用print-js組件實(shí)現(xiàn)打印操作步驟
文章介紹了在Vue?3中使用print-js組件實(shí)現(xiàn)打印操作的步驟,包括安裝依賴、創(chuàng)建打印組件和處理打印預(yù)覽界面,文章還提到打印預(yù)覽界面的樣式調(diào)整對打印效果的影響,并展示了HTML展示和打印預(yù)覽效果,最后,文章鼓勵讀者繼續(xù)瀏覽相關(guān)文章并支持腳本之家2025-02-02vue項(xiàng)目接入高德地圖點(diǎn)擊地圖獲取經(jīng)緯度以及省市區(qū)功能
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目接入高德地圖點(diǎn)擊地圖獲取經(jīng)緯度以及省市區(qū)功能的相關(guān)資料,開發(fā)中我們需要地圖定位,就是用戶輸入位置,自動定位獲取經(jīng)緯度,需要的朋友可以參考下2023-08-08