Vue中echarts渲染中国地图

下载echarts

npm i echarts@4.9.0

关于echarts跟随屏幕大小缩放问题

在多个组件用echarts实现resize,用addEvenListener监听屏幕大小,每写一个都是单独
  新建一个监听回调,相较于window.onresize只能生成一个监听回调,无论新建多少个onresize只生成一个resize回调,所以就出现只能生效一个onresize监听其他不生效的问题

推荐以下写法:
  window.addEventListener('resize',()=>{

          this.chartResize=myChart.resize()
   }) 

<template>
   <div id='mapId' ref='myEchart' :style="{width:'100%',height:'600px'}">

    </div>
</template>

<script>   
/**

@使用echarts渲染地图
*/


import echarts from 'echarts' 

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

export default {
   name: "echarts", 
    data() {
      return {
        chart: null,
        chartResize:''
      };
    },
    mounted() {
      this.chinaConfigure();
      //销毁resize监听
      window.removeEventListener('resize',this.chartResize)
    },
    beforeDestroy() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    methods: {
      chinaConfigure() { 
        let myChart = echarts.init(this.$refs.myEchart); //这里是为了获得容器所在位置
        myChart.setOption({ // 进行相关配置
          // backgroundColor: "#02AFDB",
          tooltip: {}, // 鼠标移到图里面的浮动提示框
          dataRange: {
            show: true,
            left:'center',
            align:'bottom',
            orient:'horizontal',
            // min: 0,
            // max: 1000,
            range: null,
            text: ['2k','0'],
            textStyle: {
              color: '#eee'
            },
            realtime: false,
            calculable: false,//隐藏地图下方的进度条
            color: ['#004599', '#0A73DA', '#1890FF','#45A7FF','#69C0FF','#91D5FF','#BAE7FF']
          },
          geo: { // 这个是重点配置区
            map: 'china', // 表示中国地图
            roam: true,
            label: {
              normal: {
                show: true, // 是否显示对应地名
                textStyle: {
                  color: 'rgba(0,0,0,0.4)'
                }
              }
            },
            itemStyle: {
              normal: {
                borderColor: 'rgba(0, 0, 0, 0.2)'
              },
              emphasis: {
                areaColor: null,
                shadowOffsetX: 0,
                shadowOffsetY: 0,
                shadowBlur: 20,
                borderWidth: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          },
          series: [{
              type: 'scatter',
              coordinateSystem: 'geo' // 对应上方配置
            },
            {
              name: '启动次数', // 浮动框的标题
              type: 'map',
              geoIndex: 0,
              data: [{
                "name": "北京",
                "value": 599
              }, {
                "name": "上海",
                "value": 142
              }, {
                "name": "黑龙江",
                "value": 44
              }, {
                "name": "深圳",
                "value": 92
              }, {
                "name": "湖北",
                "value": 810
              }, {
                "name": "四川",
                "value": 453
              }]
            }
          ]
        }) 
        window.addEventListener('resize',()=>{
            this.chartResize=myChart.resize()
        }) 
      }
    }
}
</script>

重点在这网上有很多china文件,但都不合适,直接引入即可

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

如果帮到大家就点个赞吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路光.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值