vue+echarts5實現(xiàn)中國地圖的示例代碼
使用echarts5.0版本實現(xiàn)中國地圖,E charts在5.0版本之后,地圖不能直接引入了,需要自己去下載。
地圖文件下載地址:
下載地址
(http://datav.aliyun.com/portal/school/atlas/area_selector#&lat=30.772340792178525&lng=103.94573258937584&zoom=9.5)
注意: 將下載后的json文件放置/public目錄下
map類型的地圖
<template>
<div>
<div ref="mapEcharts" class="map-echart"></div>
</div>
</template>
<script>
import * as echarts from 'echarts'
import axios from 'axios'
export default {
name: "Map",
data() {
return {
timer: null,
seriesData: [
{name: '天津市', value: 20057.34},
{name: '北京市', value: 15477.48},
{name: '上海市', value: 31686.1},
{name: '河北省', value: 6992.6},
{name: '山東省', value: 44045.49},
{name: '山西省', value: 4045.49},
],
map: null
}
},
created() {
},
mounted(){
this.initMapEcharts();
},
methods: {
initMapEcharts() {
// 獲取地圖數(shù)據(jù)
// 將下載后的json文件放置/public目錄下
axios.get('/china.json').then(res => {
console.log(res);
// 使用數(shù)據(jù)注冊地圖
echarts.registerMap('china', res.data)
this.$nextTick(() => {
// 初始化地圖
this.map = echarts.init(this.$refs['mapEcharts'])
// 設(shè)置基礎(chǔ)配置項
const option = {
// 標(biāo)題
title: {
text:"中國地圖",
left: 'center',
subtext: "下載鏈接",
sublink: "http://datav.aliyun.com/tools/atlas/#&lat=30.772340792178525&lng=103.94573258937584&zoom=9.5"
},
// 懸浮窗
tooltip: {
trigger: 'item',
formatter: function(params) {
return `${params.name}: ${params.value || 0}`
}
},
// 圖例
visualMap: {
min: 800,
max: 50000,
text: ['High', 'Low'],
realtime: false,
calculable: true,
inRange: {
color: ['lightskyblue', 'yellow', 'orangered']
}
},
// 要顯示的散點數(shù)據(jù)
series: [
{
type: 'map',
map: 'china',
// 這是要顯示的數(shù)據(jù)
data: this.seriesData,
// 自定義命名映射,不設(shè)置的話,label默認(rèn)是使用 geoJson中的name名
nameMap: {
'北京市': "北京重命名",
"天津市": '天津重命名'
},
},
]
}
// 將配置應(yīng)用到地圖上
this.map.setOption(option)
// 設(shè)置定時器,自動循環(huán)觸發(fā)tooltip懸浮窗事件
this.setTimer()
let that = this;
// 當(dāng)鼠標(biāo)在地圖上時,停止自動tooltip事件
this.map.on('mouseover', {series: 0,}, function(params) {
that.clearTimer()
})
// 當(dāng)鼠標(biāo)移出地圖后,再自動tooltip
this.map.on('mouseout', {series: 0}, function(params) {
that.setTimer()
})
})
})
},
setTimer() {
// 當(dāng)前選中區(qū)域的下標(biāo)
let curIndex = -1;
this.timer && clearInterval(this.timer)
this.timer = setInterval(() => {
const len = this.seriesData.length;
// dispatchAction是主動去觸發(fā)echarts事件,取消高亮當(dāng)前的數(shù)據(jù)圖形
this.map.dispatchAction({
type: 'downplay',
seriesIndex: 0,
dataIndex: curIndex
})
// 下一個選中區(qū)域的下標(biāo)
curIndex = (curIndex + 1) % len;
// 高亮下一個數(shù)據(jù)圖形
this.map.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: curIndex
})
// 顯示tooltip
this.map.dispatchAction({
type: 'showTip',
seriesIndex:0,
dataIndex: curIndex
})
}, 1000)
},
clearTimer() {
this.timer && clearInterval(this.timer)
},
},
beforeDestroy() {
this.clearTimer()
}
}
</script>
<style>
.map-echart {
height: 900px;
width: 900px;
}
</style>

