父组件
<template>
<MapChart id="atmosphereChart" :mapData="mapData" width="100vw" height="40vh" :max="100" @handleClick="handleClick"></MapChart>
</template>
<script>
import MapChart from '@/components/echarts/Charts.vue'
export default {
components: {MapChart},
data() {
return {
mapData: {
data: [
{ name: '丘北县', deptId: '22', value: 100 },
{ name: '文山市', deptId: '19', value: 100 },
{ name: '砚山县', deptId: '21', value: 0 },
{ name: '西畴县', deptId: '43', value: 0 },
{ name: '麻栗坡县', deptId: '24', value: 0 },
{ name: '马关县', deptId: '20', value: 0 },
{ name: '广南县', deptId: '23', value: 0 },
{ name: '富宁县', deptId: '', value: 100 }
]
},
}
}
}
</script>
地图组件
位置 @/components/echarts/Charts.vue
<template>
<div :id="id" :style="{ width, height }"></div>
</template>
<script>
import * as echarts from 'echarts'
import wenshan from './city-data.json'
export default {
name: 'MapChart',
props: ['id', 'width', 'height', 'max', 'mapData', 'colorRange'],
computed: {},
data() {
return {
myMap: {}
}
},
created() { },
mounted() {
this.init()
},
methods: {
init() {
const _this = this
let colorRange = this.colorRange || ['#D2F2EC', '#AEE4D6']
let borderWidth = this.colorRange ? 0 : 2
echarts.registerMap("wenshan", wenshan);
this.myMap = echarts.init(document.getElementById(this.id),);
window.addEventListener("resize", function () {
_this.myMap.resize();
});
const option = {
visualMap: [
{
show: false,
type: 'continuous',
min: 0,
max: this.max,
realtime: false,
calculable: false,
inRange: {
color: colorRange
},
itemWidth: 0,
itemHeight: 0,
},
],
series: [
{
type: "map",
map: "wenshan",
label: {
show: true,
fontSize: '14px',
color: '#49546B'
},
itemStyle: {
normal: {
borderWidth,
borderColor: "#AEE4D6",
},
emphasis: {
areaColor: '#f46d43'
},
selectedMode: "single",
},
data: this.mapData ? this.mapData.data : []
},
],
};
this.myMap.setOption(option);
this.myMap.on('click', function (params) {
_this.$emit('handleClick', params.data)
});
}
}
}
</script>
<style scoped lang='scss'>
</style>