1. 程式人生 > 其它 >echarts地圖動態載入市縣區資料

echarts地圖動態載入市縣區資料

需求:需要獲取全省的資料,展示出來,並且點選可深入到各個省市區,並且滾動滾輪可以退出省級市級

效果:

實現:

1、思路:

1-1、通過呼叫介面獲取地圖json和資料,渲染上去,

1-2、當點選進入更深層級時,記錄一下上一個層級的引數,方便滾輪退回上一層,然後呼叫介面獲取地圖json和資料重新渲染。

1-3、退回上一層級時,用上一個層級的引數去呼叫介面獲取上一層級的地圖json和資料重新渲染

2、程式碼

HTML

            <div class="echarts_item">
                <div class="title">
                    <
div>{{ $t('home.map') }}</div> </div> <div class="echarts_center_box"> <div ref="map"></div> <div> <div class="table"> <el-table stripe size
="mini" height="100%" :data="tableData"> <el-table-column :label="$t('home.area')" show-overflow-tooltip> <template v-slot="{ row }"> {{ row.province }}{{ row.city }}{{ row.area }}
</template> </el-table-column> <el-table-column prop="orderCount" :label="$t('home.orderCount')"></el-table-column> <el-table-column prop="weight" :label="$t('home.weight')"></el-table-column> <el-table-column prop="price" :label="$t('home.price')"></el-table-column> </el-table> </div> </div> </div> </div>

JS

<script>
import * as echarts from 'echarts'
export default {
    name: 'Home',
    data() {
        return {
       tableData:[], mapData: {}, province:
"", parentCode: "", parentName: "", zoom: 1.2, parentList: [], backMap: null, }}, mounted(){ this.china() }, methods:{ china(code = 100000, name = 'province') { //獲取虛擬dom節點 let myChart = echarts.init(this.$refs.map); //請求json this.ajax.get(`/admin/index/getMapJson?code=${code}`).then(chinaStr => { //轉化json let china = JSON.parse(chinaStr) //設定當前地圖的區域資料 this.mapData = china.features.map(e => { return { name: e.properties.name, adcode: e.properties.adcode, level: e.properties.level, parent: e.properties.parent?.adcode } }) //判斷省市區層級傳code let form = "" if (code == 100000) { form = "&unit=" + name } else if (china.features[0].properties.level == 'city') { form = "&unit=city&province=" + name } else if (china.features[0].properties.level == 'district') { if (this.province) { form = `&unit=area&city=${name}&province=${this.province}` } } //獲取資料 this.ajax.get(`/admin/index/getOrderMapList?type=price${form}`).then(res => { //去除監聽 myChart.off('click', this.clickMap) myChart.off('georoam', this.georoamMap) //獲取上一級code,name if (!this.parentList.map(e => e.parentCode).includes(code)) { let obj = { parentName: name, parentCode: code } this.parentList.push(obj) } //表格資料 this.tableData = res this.province = res[0]?.province //把json匯入echarts echarts.registerMap('china', china); let option = { visualMap: { top: "0px", left: 'right', min: 0, max: 1000, inRange: { color: ['#E3F1FC', '#B4E3FA', '#62C5F6', '#2998E8'] }, // text: ['高', '低'],// 文字,預設為數值文字 calculable: true }, series: [ { type: 'map', roam: true, map: 'china', layoutCenter: ['50%', '70%'], layoutSize: '100%', itemStyle: { borderColor: "#fff" }, label: {//地圖預設的文字屬性 show: false, color: '#2998E8', }, select: {//選中的區域文字屬性 label: { color: "#2998E8" }, itemStyle: { color: "#3066ba" } }, emphasis: {//高亮的區域文字屬性 itemStyle: { areaColor: '#3066ba', }, label: { show: true, textStyle: { color: '#2998E8' } } }, zoom: 1.2, // 文字位置修正 textFixed: { Alaska: [20, -20] }, data: res.map(e => {//地圖資料 if (e.area) { return { "name": e.area, "value": e.price } } else if (e.city) { return { "name": e.city, "value": e.price } } else if (e.province) { return { "name": e.province, "value": e.price } } }) } ] } //渲染地圖 myChart.setOption(option); //監聽點選和滾輪放大縮小事件 myChart.on('click', this.clickMap) myChart.on('georoam', this.georoamMap) }) }) }, georoamMap(e) { //只有縮小才觸發退後 if (this.zoom > e.zoom && this.parentList.length > 1) { //利用防抖防止頻繁呼叫返回介面 clearTimeout(this.backMap); this.backMap = setTimeout(() => { //去掉當前層級 let index = this.parentList.length - 1 this.parentList.splice(index, 1) //重新獲取上一層級進行跳轉 index = this.parentList.length - 1 this.china(this.parentList[index].parentCode, this.parentList[index].parentName) }, 500); } this.zoom = e.zoom }, clickMap(e) { //獲取深入的層級引數呼叫介面 let code = this.mapData.find(el => el.name == e.name).adcode this.china(code, e.name) }, } } </script>