echart盒子没有跟着当前div大小变化而自适应

一、问题描述

当echarts图表在一个盒子里的时候,盒子大小变化了,但是图表没有跟着自适应,比如这样,盒子变大了,但是图表没变化
请添加图片描述

二、解决方法

在盒子大小更改的同时,调用图表的resize方法,记得在nextTick调用,这是我更改盒子的方法,你们只要在盒子大小更改的时候调用就可以了

  change() {
      this.width += 20;
      this.$nextTick(() => {
        this.chart.resize();
      });
    },

三、更改后的效果

请添加图片描述

四、全部代码

我把我的代码贴出来,想要试试的话可以直接粘贴,记得要是直接粘贴用的话先检查项目是否下载了echarts,

<template>
  <div class="MonitoringSensor">
    <div id="main" :style="{ width: width + 'px', height: width + 'px' }"></div>
    <button @click="change">点击</button>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'App',
  components: {},

  data() {
    return {
      chart: null,
      width: 300,
    };
  },
  mounted() {
    this.initChart();
  },

  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById('main'));
      let options = null;
      options = {
        xAxis: {
          type: 'category',
          data: ['苹果', '梨子', '香蕉'],
          axisLabel: {
            color: '#fff',
          },
        },

        yAxis: {
          type: 'value',
          max: 200,
          axisLabel: {
            color: '#fff',
          },
          splitLine: {
            lineStyle: {
              color: '#222',
            },
          },
        },
        tooltip: {
          trigger: 'axis',
        },
        series: [
          {
            type: 'bar',
            data: [100, 50, 20],
            barWidth: 30,
          },
        ],
      };
      options && this.chart.setOption(options);
    },
    change() {
      this.width += 20;
      this.$nextTick(() => {
        this.chart.resize();
      });
    },
  },
};
</script>

<style scoped>
.MonitoringSensor {
  width: 500px;
  height: 500px;

  margin: 0 auto;
  border: 1px solid red;
}

#main {
  background: rgb(24, 80, 169);
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值