vue3+echarts绘制中国地图在省份上标记数值,实现点击高亮效果

用于记录项目中插件的使用方法!

echarts我用的是@4.9.0版本

npm i echarts@4.9.0

地图数据我用的是echarts库自带的数据

import 'echarts/map/js/china.js';

实现数字显示在区域上方

series: [

            {

                type: "scatter",

                coordinateSystem: "geo",

                data: [

                    {

                        name: "北京",

                        value: [

                            116.405285,

                            39.904985

                        ],

                        num: 123

                    }

                ],

                // 可以添加更多标记点

                label: {

                    normal: {

                        show: true,

                        formatter: (params) => {

                            return params.data.num;

                        },

                        textStyle: {

                            color: "#3096ff",

                            fontSize: 12,

                            backgroundColor: "#fff",

                            shadowColor: "#999",

                            shadowBlur: 8,

                            shadowOffsetY: 5,

                        },

                        borderRadius: 24,

                        padding: 4,

                        width: 16,

                        lineHeight: 16,

                        align: "center",

                    },

                },

                itemStyle: {

                    normal: {

                        color: "#F62157", // 标记点颜色

                    },

                },

            },

        ],

实现点击高亮

    map.off('click'); //这里解决多次触发点击事件

    map.on('click', async (params) => {

        console.log(params)

        if (params.seriesType != "map") return;

        if (selectedArea.value) {

            map.dispatchAction({

                type: 'downplay',

                seriesIndex: 0,

                dataIndex: selectedArea.value

            });

        }

        map.dispatchAction({

            type: 'highlight',

            seriesIndex: 0,

            dataIndex: params.dataIndex

        });

        selectedArea.value = params.dataIndex;

        console.log(params)

    })

实现点击出现弹窗

 tooltip: {

            // 鼠标是否可以进入悬浮框

            enterable: false,

            // 触发方式 mousemove, click, none, mousemove|click

            triggerOn: 'click',

            // item 图形触发, axis 坐标轴触发, none 不触发

            trigger: 'item',

            // 背景色

            backgroundColor: 'rgba(255,255,255,0.5)',

            textStyle: {

                color: "#000"

            },

            confine: false,

            transitionDuration: .2,

            padding: 10,

            formatter: function (params) {

                return '<div class="chartLabel">' +

                    '<div class=title>' + params.name + '</div>' +

                    '</div>'

            }

        },

全部代码

<template>

    <div>

        <div ref="china_map" style="height: 450px;width: 100%"></div>

    </div>

</template>

<script setup lang="ts">

import * as echarts from "echarts";

import { ref, reactive, toRefs, onMounted } from "vue"

import 'echarts/map/js/china.js'; // 核心文件  

// 省份数据


 

const china_map = ref();

const isChina = ref(true); // 默认显示中国地图

const emit = defineEmits(['showChange']);

const selectedArea = ref();

// 中国地图点击省份 显示当前省份的详细的地区。

const chinaMapHidden = (map) => {

    map.off('click'); //这里解决多次触发点击事件

    map.on('click', async (params) => {

        console.log(params)

        if (params.seriesType != "map") return;

        if (selectedArea.value) {

            map.dispatchAction({

                type: 'downplay',

                seriesIndex: 0,

                dataIndex: selectedArea.value

            });

        }

        map.dispatchAction({

            type: 'highlight',

            seriesIndex: 0,

            dataIndex: params.dataIndex

        });

       

        selectedArea.value = params.dataIndex;

        console.log(params)

    })

}

// 渲染地图

