vue实现世界疫情地图(点击进入子地图)

代码哈士奇
• 阅读 2005

点击进入子地图目前只实现了中国模块 数据来源,腾讯实时疫情中国疫情网 原本只想做中国模块,后来想了想,做个世界的吧 使用axios和echarts,elementui的加载模块还有按钮,本地代理,脚手架版本4.1.1 本次不封装,刚写完还没有优化,函数名和数据名也是随便起的,大佬勿喷 npm安装即

github地址https://github.com/dmhsq/echartsMap-epidemic 体验地址 http://www.zczyp.top/ 可看完本文还有一个疫情大屏数据等着你哦 vue疫情大屏数据展示+数据导出+地图图片下载

效果展示

vue实现世界疫情地图(点击进入子地图)下载的照片如下 vue实现世界疫情地图(点击进入子地图))vue实现世界疫情地图(点击进入子地图)

寻找数据源

找了几家后发现中国疫情网的国内疫情数据更新的快,腾讯海外疫情也不错

中国疫情网的数据: 这个是全国数据 vue实现世界疫情地图(点击进入子地图)这个是按照日期 vue实现世界疫情地图(点击进入子地图)

设置代理

用axios请求这些数据如果出现跨域问题,就设置本地代理,在根目录下创建vue.config.js文件

module.exports = {
  devServer: {
    proxy: {
      "/api": {
        target: "https://www.ncovchina.com/data",
        changeOrigin: true,
        pathRewrite: {
          "^/api": ""
        }
      },
      "/aki": {
        target: "http://api.fanyi.baidu.com/api/trans/vip",
        changeOrigin: true,
        pathRewrite: {
          "^/aki": ""
        }
      }
    },
    host: "0.0.0.0",
    port: 8083,
    clientLogLevel: "info"
  }
};

api是中国疫情网数据 aki是百度翻译接口,但是本次不用,因为百度翻译标准版返回的不准,用的爱翻译的谷歌翻译

发送请求提取数据

提取数据

axios.post(/api/getDisease.html) 获得数据 vue实现世界疫情地图(点击进入子地图)

国内疫情数据获取并提取

 this.isLoading = true;
      axios.post(`/api/getDisease.html`).then(res => {
        let data = JSON.parse(res.data.data);
        console.log(data);
        let dss = data.areaTree[0].children;
        this.datas = dss;
        this.chinaTotal = data.chinaTotal.confirm;
        this.chinaAdd = data.chinaAdd.confirm;
        this.chinaNow = data.chinaTotal.nowConfirm;
        this.lastTime = data.lastUpdateTime;
        this.isLoading = false;
      });

分别提取了新增,总确诊,现存确诊 在绘制中国疫情地图时需要

axios .post( "https://api.inews.qq.com/newsqa/v1/automation/foreign/country/ranklist" ) 获取海外疫情这里获取了185个国家的数据 海外疫情数据获取并提取

this.isLoading = true;
      axios
        .post(
          "https://api.inews.qq.com/newsqa/v1/automation/foreign/country/ranklist"
        )
        .then(res => {
          let data = res.data.data;
          let names = [];
          data.forEach(item => {
            names.push(item.name);
          });
          this.worldData = data;
          this.test(names).then(res => {
            console.log(res.data.dst);
            let ss = res.data.dst;
            console.log(ss);
            let namess = ('"' + ss.replace(/, /g, '","') + '"').split(",");
            this.worldNames = namess;

            this.setEc(this.check);
            this.isLoading = false;
          });
        });

test函数封装了爱翻译的谷歌翻译,setEc是绘制地图函数

test(content) {
      return axios.post(
        "http://api.aifanyi.net/google.php",
        `content=${content}&from=zh-CN&fromtxt=中文&to=英文&totxt=英语`
      );
    },

为什么要这样提取数据,因为echarts地图数据是英文匹配,直接翻译即可 namess为提取翻译后的国家数组赋值给worldName worldData为海外疫情数据 names.push是提取国家名以便于翻译

踩坑

