本文章借鑒改文章局蚀,傳送車。本文章只為了記錄。勿噴趾痘,感謝擎颖。
文章成功展示
在這里插入圖片描述
首先我們需要引入echarts榛斯,現(xiàn)在echarts應(yīng)該已經(jīng)到5了,我們開發(fā)的時候建議看v5的版本
npm install echarts --save 或者
cnpm install echarts --save
現(xiàn)在echarts不給提供地圖了搂捧,所以需要我們自己去尋找地圖json驮俗。穿梭車里邊包含2個json,一個是去掉了南海諸島允跑,一個是保留的王凑,加上上邊的代碼可以完整實現(xiàn)搪柑。(但是如果你想去掉南海諸島,還需要在代碼中加入)
穿梭車
regions: [
{
name: "南海諸島",
value: 0,
itemStyle: {
normal: {
opacity: 0,
label: {
show: false,
},
},
},
},
],
當然如果你不需要去掉可以直接往下看
main.js
import * as echarts from 'echarts'
import china from '../public/china.json';
echarts.registerMap('china', china);
app.config.globalProperties.$echarts = echarts
完整代碼
<template>
<div class="NationView">
<!-- <div>
<a-row class="viewTitle">
<a-col :span="8" class="borderTop"></a-col>
<a-col :span="8">
<a-row type="flex">
<a-col flex="50px" class="rightBias"></a-col>
<a-col flex="auto" class="borderBottom text-center centerBias"
>全網(wǎng)Pass平臺運營試圖</a-col
>
<a-col flex="50px" class="leftBias"></a-col>
</a-row>
</a-col>
<a-col :span="8" class="borderTop"></a-col>
</a-row>
</div> -->
<a-row class="NationViewBox">
<a-col :span="24" class="viewbox"
><div id="NationView" ref="NationView"></div
></a-col>
</a-row>
</div>
</template>
<script>
import { defineComponent, onMounted, ref, getCurrentInstance } from "vue";
export default defineComponent({
setup() {
let { proxy } = getCurrentInstance();
const chart = ref(null);
let geoCoordMap = {
// 地圖地點的經(jīng)度緯度
上海: [121.4648, 31.2891],
東莞: [113.8953, 22.901],
東營: [118.7073, 37.5513],
中山: [113.4229, 22.478],
臨汾: [111.4783, 36.1615],
臨沂: [118.3118, 35.2936],
丹東: [124.541, 40.4242],
麗水: [119.5642, 28.1854],
烏魯木齊: [87.9236, 43.5883],
佛山: [112.8955, 23.1097],
保定: [115.0488, 39.0948],
蘭州: [103.5901, 36.3043],
包頭: [110.3467, 41.4899],
北京: [116.4551, 40.2539],
北海: [109.314, 21.6211],
南京: [118.8062, 31.9208],
南寧: [108.479, 23.1152],
南昌: [116.0046, 28.6633],
南通: [121.1023, 32.1625],
廈門: [118.1689, 24.6478],
臺州: [121.1353, 28.6688],
合肥: [117.29, 32.0581],
呼和浩特: [111.4124, 40.4901],
咸陽: [108.4131, 34.8706],
哈爾濱: [127.9688, 45.368],
唐山: [118.4766, 39.6826],
嘉興: [120.9155, 30.6354],
大同: [113.7854, 39.8035],
大連: [122.2229, 39.4409],
天津: [117.4219, 39.4189],
太原: [112.3352, 37.9413],
威海: [121.9482, 37.1393],
寧波: [121.5967, 29.6466],
寶雞: [107.1826, 34.3433],
宿遷: [118.5535, 33.7775],
常州: [119.4543, 31.5582],
廣州: [113.5107, 23.2196],
廊坊: [116.521, 39.0509],
延安: [109.1052, 36.4252],
張家口: [115.1477, 40.8527],
徐州: [117.5208, 34.3268],
德州: [116.6858, 37.2107],
惠州: [114.6204, 23.1647],
成都: [103.9526, 30.7617],
揚州: [119.4653, 32.8162],
承德: [117.5757, 41.4075],
拉薩: [91.1865, 30.1465],
無錫: [120.3442, 31.5527],
日照: [119.2786, 35.5023],
昆明: [102.9199, 25.4663],
杭州: [119.5313, 29.8773],
棗莊: [117.323, 34.8926],
柳州: [109.3799, 24.9774],
株洲: [113.5327, 27.0319],
武漢: [114.3896, 30.6628],
汕頭: [117.1692, 23.3405],
江門: [112.6318, 22.1484],
沈陽: [123.1238, 42.1216],
滄州: [116.8286, 38.2104],
河源: [114.917, 23.9722],
泉州: [118.3228, 25.1147],
泰安: [117.0264, 36.0516],
泰州: [120.0586, 32.5525],
濟南: [117.1582, 36.8701],
濟寧: [116.8286, 35.3375],
核髋耄口: [110.3893, 19.8516],
淄博: [118.0371, 36.6064],
淮安: [118.927, 33.4039],
深圳: [114.5435, 22.5439],
清遠: [112.9175, 24.3292],
溫州: [120.498, 27.8119],
渭南: [109.7864, 35.0299],
湖州: [119.8608, 30.7782],
湘潭: [112.5439, 27.7075],
濱州: [117.8174, 37.4963],
濰坊: [119.0918, 36.524],
煙臺: [120.7397, 37.5128],
玉溪: [101.9312, 23.8898],
珠海: [113.7305, 22.1155],
鹽城: [120.2234, 33.5577],
盤錦: [121.9482, 41.0449],
石家莊: [114.4995, 38.1006],
福州: [119.4543, 25.9222],
秦皇島: [119.2126, 40.0232],
紹興: [120.564, 29.7565],
聊城: [115.9167, 36.4032],
肇慶: [112.1265, 23.5822],
舟山: [122.2559, 30.2234],
蘇州: [120.6519, 31.3989],
萊蕪: [117.6526, 36.2714],
菏澤: [115.6201, 35.2057],
營口: [122.4316, 40.4297],
葫蘆島: [120.1575, 40.578],
衡水: [115.8838, 37.7161],
衢州: [118.6853, 28.8666],
西寧: [101.4038, 36.8207],
西安: [109.1162, 34.2004],
貴陽: [106.6992, 26.7682],
連云港: [119.1248, 34.552],
邢臺: [114.8071, 37.2821],
邯鄲: [114.4775, 36.535],
鄭州: [113.4668, 34.6234],
鄂爾多斯: [108.9734, 39.2487],
重慶: [107.7539, 30.1904],
金華: [120.0037, 29.1028],
銅川: [109.0393, 35.1947],
銀川: [106.3586, 38.1775],
鎮(zhèn)江: [119.4763, 31.9702],
長春: [125.8154, 44.2584],
長沙: [113.0823, 28.2568],
長治: [112.8625, 36.4746],
陽泉: [113.4778, 38.0951],
青島: [120.4651, 36.3373],
韶關(guān): [113.7964, 24.7028],
吐魯番: [89.18596, 42.94244],
那曲: [92.05136, 31.47614],
呂梁: [111.15081, 37.51761],
伊春: [128.669, 47.72582],
呼倫貝爾: [119.73572, 49.21336],
嘉峪關(guān): [98.33435281250001, 39.668377553435185],
漢中: [107.03194, 33.06784],
};
// 發(fā)射點數(shù)組
let GZData = [
[
[{ name: "吐魯番" }, { name: "吐魯番" }],
[{ name: "吐魯番" }, { name: "福州" }],
[{ name: "吐魯番" }, { name: "呂梁" }],
[{ name: "吐魯番" }, { name: "長春" }],
[{ name: "吐魯番" }, { name: "漢中" }],
[{ name: "吐魯番" }, { name: "那曲" }],
[{ name: "吐魯番" }, { name: "廣州" }],
[{ name: "吐魯番" }, { name: "常州" }],
[{ name: "吐魯番" }, { name: "北京" }],
[{ name: "吐魯番" }, { name: "南寧" }],
[{ name: "吐魯番" }, { name: "昆明" }],
[{ name: "吐魯番" }, { name: "呼和浩特" }],
[{ name: "吐魯番" }, { name: "西寧" }],
[{ name: "吐魯番" }, { name: "呼倫貝爾" }],
[{ name: "吐魯番" }, { name: "嘉峪關(guān)" }],
[{ name: "吐魯番" }, { name: "成都" }],
[{ name: "吐魯番" }, { name: "伊春" }],
// [{name: '吐魯番'}, {name: '吐魯番'}],
[{ name: "吐魯番" }, { name: "汗つ耄口" }],
],
];
// 發(fā)射點數(shù)組下標
let launchIndex = 0;
// 按照數(shù)組順序循環(huán)發(fā)射(如果是使用定時器銷毀重建圖形發(fā)射點那么這個就派上用場了)
let len = GZData.length;
let arrIndex = launchIndex;
if (launchIndex < len - 1) {
launchIndex = launchIndex + 1;
} else {
launchIndex = 0;
}
// 防止切換用戶列表時,定時器延遲造成下標超出數(shù)組問題
if (arrIndex > len - 1) {
launchIndex = 0;
arrIndex = 0;
}
// 數(shù)組遍歷
let lineDataArr = [];
lineDataArr.push(GZData[arrIndex]);
// // 自定義多個發(fā)射點(push多少個就會有多少個同時發(fā)射點)
// lineDataArr.push(GZData[0])
// lineDataArr.push(GZData[1])
// 處理線路所需要的數(shù)據(jù)格式data
let convertData = function(data) {
// console.log(data)
let res = [];
for (let i = 0; i < data.length; i++) {
let dataItem = data[i];
let fromCoord = geoCoordMap[dataItem[0].name];
let toCoord = geoCoordMap[dataItem[1].name];
if (fromCoord && toCoord) {
res.push({
fromName: dataItem[0].name,
toName: dataItem[1].name,
coords: [fromCoord, toCoord],
});
}
}
return res;
};
// 發(fā)射點和線路顏色
let color = ["#ffa022", "#a6c84c", "#46bee9"]; // 橙 墨綠 藍
let seriesData = [];
lineDataArr.forEach(function(item, i) {
seriesData.push(
{
// 亮光發(fā)射效果
// name: item[0] + ' Top10',
name: "廣州",
type: "lines",
zlevel: 1,
effect: {
show: true,
period: 6, // 特效動畫的時間百姓,單位為 s
// delay: i * 5000, // 特效動畫的延時渊额,支持設(shè)置成數(shù)字或者回調(diào)。單位ms
trailLength: 0.7,
color: "#fff",
symbolSize: 3,
},
label: {
show: true,
},
lineStyle: {
normal: {
color: color[i],
width: 0,
curveness: 0.2,
},
},
// data: convertData(item[1])
data: convertData(item),
},
{
// 線路效果
// name: item[0] + ' Top10',
name: "廣州",
type: "lines",
zlevel: 2,
effect: {
// show: true,
// period: 6,
// trailLength: 0,
// symbol: planePath,
// symbolSize: 55
},
label: {
show: true,
},
lineStyle: {
normal: {
color: color[i],
width: 1.5,
opacity: 0.4,
curveness: 0.2,
},
},
// data: convertData(item[1])
data: convertData(item),
},
{
// 文字和地點漣漪效果
// name: item[0] + ' Top10',
name: "廣州",
type: "effectScatter",
coordinateSystem: "geo",
// geoIndex: 0,
zlevel: 2,
rippleEffect: {
brushType: "stroke",
},
label: {
emphasis: {
// 有這一層為鼠標移入漣漪點才顯示文字垒拢,去掉這一層那么直接顯示文字再地圖上
show: true,
// position: 'top',
formatter: "旬迹",
// offset: [10, -4]
},
},
// label: { // 漣漪文字位置
// normal: {
// show: true,
// position: 'right',
// formatter: ''
// }
// },
symbolSize: 8,
// symbolOffset:[4, 4], // 標記相對于原本位置的偏移
itemStyle: {
// 漣漪相關(guān)顏色
normal: {
color: color[i],
},
// color: color[i]
},
data: item.map(function(dataItem) {
return {
name: dataItem[1].name,
value: geoCoordMap[dataItem[1].name].concat([dataItem[1].value]),
};
}),
}
);
});
// 中國地圖線路特效配置 end
const option = ref({
backgroundColor: "#0c1c30", // 圖形容器背景色
tooltip: {
// 鼠標移到圖里面的浮動提示框
// formatter詳細配置: https://echarts.baidu.com/option.html#tooltip.formatter
formatter(params) {
// console.log(params)
let value = params.value;
if (Array.isArray(value)) {
// 鼠標移到漣漪點上不顯示浮層
return "";
}
if (value === undefined) {
// 鼠標移到路線上不顯示浮層
return "";
}
if (isNaN(value)) {
value = 0;
}
let htmlStr = `
<div style='font-size:16px;'> ${params.name}</div>
<p style='text-align:left;margin-top:4px;'>
區(qū)域分布:${value}%<br/>
</p>
`;
return htmlStr;
},
// backgroundColor:"#ff7f50",//提示標簽背景顏色
// textStyle:{color:"#fff"} //提示標簽字體顏色
},
// geo配置詳解: https://echarts.baidu.com/option.html#geo
geo: {
// 地理坐標系組件用于地圖的繪制
map: "china", // 表示中國地圖
// roam: true, // 是否開啟鼠標縮放和平移漫游
zoom: 1.2, // 當前視角的縮放比例(地圖的放大比例)
// top:20,
label: {
show: true,
color: "#f2f2f2", // 文字顏色
},
itemStyle: {
// 地圖區(qū)域的多邊形 圖形樣式求类。
areaColor: "#0c1c30", // 地區(qū)默認顏色
// borderColor: '#678dd6', //片區(qū)邊框顏色
borderWidth: 1, // 邊框描繪
borderColor: "#90c5ed", //片區(qū)邊框顏色
emphasis: {
// 高亮狀態(tài)下的多邊形和標簽樣式
areaColor: "#24cbff", // 高亮區(qū)域背景顏色
shadowBlur: 20,
shadowColor: "rgba(0, 0, 0, 0.5)",
},
},
},
series: [
...seriesData, // 中國地圖線路特效配置
{
// name: '', // 浮動框的標題(上面的formatter自定義了提示框數(shù)據(jù)奔垦,所以這里可不寫)
type: "map",
geoIndex: 0,
label: {
show: true,
},
// 這是需要配置地圖上的某個地區(qū)的數(shù)據(jù),根據(jù)后臺的返回的數(shù)據(jù)進行拼接(下面是我定義的假數(shù)據(jù))
data: [
{
name: "北京",
value: 2,
},
{
name: "上海",
value: 80,
},
{
name: "黑龍江",
value: 25,
},
{
name: "新疆",
value: 10,
},
{
name: "深圳",
value: 36,
},
{
name: "湖北",
value: 2,
},
{
name: "四川",
value: 60,
},
],
},
],
});
//頁面跳轉(zhuǎn)
onMounted(() => {
console.log(proxy, chart.value, option.value);
chart.value = proxy.$echarts.init(
document.getElementById("NationView"),
"macarons"
);
chart.value.setOption(option.value);
//鼠標移入地圖不變黃色
chart.value.on("mouseover", function() {
chart.value.dispatchAction({
type: "downplay",
});
});
});
return {};
},
});
</script>
<style lang="scss" scoped>
.NationView {
padding: 0px 10px 10px;
.viewTitle {
height: 60px;
padding-top: 10px;
.borderTop {
border-top: 1px solid #b2b2b2;
}
.leftBias {
width: 50px;
height: 50px;
position: relative;
background-color: transparent;
}
.leftBias:before {
position: absolute;
top: 0px;
right: 0;
left: 0;
bottom: 0;
border-bottom: 50px solid #b2b2b2;
border-left: 50px solid transparent;
content: "";
}
.leftBias:after {
position: absolute;
left: 1px;
right: 0;
top: 1px;
bottom: 0;
border-bottom: 49px solid white;
border-left: 49px solid transparent;
content: "";
}
.rightBias {
width: 50px;
height: 50px;
position: relative;
background-color: transparent;
}
.rightBias:before {
position: absolute;
top: 0px;
right: 0;
left: 0;
bottom: 0;
border-bottom: 49px solid #b2b2b2;
border-right: 49px solid transparent;
content: "";
}
.rightBias:after {
position: absolute;
left: 0;
right: 1px;
top: 1px;
bottom: 0;
border-bottom: 49px solid white;
border-right: 49px solid transparent;
content: "";
}
.borderBottom {
border-bottom: 1px solid #b2b2b2;
}
.centerBias {
line-height: 49px;
font-size: 1.5rem;
}
}
.NationViewBox {
height: 600px;
.viewbox {
height: 100%;
#NationView {
width: 100%;
height: 100%;
}
}
}
}
</style>