geo類型地圖
<template>
<div>
<div ref="geoEcharts" class="geo-echart"></div>
</div>
</template>
<script>
import * as echarts from 'echarts'
import axios from 'axios'
import { color } from 'echarts'
export default {
name: "Geo",
data() {
return {
timer: null,
seriesData: [
{name: '天津市', value: 20057.34},
{name: '北京市', value: 15477.48},
{name: '上海市', value: 31686.1},
{name: '河北省', value: 6992.6},
{name: '山東省', value: 44045.49},
{name: '山西省', value: 4045.49},
],
map: null
}
},
created() {
},
mounted(){
this.initGeoEcharts();
},
methods: {
initGeoEcharts() {
axios.get('/china.json').then(res => {
echarts.registerMap('china', res.data)
this.$nextTick(() => {
const map = echarts.init(this.$refs['geoEcharts'], null, {renderer:'svg'})
const option = {
title: {
text:"中國地圖",
left: 'center',
subtext: "下載鏈接",
sublink: "http://datav.aliyun.com/tools/atlas/#&lat=30.772340792178525&lng=103.94573258937584&zoom=9.5"
},
// 懸浮窗
tooltip: {
trigger: 'item',
formatter: function(params) {
console.log(2222, params);
return `${params.name}: ${params.value || 0}`
}
},
// 圖例
visualMap: {
min: 800,
max: 50000,
text: ['High', 'Low'],
realtime: false,
calculable: true,
inRange: {
color: ['lightskyblue', 'yellow', 'orangered']
}
},
geo: {
map: 'china',
zoom: 1,
roam: 'move',
nameMap: {
'新疆維吾爾自治區(qū)': "新疆",
"西藏自治區(qū)": '西藏',
"甘肅省": "甘肅",
"寧夏回族自治區(qū)": "寧夏",
"廣西壯族自治區(qū)": "廣西",
"內(nèi)蒙古自治區(qū)": "內(nèi)蒙古",
"香港特別行政區(qū)": "香港",
"澳門特別行政區(qū)": "澳門"
},
label: {
show: true,
color: 'black',
position: "inside",
distance: 0,
fontSize: 10,
rotate:45
},
// 所有地圖的區(qū)域顏色
itemStyle: {
areaColor: 'rgba(0,60,153,0.8)',
borderColor: '#02c0ff'
},
emphasis: {
itemStyle: {
areaColor: 'rgba(0,60,153,0.5)',
shadowColor: 'rgba(0,0,0,0.8)',
shadowBlur: 5,
shadowOffsetY: 5
}
},
// 針對某些區(qū)域做一些特別的樣式
// regions: [{
// name: '廣東省',
// itemStyle: {
// areaColor: 'yellow',
// color: 'black',
// borderColor: 'pink'
// }
// }]
},
// 數(shù)據(jù)
series: [
{
type: 'scatter',
coordinateSystem: 'geo',
data: [
{name: '江蘇省', value:[120.15, 31.99, 9]}, // 值為,經(jīng)緯度,數(shù)據(jù)
{name: '湖北省', value: [111, 31.89, 15477]},
{name: '四川省', value: [102.15, 31.89, 31686]},
{name: '浙江省', value: [120.15, 29.89, 6992]},
{name: '山東省', value: [118.15, 36.9, 44045]}
],
SymbolSize: 4
},
{
type: 'lines',
coordinateSystem: 'geo',
data: [
{coords: [[121.15,38], [111, 31.89], [100.15, 31.89],[121.15, 29.89], [105.15, 30.89]]}
],
polyline: true, // 是否是多段線
lineStyle: {
color: {
type: 'radial',
x: 0.5,
y: 0.5,
r: 0.5,
colorStops: [{
offset: 0, color: 'red' // 0% 處的顏色
}, {
offset: 1, color: 'blue' // 100% 處的顏色
}],
global: false, // 缺省為 false
shadowColor: 'rgba(0, 0, 0, 0.5)',
shadowBlur: 10,
curveness: 1
},
opacity: 0.3,
width: 2,
},
// 線特效的配置
effect: {
show: true,
period: 5, // 特效動畫的時間,單位為 s
trailLength: 1, //特效尾跡長度[0,1]值越大,尾跡越長重
// symbol: 'image://' + require('@/echartsMap/money.png'), // 自定義動畫圖標(biāo)
symbolSize: 15, //圖標(biāo)大小
color:"red"
}
}
]
}
map.setOption(option)
})
})
}
},
}
</script>
<style>
.geo-echart{
height: 900px;
width: 900px;
}
</style>

到此這篇關(guān)于vue+echarts5實現(xiàn)中國地圖的示例代碼的文章就介紹到這了,更多相關(guān)vue echarts5中國地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue element-ui實現(xiàn)el-table表格多選以及回顯方式
這篇文章主要介紹了vue element-ui實現(xiàn)el-table表格多選以及回顯方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
ElementPlus組件與圖標(biāo)按需自動引入的實現(xiàn)方法
這篇文章主要介紹了ElementPlus組件與圖標(biāo)按需自動引入的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
vue使用formData時候傳遞參數(shù)是個空值的情況處理
這篇文章主要介紹了vue使用formData時候傳遞參數(shù)是個空值的情況處理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
vue.js實現(xiàn)含搜索的多種復(fù)選框(附源碼)
這篇文章主要給大家介紹了利用vue.js實現(xiàn)含搜索的多種復(fù)選框的相關(guān)資料,文中給出了簡單的介紹,但提供了完整的實例源碼供大家下載學(xué)習(xí),相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
vue3使用element-plus搭建后臺管理系統(tǒng)之菜單管理功能
這篇文章主要介紹了vue3使用element-plus搭建后臺管理系統(tǒng)之菜單管理,使用element-plus el-tree組件快速開發(fā)樹形菜單結(jié)構(gòu),el-tree組件中filter-node-method事件便可以實現(xiàn)樹形菜單篩選過濾功能,需要的朋友可以參考下2022-04-04