百度翻译,腾讯翻译,有道翻译会导致翻译不准数据减少,给185个国家名字,返回了174或者多点 Push会改变原格式,所有我们先push字符串再把字符串赋值给地图数据,见下面

处理数据并绘图

国内疫情地图数据处理绘制

      let min = 0;
      let max = 100;
      let title = " 地图";//地图标题(总,现存,新增)
      let data = [];//疫情数据
      let names = [];//提取省级名字
      let values = [];//提取城市疫情数据
      this.check = check;//获取选择(总,现存,新增)
      if (check === 0) {
        title = "国内现存确诊地图";
        this.datas.forEach((item, index) => {
          names.push(item.name);
          values.push(item.total.confirm - item.total.dead - item.total.heal);
          data.push({ name: names[index], value: values[index] });
        });
        min = 0;
        max = 100;
      }
      if (check === 1) {
        title = "国内总确诊地图";
        this.datas.forEach((item, index) => {
          names.push(item.name);
          values.push(item.total.confirm);
          data.push({ name: names[index], value: values[index] });
        });
        min = 0;
        max = 3000;
      }
      if (check === 2) {
        title = "国内新增确诊地图";
        this.datas.forEach((item, index) => {
          names.push(item.name);
          values.push(item.today.confirm);
          data.push({ name: names[index], value: values[index] });
        });
        min = 0;
        max = 5;
      }
      echarts.registerMap("china", china);
      let myChart = echarts.init(this.$refs.chart);
      myChart.clear();
      let option = {
        title: {
          text: title,
          left: "center"
        },
        tooltip: {
          trigger: "item",
          showDelay: 0,
          transitionDuration: 0.2
        },//工具
        visualMap: {
          left: "right",
          min: min,
          max: max,
          inRange: {
            color: [
              "#f9f9fa",
              // "#bfbfc0",
              // "#74add1",
              // "#abd9e9",
              // "#e0f3f8",
              // "#ffffbf",
              "#fee090",
              "#fdae61",
              "#f46d43",
              "#d73027",
              "#a50026"
            ]//颜色梯度
          },
          text: ["High", "Low"], // 文本,默认为数值文本 见地图
          calculable: true
        },
        toolbox: {
          show: true,
          //orient: 'vertical',
          left: "left",
          top: "top",
          feature: {
            dataView: { readOnly: false },
            restore: {},
            saveAsImage: {}
          }//工具功能
        },
        series: [
          {
            name: "数据",
            type: "map",
            roam: true,
            map: "china",//地图
            emphasis: {
              label: {
                show: true
              }
            },
            itemStyle: {
              normal: {
                label: {
                  show: true//显示城市名
                }
              }
            },
            data: data
          }
        ]
      };
      myChart.setOption(option);//绘图
      this.isChina = true;//控制地图选择是否显示
      myChart.on("click", function(params) {
        console.log(params);
      });

解释 check控制选择地图类型(总,新增,现存) min和max 还有color数组 vue实现世界疫情地图(点击进入子地图) 对应最大最小和颜色梯度 myChart.on("click", function(params) { console.log(params); }); 点击地图触发事件

海外疫情地图数据处理绘制

