首页 > 其他分享 >vue2 使用echarts实现地图点击进入下一层级+点击空白处回退

vue2 使用echarts实现地图点击进入下一层级+点击空白处回退

时间:2024-05-16 08:58:06浏览次数:22  
标签:code name 地图 json 点击 vue2 mapJson echarts

先验知识:

vue2中echarts的安装和显示中国地图:https://www.cnblogs.com/sunshine233/p/16140522.html

鼠标事件: https://echarts.apache.org/zh/api.html#echartsInstance.on echarts.getMap():https://echarts.apache.org/zh/api.html#echarts.getMap 监听“空白处”的事件:https://echarts.apache.org/handbook/zh/concepts/event/#%E7%9B%91%E5%90%AC%E2%80%9C%E7%A9%BA%E7%99%BD%E5%A4%84%E2%80%9D%E7%9A%84%E4%BA%8B%E4%BB%B6

npm 安装 echarts4.9(全局引入不支持5.0)

运行效果(只做了河南的点击和后退):

  实现思路:

1. 引入地图并显示

// 1. 基于准备好的dom,初始化echarts实例
this.myChart = this.$echarts.init(this.$refs.myChart);
// 2. 引入要显示的地图 json 文件(json文件可以是echart自带的,也可以是自己下载的)
this.mapJson = require("echarts/map/json/china.json");
// 3. 注册可用的地图 registerMap("自定义的名字,要和option中map:''对应",注册的地图指向的json文件)
this.$echarts.registerMap("mapJson", this.mapJson);
// 4. 指定图表的配置项和数据
this.option = {
    geo: {
        type: "map",
        map: "mapJson",//这里的数据会变,"这里是引入的存储json文件的变量名"
        label: {
            normal: {
                color: "#000000",
                show: true, //显示省份名称
            },
        },
    },
    series: [
        {
            name: "在地图中显示散点图",
            type: "scatter",
            coordinateSystem: "geo", //设置坐标系为 geo
            data: [
                //这里放标注点的坐标[{name: "北京",value: [116.46, 39.92]}]
            ],
        },
    ],
};
// 5. 设置图表实例的配置项以及数据,万能接口,所有参数和数据的修改都可以通过 setOption 完成,ECharts 会合并新的参数和数据,然后刷新图表。
this.myChart.setOption(this.option);

2. 地图显示的是“中国”还是“省”还是“市区”主要就是靠引入的json文件区分,
也就是  this.mapJson = require("echarts/map/json/china.json");  的mapJson,
然后   this.$echarts.registerMap("mapJson", this.mapJson); 注册一下修改后的文件,
 this.myChart.setOption(this.option);  地图即可重新显示新的地区。

3. 怎么确定  this.mapJson  引入的哪个文件呢? 添加地图的监听事件后,可以获取一个code值,用这个code值判断应该引入哪个省市的文件。点击空白处回退同理,只需要确定应该回退层级的code值,就可以由此判断应该回退到哪个层级页面了。

全部代码如下:

<template>
    <div id="app">
        <div id="myChart" ref="myChart"></div>
        <el-button type="success" @click="cancelEchartsEvent">点击取消on事件</el-button>
    </div>
</template>

<script>
export default {
    data() {
        return {
            myChart: null,
            mapJson: null,
            option: {},
            curArea: {
                code: -1,
                name: ""
            },
        };
    },
    mounted() {
        this.initEchartParams();
        this.addMouseClick();//开启 echarts 事件监听
    },
    beforeDestroy() {
        this.cancelEchartsEvent();
    },
    methods: {
        cancelEchartsEvent() {
            console.log("页面卸载前,取消 echarts 的事件监听");
            this.myChart.off("click");
        },
        initEchartParams() {
            // 1. 基于准备好的dom,初始化echarts实例
            this.myChart = this.$echarts.init(this.$refs.myChart);
            // 2. 引入要显示的地图 json 文件(json文件可以是echart自带的,也可以是自己下载的)
            this.mapJson = require("echarts/map/json/china.json");
            // 3. 注册可用的地图 registerMap("自定义的名字,要和option中map:''对应",注册的地图指向的json文件)
            this.$echarts.registerMap("mapJson", this.mapJson);
            // 4. 指定图表的配置项和数据
            this.option = {
                geo: {
                    type: "map",
                    map: "mapJson",//这里的数据会变,"这里是引入的存储json文件的变量名"
                    label: {
                        normal: {
                            color: "#000000",
                            show: true, //显示省份名称
                        },
                    },
                },
                series: [
                    {
                        name: "在地图中显示散点图",
                        type: "scatter",
                        coordinateSystem: "geo", //设置坐标系为 geo
                        data: [
                            //这里放标注点的坐标[{name: "北京",value: [116.46, 39.92]}]
                            { name: "郑州", value: [113.665412, 34.757975] },
                            { name: "北京", value: [116.41995, 40.18994] },
                            { name: "天津", value: [117.205126, 39.034933] },
                            { name: "昆明", value: [102.81844, 24.906231] },
                            { name: "广州", value: [113.26453, 23.155008] },
                        ],
                    },
                ],
            };
            // 5. 设置图表实例的配置项以及数据,万能接口,所有参数和数据的修改都可以通过 setOption 完成,ECharts 会合并新的参数和数据,然后刷新图表。
            this.myChart.setOption(this.option);
        },
        addMouseClick() {
            this.addClickInMap();
            this.addClickInBlank();
        },
        // 点击了地图非空白处
        addClickInMap() {
            // echarts.getMap 获取已注册的地图
            const maps = this.$echarts.getMap("mapJson").geoJson.features;
            console.log({ maps });

            this.myChart.on("click", (res) => {
                let clickArea = maps.find(map => map.properties.name == res.name);
                // console.log({ clickArea });

                if (!clickArea) {
                    // 如果点的不是地图,而是地图上的散点,什么也不做
                    return;
                }
                this.curArea.code = clickArea.id;
                this.curArea.name = res.name;
                console.log("当前点击的是: " + this.curArea.name + " this.curArea.code:" + this.curArea.code);

                // 修改 mapJson 的数值
                this.configGeoMap(this.curArea.code);
            });
        },
        // 点击了地图空白处
        addClickInBlank() {
            this.myChart.getZr().on('click', (event) => {
                // !event.target 不存在 证明点击的地方是地图空白处
                if (!event.target) {
                    let preCode = -1;
                    const curAreaCode = this.curArea.code;
                    console.log("回退前地区code ", curAreaCode);

                    if (curAreaCode == "100000") {//当前在中国,不回退
                        console.log("当前地图已经是 china, 不可回退!");
                        return;
                    } else if (curAreaCode % 10000 == 0) {//说明当前在省,回退到 china
                        preCode = 100000;
                    } else if (curAreaCode % 100 == 0) {////说明当前在 郑州市,回退到 河南省
                        preCode = curAreaCode - curAreaCode % 10000;
                    } else {
                        preCode = curAreaCode - curAreaCode % 100; //回退到city
                    }
                    // 回退后,当前显示地图的code就是preCode
                    this.curArea.code = preCode;
                    console.log('点击了空白处,回退后地区 code:' + preCode);

                    this.configGeoMap(preCode);
                }
            });
        },
        // 修改 mapJson 的数值
        configGeoMap(code) {
            // 这里要根据code值找到对应的json文件,如果是从服务器获取,也是找到对应的json文件即可
            // 这里我用的是 用npm安装[email protected] 时自动下载的文件 
            if (code == "100000") {
                this.mapJson = require("echarts/map/json/china.json");
            } else if (code == "410000") {
                this.mapJson = require("echarts/map/json/province/henan.json");
            } else {
                console.log('除了河南外,其它地区暂时不能跳转');
            }

            this.$echarts.registerMap("mapJson", this.mapJson);
            this.myChart.setOption(this.option);
        }
    }
}
</script>
<style scoped>
#myChart {
    width: 100%;
    height: 500px;
    background-color: #f1f3f4;
}

