import geoJson from '@/assets/js/china'
import 'echarts/map/js/china'
import 'echarts/lib/echarts'
export default {
name: 'echartMap',
props: ['swipeRankList', 'defaultProvinceSwipe'],
data () {
return {
loading: true
}
},
mounted () {
let echartsMap = null
if (process.env.NODE_ENV === 'production') {
echartsMap = echarts.init(document.getElementById('echartsMap'))
} else {
echartsMap = this.$echarts.init(document.getElementById('echartsMap'))
}
setTimeout(() => {
this.getEcharts(echartsMap)
}, 1000)
window.addEventListener('resize', () => {
echartsMap.dispose()
setTimeout(() => {
if (process.env.NODE_ENV === 'production') {
echartsMap = echarts.init(document.getElementById('echartsMap'))
} else {
echartsMap = this.$echarts.init(document.getElementById('echartsMap'))
}
this.getEcharts(echartsMap)
}, 300)
echartsMap.resize()
}, false)
},
methods: {
getEcharts (ele) {
let echartDataList = []
echartDataList = this.swipeRankList
let mapFeatures = geoJson.features
let defaultProvinceSwipe = this.defaultProvinceSwipe || ''
var newEchartDataList = []
var timeTicket = null
mapFeatures.forEach((v) => {
let name = v.properties.name
echartDataList.forEach(item => {
if (item.name.search(name) !== -1) {
newEchartDataList.push({
name: name,
bankName: item.name,
value: item.newSwipe || 0,
cp: v.properties.cp,
dataIndex: item.rowNumber,
incumbency: item.incumbency || 0,
queryLevel: 1
})
}
})
})
let option = {
tooltip: {
padding: 0,
enterable: true,
transitionDuration: 1,
formatter: function (params) {
let tipHtml = ''
let tipName = params.data.bankName || params.data.name
let tipIncumbency = params.data.incumbency || 0
let tipValue = params.data.value || 0
if (params.data) {
tipHtml = '<div style="border-radius:5px;background:rgba(0, 0, 0, 0.3);padding: 0.25rem;font-size: 0.575rem;line-height: 1rem; color: #ffffff;display: flex;flex-direction: column;">' +
' <span>' + tipName + '</span>' +
' <span>本月累计新客首刷:' + tipValue + '人</span>' +
' <span>在职人数:' + tipIncumbency + '人</span>' +
'</div>'
}
return tipHtml
}
},
grid: {
borderWidth: 0,
top: 0,
bottom: 0,
left: 0,
right: 0,
containLabel: false
},
series: [{
name: '邮储销售看板',
type: 'map',
mapType: 'china',
zoom: 1.25,
roam: false,
itemStyle: {
normal: {
borderColor: '#0075ff',
areaColor: '#f9f9f9',
label: {
show: false
}
},
emphasis: {
areaColor: '#15ead9',
label: {
show: false
}
}
},
data: newEchartDataList
}]
}
let dataNewList = option.series[0].data
let dataIndex = 0
let dataIndexFlag = 0
let dataIndexOld = 0
let dataIndexArr = []
if (defaultProvinceSwipe === '' || defaultProvinceSwipe.length <= 0) {
dataIndex = parseInt(Math.random() * dataNewList.length)
} else {
dataNewList.forEach((item, index) => {
if (defaultProvinceSwipe.indexOf(item.name) >= 0) {
dataIndexFlag++
dataIndex = index
}
})
}
timeTicket && clearInterval(timeTicket)
timeTicket = setTimeout(() => {
if (dataIndexFlag <= 0) {
dataIndex = parseInt(Math.random() * dataNewList.length)
}
ele.dispatchAction({
type: 'downplay',
seriesIndex: 0
})
ele.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: dataIndex
})
ele.dispatchAction({
type: 'showTip',
seriesIndex: 0,
dataIndex: dataIndex
})
}, 300)
ele.on('click', function (params) {
dataIndexOld = dataIndexArr[dataIndexArr.length - 1]
dataNewList.forEach((item, index) => {
if (item.dataIndex === params.data.dataIndex) {
dataIndex = index
dataIndexArr.push(dataIndex)
}
})
ele.dispatchAction({
type: 'downplay',
seriesIndex: 0,
dataIndex: dataIndexOld
})
ele.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: dataIndex
})
ele.dispatchAction({
type: 'showTip',
seriesIndex: 0,
dataIndex: dataIndex
})
})
this.loading = false
ele.setOption(option)
}
}
}
标记1:(如需要可复制)
若当前项目中后台给的数据与地图默认给的数据不同且有多余的,又不想去操作多余的地区则可以使用该方法,但注意控制台会报错,无需紧张,那是获取不到数据而引起的,这个报错无关紧要,若无报错更好
import geoJson from '@/assets/js/china'
import 'echarts/map/js/china'
import 'echarts/lib/echarts'
let mapFeatures = geoJson.features
mapFeatures.forEach((v) => {
let name = v.properties.name
echartDataList.forEach(item => {
if (item.name.search(name) !== -1) {
newEchartDataList.push({
name: name,
bankName: item.name,
value: item.newSwipe || 0,
cp: v.properties.cp,
dataIndex: item.rowNumber,
incumbency: item.incumbency || 0,
queryLevel: 1
})
}
})
})