let datas = [];
      let title = "";
      this.isChina = false;
      this.check = check;
      let min = 0;
      let max = 100;
      echarts.registerMap("world", world);
      let values = [];
      if (check === 0) {
        this.worldData.forEach((item, index) => {
          values.push(item.nowConfirm);
          let sss = this.worldNames[index];
          sss = sss.substring(1, sss.length - 1);
          if (sss === "USA") {
            sss = "United States";
          }
          datas.push({
            name: sss,
            value: values[index]
          });
        });
        datas.push({ name: "China", value: this.chinaNow });
        title = "世界疫情现存地图";
        min = 1000;
        max = 100000;
      }
      if (check === 1) {
        this.worldData.forEach((item, index) => {
          values.push(item.confirm);
          let sss = this.worldNames[index];
          sss = sss.substring(1, sss.length - 1);
          if (sss === "USA") {
            sss = "United States";
          }
          datas.push({
            name: sss,
            value: values[index]
          });
        });
        datas.push({ name: "China", value: this.chinaTotal });
        title = "世界疫情总确诊地图";
        min = 100000;
        max = 1000000;
      }
      if (check === 2) {
        this.worldData.forEach((item, index) => {
          values.push(item.confirmAdd);
          let sss = this.worldNames[index];
          sss = sss.substring(1, sss.length - 1);
          if (sss === "USA") {
            sss = "United States";
          }
          datas.push({
            name: sss,
            value: values[index]
          });
        });
        datas.push({ name: "China", value: this.chinaAdd });
        title = "世界疫情新增确诊地图";
        min = 10;
        max = 1000;
      }

      let myChart = echarts.init(this.$refs.chart);
      myChart.clear();
      let option = {
        title: {
          text: title,
          left: "center"
        },
        tooltip: {
          trigger: "item",
          showDelay: 0,
          transitionDuration: 0.2
        },
        visualMap: {
          left: "right",
          min: min,
          max: max,
          inRange: {
            color: [
              "#f9f9fa",
              // "#bfbfc0",
              // "#74add1",
              // "#abd9e9",
              "#e0f3f8",
              "#ffffbf",
              "#fee090",
              "#fdae61",
              "#f46d43",
              "#d73027",
              "#a50026"
            ]
          },
          text: ["High", "Low"], // 文本,默认为数值文本见地图
          calculable: true
        },
        toolbox: {
          show: true,
          //orient: 'vertical',
          left: "left",
          top: "top",
          feature: {
            dataView: { readOnly: false },
            restore: {},
            saveAsImage: {}
          }
        },
        series: [
          {
            name: "数据",
            type: "map",
            roam: true,
            map: "world",
            emphasis: {
              label: {
                show: true
              }
            },
            itemStyle: {
              normal: {
                label: {
                  show: false
                }
              }
            },
            data: datas
          }
        ]
      };
      myChart.setOption(option);
      let vm = this;
      myChart.on("click", function(params) {
        console.log(params);
        if (params.name === "China") {
          console.log(1111);
          vm.setEcs(vm.check);
        }
      });

