echarts使图表在缩放拉伸时始终和界面保持一致

1.效果
在这里插入图片描述
2.关键点

   window.addEventListener('resize', function () {
        bar.resize();
      });

3.完整代码例子

 drawbar(myecharts) {
      let bar = echarts.init(document.getElementById(myecharts));
      document.getElementById(myecharts).setAttribute("_echarts_instance_", "");
      let option = {
        // backgroundColor:"#000a3f",
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          backgroundColor: 'rgba(9, 24, 48, 0.5)',
          borderColor: 'rgba(75, 253, 238, 0.4)',
          textStyle: {
            color: '#CFE3FC',
          },
          borderWidth: 1,
        },
        grid: {
          top: '15%',
          right: '8%',
          left: "8%",
          bottom: '15%'
        },
        xAxis: [{
          name: "月份",
          type: 'category',
          data: ["一月", "二月", "三月", "四月", "五月"],
          axisLine: {
            lineStyle: {
              color: '#00000'
            }
          },
          axisLabel: {
            margin: 10,
            color: '#000000',
            textStyle: {
              fontSize: 12
            },
          },
          axisTick: {
            show: false
          }
        }],
        yAxis: [{
          name: "入园(人数)",
          axisLabel: {
            formatter: '{value}',
            color: '#000000',
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#000000'
            }
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255,255,255,0.12)'
            }
          }
        }],
        series: [{
          type: 'bar',
          data: [2000, 1520, 1850, 3400, 2756],
          radius: ['10%', '40%'],
          center: ['40%', '50%'],
          barWidth: '30%',
          itemStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: '#0FCA7A' // 0% 处的颜色
              }, {
                offset: 1,
                color: '#0FCA7A' // 100% 处的颜色
              }], false),
              shadowColor: 'rgba(0,160,221,1)',
              shadowBlur: 4,
            }
          },
          label: {
            normal: {
              show: true,
              lineHeight: 10,
              formatter: '{c}',
              position: 'top',
              textStyle: {
                color: '#00D6F9',
                fontSize: 12
              }

            }
          }
        }]
      };
      //关键在这里
      window.addEventListener('resize', function () {
        bar.resize();
      });
      bar.setOption(option);
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值