vue項(xiàng)目引入 echarts,實(shí)現(xiàn)地圖相關(guān)操作

前言

主要介紹了vue 項(xiàng)目引入echarts 。
功能是地圖示例,底圖底色默認(rèn)、浮動(dòng)、點(diǎn)擊顏色更新,給提示框(tooltitle)添加點(diǎn)擊事件(點(diǎn)擊顯示彈出框),廢話不多說,直接上干貨!

NPM 安裝 ECharts

npm install echarts --save

引入項(xiàng)目

  • main.js文件中引入
// echarts 引入
import echarts from 'echarts'
Vue.prototype.$echarts = echarts

獲取地圖geojson數(shù)據(jù)

DataV.GeoAtlas地理小工具系列

代碼展示

<template>
    <div id="main" :style="{width: '500px',height: '500px'}"></div>
</template>
<script>
import * as echarts from 'echarts';
import YC from "./yc.json" // 引入的鹽城市g(shù)eojson數(shù)據(jù)
export default {
  data() {
    return {
      option: {},
    };
  },
  methods: {
    drawMiddleChart() {       
        // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
        var myChart = echarts.init(document.getElementById('main'));
        echarts.registerMap('YC', YC);
       // 初始化 echarts geojson 數(shù)據(jù)
        const geoCoordMap = []
        YC.features.forEach(item => {
            geoCoordMap.push({
                name: item.properties.name,
                value: item.properties.centroid
            })
        })
        // echarts 添加地圖數(shù)據(jù) registerMap(城市字母開頭大寫, geojson數(shù)據(jù))
        echarts.registerMap('YC', YC)
        this.option = {
            tooltip: {
                show: true,
                trigger: 'item',
                triggerOn: 'click',
                enterable: true,
                extraCssText: 'z-index: 99;max-width: 100px; min-height:100px;white-space:pre-wrap',
                formatter: function(params) {
                    return `<div onclick="dialog('${params.name}')">${params.name}</div>`
                }
            },
            series: [{
                name: '鹽城地區(qū)',
                type: 'map',
                map: 'YC',
                label: {
                    show: true
                },
                data: geoCoordMap, // 格式化后的geojaon數(shù)據(jù)
                geoIndex: 0, // 只展示geo地圖,,不然會(huì)有兩個(gè)地圖重疊
                silent: false,
                emphasis: {
                areaColor: 'transparent'
                }
            }],
            // geo 配置
            geo: { 
            map: 'YC',
            roam: false,
            label: {
                show: true
            },
            itemStyle: {
                normal: {
                areaColor: '#00FFF4', //默認(rèn)區(qū)塊顏色
                borderColor: '#ffffff', //區(qū)塊描邊顏色
                borderWidth: 2 //區(qū)塊描邊顏色寬度
                },
                //鼠標(biāo)經(jīng)過高亮顯示
                emphasis: {
                focus: "self", //突出選中的區(qū)域 其它區(qū)域談化效果
                itemStyle: {
                    opacity: 1,
                    borderColor: "#f18355",
                    borderWidth: "3",
                    areaColor: "yellow"
                }
                }
            },
            // 點(diǎn)擊之后的色塊顏色
            select: {
                itemStyle: {
                    areaColor: "blue",
                    borderColor: "#f18355",
                    borderWidth: "3"
                }
            },
            regions: [ // 每個(gè)區(qū)域的顏色
                {
                  name: '大豐區(qū)', //區(qū)塊名稱
                  itemStyle: {
                    normal: {
                      areaColor: '#B9D696' // 區(qū)域顏色
                    }
                  }
                }
            ]
            }
        }
        // 繪制圖表
        myChart.setOption(this.option);
    }
  },
  mounted() {
    this.drawMiddleChart();
    const _this = this
    window.dialog= function() {
        console.log('執(zhí)行的')
    }
  }
}
</script>

效果圖:

鹽城.png

注意事項(xiàng)

  1. triggerOn 一定要改為 “click”
  2. enterable 要設(shè)置為 true,才能使鼠標(biāo)進(jìn)入提示框,才可以為提示框添加點(diǎn)擊事件。
  3. dialog方法傳參的時(shí)候一定要加上引號(hào),否則會(huì)報(bào)錯(cuò)。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容