const chinaMaprsult = (name = null) => {

    let chinaMap = echarts.init(china_map?.value)

    const options = {

        title: {

            text: '',

            left: 'center',

            textStyle: {

                color: '#fff'

            }

        },

        tooltip: {

            // 鼠标是否可以进入悬浮框

            enterable: false,

            // 触发方式 mousemove, click, none, mousemove|click

            triggerOn: 'click',

            // item 图形触发, axis 坐标轴触发, none 不触发

            trigger: 'item',

            // 背景色

            backgroundColor: 'rgba(255,255,255,0.5)',

            textStyle: {

                color: "#000"

            },

            confine: false,

            transitionDuration: .2,

            padding: 10,

            formatter: function (params) {

                return '<div class="chartLabel">' +

                    '<div class=title>' + params.name + '</div>' +

                    '</div>'

            }

        },

        geo: {

            map: name ? name : "china", // 核心

            zoom: 1.2,

            // roam: 'scale', //是否开启鼠标缩放和平移漫游。默认不开启。如果只想要开启缩放或者平移,可以设置成 'scale' 或者 'move'。设置成 true 为都开启

            label: { // 页面初始化加载的文字

                normal: {

                    show: true,

                    textStyle: {

                        color: "#ccc", // 页面初始化的地图文字颜色

                        fontSize: 10 // // 页面初始化的地图文字大小

                    },

                },

            },

            itemStyle: { //设置样式

                normal: {

                    areaColor: 'rgba(48, 82, 153, 0.5)', // 区域的颜色

                    borderColor: '#59e2f9' //边框颜色

                },

                emphasis: {

                    areaColor: 'rgb(44, 165, 198, 0.9)' // 鼠标悬浮区域颜色

                }

            },

            emphasis: {

                label: {

                    color: '#fff' // 鼠标悬浮文字颜色

                }

            },

        },

        series: [

            {

                type: "scatter",

                coordinateSystem: "geo",

                data: [

                    {

                        name: "北京",

                        value: [

                            116.405285,

                            39.904985

                        ],

                        num: 123

                    }

                ],

                // 可以添加更多标记点

                label: {

                    normal: {

                        show: true,

                        formatter: (params) => {

                            return params.data.num;

                        },

                        textStyle: {

                            color: "#3096ff",

                            fontSize: 12,

                            backgroundColor: "#fff",

                            shadowColor: "#999",

                            shadowBlur: 8,

                            shadowOffsetY: 5,

                        },

                        borderRadius: 24,

                        padding: 4,

                        width: 16,

                        lineHeight: 16,

                        align: "center",

                    },

                },

                itemStyle: {

                    normal: {

                        color: "#F62157", // 标记点颜色

                    },

                },

            },

        ],

    }

    chinaMap.setOption(options)

    chinaMapHidden(chinaMap)

}

onMounted(() => {

    chinaMaprsult('china');

})

</script>

<style scoped></style>

  • 33
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3是一种流行的JavaScript框架,而Echarts5是一种强大的数据可视化库。结合Vue3和Echarts5,可以很容易地实现世界地图的可视化效果。 首先,你需要在Vue项目中安装Echarts5。可以通过npm或yarn来安装echarts: ``` npm install echarts ``` 或者 ``` yarn add echarts ``` 安装完成后,你可以在Vue组件中引入Echarts,并使用它来绘制世界地图。 在Vue组件中,你可以使用`<template>`标签来定义HTML模板,使用`<script>`标签来编写JavaScript代码,使用`<style>`标签来定义CSS样式。 下面是一个简单的示例代码,展示了如何在Vue3中使用Echarts5绘制世界地图: ```vue <template> <div id="world-map" style="width: 100%; height: 400px;"></div> </template> <script> import * as echarts from 'echarts'; export default { mounted() { this.drawWorldMap(); }, methods: { drawWorldMap() { const chartDom = document.getElementById('world-map'); const myChart = echarts.init(chartDom); // 定义地图数据 const mapData = [ { name: 'China', value: 100 }, { name: 'United States', value: 50 }, // 其他国家... ]; // 配置地图选项 const option = { tooltip: { trigger: 'item', formatter: '{b}: {c}', }, visualMap: { min: 0, max: 100, left: 'left', top: 'bottom', text: ['High', 'Low'], seriesIndex: [1], inRange: { color: ['#e0ffff', '#006edd'], }, }, series: [ { type: 'map', mapType: 'world', roam: true, label: { show: true, }, data: mapData, }, ], }; // 使用配置项绘制地图 myChart.setOption(option); }, }, }; </script> <style> #world-map { width: 100%; height: 400px; } </style> ``` 在上面的代码中,我们首先引入了Echarts库,然后在`mounted`生命周期钩子函数中调用`drawWorldMap`方法来绘制地图。`drawWorldMap`方法中,我们使用`echarts.init`方法初始化一个Echarts实例,并通过配置项`option`来定义地图的样式和数据。最后,使用`myChart.setOption(option)`方法将配置项应用到地图上。 这样,你就可以在Vue项目中使用Vue3和Echarts5来实现世界地图的可视化效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值