<chart-map tmpId="passengerFlowMap" :data="proAreaList"></chart-map>
import chartMap from '@/charts/ChartMap'
//ChartMap.vue文件
<template>
<div>
<div :id="tmpId" class="main"></div>
</div>
</template>
<script>
import * as echarts from 'echarts'
import geoJson from '@/assets/json/china.json'//中国地图的json文件
export default {
data() {
return {
myChart: null,
dataAll: 0
}
},
props: {
data: {
type: Array,
default: () => [],
required: true
},
tmpId: {
type: String,
default: () => 'id_chartpie' + Date.now(),
required: false
}
},
watch: {
data: {
handler(val) {
this.init()
}
}
},
mounted() {
let _this = this
echarts.registerMap('china', geoJson)
this.myChart = echarts.init(document.getElementById(this.tmpId))
window.addEventListener('resize', () => {
setTimeout(function timer() {
_this.myChart.resize()
}, 100)
})
this.init()
},
methods: {
init() {
let _this = this
this.dataAll = 0
let chinaGeoCoordMap = {}
let chinaDatas = []
let curIndex = 0
if (this.data.length === 0) {
return
}
this.data.forEach(e => {
chinaGeoCoordMap[e.name] = [e.longitude, e.latitude]
chinaDatas.push({
name: e.name,
value: e.num
})
_this.dataAll += Number(e.num)
})
chinaGeoCoordMap[_this.$setting.mapCenterName] = _this.$setting.mapCenter
var series = []
var option = {
layoutCenter: ['50%', '72.5%'],
layoutSize: 800,
tooltip: {
trigger: 'item',
backgroundColor: '#000',
borderColor: '#00748D',
textStyle: {
// 提示框浮层的文本样式。
color: '#fff',
fontStyle: 'normal',
fontWeight: 'normal',
fontFamily: 'sans-serif',
fontSize: 14
},
showDelay: 0,
hideDelay: 0,
enterable: true,
transitionDuration: 0,
extraCssText: 'z-index:100',
formatter: function(params, ticket, callback) {
//根据业务自己拓展要显示的内容
var res = ''
var name = params.name
var value = params.value[params.seriesIndex + 1]
if (params.seriesType !== 'effectScatter') {
return
}
if (params.name == _this.$setting.mapCenterName) {
res =
"<div class='tooltip-icon'></div><div class='tooltip-name'>" +
name +
"</div><br/><span class='tooltip-unit'>人次:</span><span class='tooltip-num'>" +
_this.dataAll +
'</span>'
return res
}
res = "<div class='tooltip-icon'></div><div class='tooltip-name'>" + name + "</div><br/><span class='tooltip-unit'>人次:</span><span class='tooltip-num'>" + value + '</span>'
return res
}
},
backgroundColor: '#041A20',
// visualMap: { //图例值控制
// min: 0,
// max: 1,
// calculable: true,
// show: true,
// color: ['#f44336', '#fc9700', '#ffde00', '#ffde00', '#00eaff'],
// textStyle: {
// color: '#fff'
// }
// },
geo: {
map: 'china',
zoom: 0.9,
label: {
emphasis: {
show: false
}
},
roam: true, //是否允许缩放
itemStyle: {
normal: {
color: '#000002', //地图背景色
borderColor: '#647D88', //省市边界线00fcff 516a89
borderWidth: 1
},
emphasis: {
color: 'rgba(37, 43, 61, .5)' //悬浮背景
}
}
},
series: series
}
var convertData = function(data) {
var res = []
for (var i = 0; i < data.length; i++) {
var dataItem = data[i]
var fromCoord = chinaGeoCoordMap[dataItem.name]
var toCoord = _this.$setting.mapCenter
if (fromCoord && toCoord) {
if (i < 3) {
res.push([
{
coord: fromCoord,
value: dataItem.value,
lineStyle: {
width: 1, //尾迹线条宽度
color: '#f81d22',
opacity: 1, //尾迹线条透明度
curveness: 0.3 //尾迹线条曲直度
}
},
{
coord: toCoord
}
])
} else {
res.push([
{
coord: fromCoord,
value: dataItem.value
},
{
coord: toCoord
}
])
}
}
}
return res
}
;[[_this.$setting.mapCenterName, chinaDatas]].forEach(function(item, i) {
series.push(
{
type: 'lines',
zlevel: 2,
effect: {
show: true,
period: 9, //箭头指向速度,值越小速度越快
trailLength: 0.02, //特效尾迹长度[0,1]值越大,尾迹越长重
symbol: 'arrow', //箭头图标
symbolSize: 6 //图标大小
},
lineStyle: {
normal: {
width: 1, //尾迹线条宽度
color: '#27C7E9',
opacity: 1, //尾迹线条透明度
curveness: 0.3 //尾迹线条曲直度
}
},
data: convertData(item[1])
},
{
type: 'effectScatter',
coordinateSystem: 'geo',
zlevel: 2,
rippleEffect: {
//涟漪特效
period: 4, //动画时间,值越小速度越快
brushType: 'stroke', //波纹绘制方式 stroke, fill
scale: 4 //波纹圆环最大限制,值越大波纹越大
},
label: {
normal: {
show: true,
position: 'right', //显示位置
offset: [5, 0], //偏移设置
formatter: function(params) {
//圆环显示文字
return params.data.name
},
fontSize: 13
},
emphasis: {
show: true
}
},
symbol: 'circle',
symbolSize: function(val) {
let l = 0
let num = val[2]
while (num >= 1) {
num = num / 10
l++
}
return 5 + l * 2 //圆环大小
},
itemStyle: {
normal: {
show: false,
color: '#28C8EA'
}
},
data: item[1].map(function(dataItem, index) {
if (index < 3) {
return {
name: dataItem.name,
value: chinaGeoCoordMap[dataItem.name].concat([dataItem.value]),
itemStyle: {
color: '#f81d22'
}
}
} else {
return {
name: dataItem.name,
value: chinaGeoCoordMap[dataItem.name].concat([dataItem.value])
}
}
})
},
//被攻击点
{
type: 'effectScatter',
coordinateSystem: 'geo',
zlevel: 2,
rippleEffect: {
period: 4,
brushType: 'stroke',
scale: 4
},
label: {
normal: {
show: true,
position: 'left',
//offset:[5, 0],
color: '#EDA461',
formatter: '{b}',
textStyle: {
fontSize: '14px',
color: '#EDA461'
}
},
emphasis: {
show: true,
color: '#EDA461'
}
},
symbol: 'circle',
symbolSize: 20,
itemStyle: {
normal: {
show: false,
color: '#EDA461'
}
},
data: [
{
name: item[0],
value: chinaGeoCoordMap[_this.$setting.mapCenterName].concat([10])
}
]
}
)
})
this.myChart.setOption(option)
// // 自动
clearInterval(this.timer)
this.timer = null
// if (curIndex == null) {
// console.log('1111null', '饼图自动')
// this.myChart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: 0 })
// } else {
// // 取消高亮
// this.myChart.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: curIndex })
// if (curIndex == null) {
// this.myChart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: 0 })
// } else {
// // 取消高亮
// let that = this
// this.myChart.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: curIndex })
// this.timer = setInterval(function() {
// setTimeout(function() {
// var dataLen = that.data.length
// // 取消高亮
// that.myChart.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: curIndex })
// curIndex = (curIndex + 1) % dataLen
// //设置高亮
// that.myChart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: curIndex })
// // 显示tooltip
// that.myChart.dispatchAction({
// type: 'showTip',
// seriesIndex: 0,
// dataIndex: curIndex
// })
// }, 0)
// }, 3000)
// }
// }
}
}
}
</script>
<style scoped lang="less">
.main {
width: 100%;
height: 120%;
// height: 100%;
position: absolute;
top: -200px;
left: 0;
z-index: 11;
}
/deep/ .tooltip-name {
font-size: 14px;
color: #fff;
margin: 0 20px 0 0;
}
/deep/ .tooltip-icon {
width: 6px;
height: 6px;
content: '';
margin: 10px 5px;
float: left;
background: #eda461;
}
/deep/ .tooltip-unit {
color: #a5bfcb;
font-size: 12px;
}
/deep/ .tooltip-num {
color: #28c8ea;
font-size: 13px;
}
</style>
vue echarts map 做迁徙图
最新推荐文章于 2023-12-20 14:57:28 发布