button {
    margin: 20px;
}
</style>

 

标签:code,name,地图,json,点击,vue2,mapJson,echarts
From: https://www.cnblogs.com/sunshine233/p/18195239

相关文章

  • echarts图由于容器隐藏导致图表不显示问题解决办法
    开发过程中常常会遇到echarts图由于容器隐藏导致图表不显示问题,最简单的办法就是给容器元素加上宽度和高度容器加上固定的宽度和高度<divid="res"style="height:450px;width:1200px"></div>然而在实际开发中某些场景下,要求图表宽度100%显示,而计算容器的宽度有时又会十分的麻......
  • vue2小效果的实现
    代码量:100行左右搏客量:1所用时间:1h<!--components文件中的Person.vue--><template><divclass="person"><h2>姓名:{{name}}</h2><h2>年龄:{{age}}</h2><button@click="changeName">......
  • Vue2入门之超详细教程十七-常用内置命令集合
    Vue2入门之超详细教程十四-常用内置指令集合1、简介常用内置指令集合v-bind:单向绑定解析表达式,可简写为:xxxv-model:双向数据绑定v-for:遍历数组/对象/字符串v-on:绑定时间监听,可简写为@v-if:条件渲染(动态控制节点是否存在)v-else:条件渲染(动态控制节点是否存在)v-show:条件渲染......
  • 实现Python日志点击跳转到代码位置的方法
    前言在软件开发过程中,日志是一种非常重要的工具,用于记录程序运行时的状态、错误信息以及调试信息。当程序出现问题时,定位到具体的代码位置对于排查问题至关重要。本文将介绍如何在Python日志中实现点击跳转到代码位置的功能,以提高调试效率。为什么需要点击跳转功能?通常情况下,......
  • vue2使用elementUI组件el-tooltip指定元素进行提示信息(图标显示信息)
     <el-table-columnprop="operation"label="操作"borderwidth="200px"><templateslot-scope="scope"><divclass="operation-icons"><!......
  • 生命周期---Vue2&Vue3
    生命周期---Vue2&Vue3简单理解为:组件从创建到被销毁的一个过程,就相当于人的一生,从出生到死亡的一个过程。组件的生命周期也称生命周期、生命周期函数、生命周期钩子生命周期在特定的时刻会调用特定的函数生命周期分为四个阶段,每个阶段都有两个钩子,现只讨论这八个钩子V......
  • django 点击按钮,显示文本,js实现
    方法一:点击显示弹出框<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>数据弹窗</title><scripttype="text/javascript">functionshowAlert(data){......
  • el-cascader设置为任意一级选项,去除单选按钮以及点击关闭下拉选择
    1、标签组件:<el-cascaderref="cascaderRef1"popper-class="popper-cascader"@change="handleChangeCascader(cascaderRef1)"></el-cascader>2、给popper-cascader设置样式,在element-ui,scss里编写.popper-cascader.el-cascader-panel......
  • Echarts设置饼状图保证你看的明明白白
    简单的饼状图<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>ECharts-动画</title>......
  • windows10 资源管理器 卡死 底部任务栏不显示程序 点击底部任务栏两次会重启资源管理
     故障存储段,类型0事件名称:AppHangB1响应:不可用CabID:0问题签名:P1:explorer.exeP2:10.0.19041.1266P3:418a6e83P4:a874P5:134217728P6: P7: P8: P9: P10: 附加文件:\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WERE85A.tmp.WERInternalMetadata.xml\\?......