解释参考国内的 说明 myChart.on("click", function(params) { console.log(params); if (params.name === "China") { console.log(1111); vm.setEcs(vm.check); } 进入中国地图

完整代码

<template>
  <div>
    <div
      style="background-color: #42b983;width: 1010px;height: 810px;"
      v-loading="isLoading"
    >
      <div
        style="float: left;position: relative;top:50px;left: 250px;z-index: 5"
        v-if="!isChina"
      >
        <el-button type="success" @click="getworld">刷新</el-button>
        <el-button type="info" @click="setEc(0)">现存确诊</el-button>
        <el-button type="info" @click="setEc(1)">总确诊</el-button>
        <el-button type="info" @click="setEc(2)">新增确诊</el-button>
        <el-button type="info" @click="setEcs(check)">中国</el-button>
      </div>
      <div
        style="float: left;position: relative;left: 250px;z-index: 5"
        v-if="isChina"
      >
        <h3>上次更新时间{{ lastTime }}</h3>
        <el-button type="success" @click="getSd">刷新</el-button>
        <el-button type="info" @click="setEcs(0)">现存确诊</el-button>
        <el-button type="info" @click="setEcs(1)">总确诊</el-button>
        <el-button type="info" @click="setEcs(2)">新增确诊</el-button>
        <el-button type="info" @click="setEc(check)">全球</el-button>
      </div>
      <div style="width: 1000px;height: 800px;" ref="chart"></div>
    </div>
  </div>
</template>

<script>
import md5 from "js-md5";
import echarts from "echarts";
import china from "echarts/map/json/china.json";
import world from "echarts/map/json/world.json";
// import lodash from 'lodash'
import axios from "axios";
export default {
  name: "echartest",
  data() {
    return {
      chinaTotal: 0,
      chinaAdd: 0,
      chinaNow: 0,
      datas: [],
      worldData: [],
      lastTime: "",
      check: 0,
      isLoading: false,
      isChina: false,
      worldNames: [],
      worldValue: []
    };
  },
  mounted() {
    this.isLoading = true;
    this.getSd();
    this.getworld();
  },
  methods: {
    test(content) {
      return axios.post(
        "http://api.aifanyi.net/google.php",
        `content=${content}&from=zh-CN&fromtxt=中文&to=英文&totxt=英语`
      );
    },
    getworld() {
      this.isLoading = true;
      axios
        .post(
          "https://api.inews.qq.com/newsqa/v1/automation/foreign/country/ranklist"
        )
        .then(res => {
          let data = res.data.data;
          let names = [];
          data.forEach(item => {
            names.push(item.name);
          });
          this.worldData = data;
          this.test(names).then(res => {
            console.log(res.data.dst);
            let ss = res.data.dst;
            console.log(ss);
            let namess = ('"' + ss.replace(/, /g, '","') + '"').split(",");
            this.worldNames = namess;

            this.setEc(this.check);
            this.isLoading = false;
          });
        });
    },
    getSd() {
      this.isLoading = true;
      axios.post(`/api/getDisease.html`).then(res => {
        let data = JSON.parse(res.data.data);
        console.log(data);
        let dss = data.areaTree[0].children;
        this.datas = dss;
        this.chinaTotal = data.chinaTotal.confirm;
        this.chinaAdd = data.chinaAdd.confirm;
        this.chinaNow = data.chinaTotal.nowConfirm;
        this.lastTime = data.lastUpdateTime;
        this.isLoading = false;
      });
    },
    setEcs(check) {
      let min = 0;
      let max = 100;
      let title = " 地图";
      let data = [];
      let names = [];
      let values = [];
      this.check = check;
      if (check === 0) {
        title = "国内现存确诊地图";
        this.datas.forEach((item, index) => {
          names.push(item.name);
          values.push(item.total.confirm - item.total.dead - item.total.heal);
          data.push({ name: names[index], value: values[index] });
        });
        min = 0;
        max = 100;
      }
      if (check === 1) {
        title = "国内总确诊地图";
        this.datas.forEach((item, index) => {
          names.push(item.name);
          values.push(item.total.confirm);
          data.push({ name: names[index], value: values[index] });
        });
        min = 0;
        max = 3000;
      }
      if (check === 2) {
        title = "国内新增确诊地图";
        this.datas.forEach((item, index) => {
          names.push(item.name);
          values.push(item.today.confirm);
          data.push({ name: names[index], value: values[index] });
        });
        min = 0;
        max = 5;
      }
      echarts.registerMap("china", china);
      let myChart = echarts.init(this.$refs.chart);
      myChart.clear();
      let option = {
        title: {
          text: title,
          left: "center"
        },
        tooltip: {
          trigger: "item",
          showDelay: 0,
          transitionDuration: 0.2
        },
        visualMap: {
          left: "right",
          min: min,
          max: max,
          inRange: {
            color: [
              "#f9f9fa",
              // "#bfbfc0",
              // "#74add1",
              // "#abd9e9",
              // "#e0f3f8",
              // "#ffffbf",
              "#fee090",
              "#fdae61",
              "#f46d43",
              "#d73027",
              "#a50026"
            ]
          },
          text: ["High", "Low"], // 文本,默认为数值文本
          calculable: true
        },
        toolbox: {
          show: true,
          //orient: 'vertical',
          left: "left",
          top: "top",
          feature: {
            dataView: { readOnly: false },
            restore: {},
            saveAsImage: {}
          }
        },
        series: [
          {
            name: "数据",
            type: "map",
            roam: true,
            map: "china",
            emphasis: {
              label: {
                show: true
              }
            },
            itemStyle: {
              normal: {
                label: {
                  show: true
                }
              }
            },
            // 文本位置修正

            data: data
          }
        ]
      };
      myChart.setOption(option);
      this.isChina = true;
      myChart.on("click", function(params) {
        console.log(params);
      });
    },
    setEc(check) {
      let datas = [];
      let title = "";
      this.isChina = false;
      this.check = check;
      let min = 0;
      let max = 100;
      echarts.registerMap("world", world);
      let values = [];
      if (check === 0) {
        this.worldData.forEach((item, index) => {
          values.push(item.nowConfirm);
          let sss = this.worldNames[index];
          sss = sss.substring(1, sss.length - 1);
          if (sss === "USA") {
            sss = "United States";
          }
          datas.push({
            name: sss,
            value: values[index]
          });
        });
        datas.push({ name: "China", value: this.chinaNow });
        title = "世界疫情现存地图";
        min = 1000;
        max = 100000;
      }
      if (check === 1) {
        this.worldData.forEach((item, index) => {
          values.push(item.confirm);
          let sss = this.worldNames[index];
          sss = sss.substring(1, sss.length - 1);
          if (sss === "USA") {
            sss = "United States";
          }
          datas.push({
            name: sss,
            value: values[index]
          });
        });
        datas.push({ name: "China", value: this.chinaTotal });
        title = "世界疫情总确诊地图";
        min = 100000;
        max = 1000000;
      }
      if (check === 2) {
        this.worldData.forEach((item, index) => {
          values.push(item.confirmAdd);
          let sss = this.worldNames[index];
          sss = sss.substring(1, sss.length - 1);
          if (sss === "USA") {
            sss = "United States";
          }
          datas.push({
            name: sss,
            value: values[index]
          });
        });
        datas.push({ name: "China", value: this.chinaAdd });
        title = "世界疫情新增确诊地图";
        min = 10;
        max = 1000;
      }

      let myChart = echarts.init(this.$refs.chart);
      myChart.clear();
      let option = {
        title: {
          text: title,
          left: "center"
        },
        tooltip: {
          trigger: "item",
          showDelay: 0,
          transitionDuration: 0.2
        },
        visualMap: {
          left: "right",
          min: min,
          max: max,
          inRange: {
            color: [
              "#f9f9fa",
              // "#bfbfc0",
              // "#74add1",
              // "#abd9e9",
              "#e0f3f8",
              "#ffffbf",
              "#fee090",
              "#fdae61",
              "#f46d43",
              "#d73027",
              "#a50026"
            ]
          },
          text: ["High", "Low"], // 文本,默认为数值文本
          calculable: true
        },
        toolbox: {
          show: true,
          //orient: 'vertical',
          left: "left",
          top: "top",
          feature: {
            dataView: { readOnly: false },
            restore: {},
            saveAsImage: {}
          }
        },
        series: [
          {
            name: "数据",
            type: "map",
            roam: true,
            map: "world",
            emphasis: {
              label: {
                show: true
              }
            },
            itemStyle: {
              normal: {
                label: {
                  show: false
                }
              }
            },
            // 文本位置修正

            data: datas
          }
        ]
      };
      myChart.setOption(option);
      let vm = this;
      myChart.on("click", function(params) {
        console.log(params);
        if (params.name === "China") {
          console.log(1111);
          vm.setEcs(vm.check);
        }
      });
    },

  }
};
</script>

<style scoped></style>

代码优化

可以优化重复的比如check选择那一块,绘制也可以优化,优化的地方还有很多,这里只讲实现





  大家好,我是代码哈士奇,是一名软件学院网络工程的学生,因为我是“狗”,狗走千里吃肉。想把大学期间学的东西和大家分享,和大家一起进步。但由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教!暂时只在csdn这一个平台进行更新,博客主页:https://blog.csdn.net/qq_42027681

未经本人允许,禁止转载

vue实现世界疫情地图(点击进入子地图)


后续会推出

前端:vue入门 vue开发小程序 等 后端: java入门 springboot入门等 服务器:mysql入门 服务器简单指令 云服务器运行项目 一些插件的使用等

大学之道亦在自身,努力学习,热血青春 如果对编程感兴趣可以加入我们的qq群一起交流:974178910

有问题可以下方留言,看到了会回复哦

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Karen110 Karen110
3年前
一篇文章带你了解JavaScript日期
日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这