Echarts设置容器宽高为rem,初始化加载会缩小

1、问题:这两天写的APP需要在一个页面中使用echarts图表,发现一个问题:在进入改页面初始化时,图表会缩小。但是把容器宽高设置为px就可以了,但是写的移动端需要适配必须rem。

 2、问题根因:div还没有创建出来echarts就已经加载了,因为获取不到宽高就默认宽高100%为100px。

3、解决方法:给echarts设置延迟加载。

mounted() {
    this.drawAssetTrendChart();
  },
  methods: {
    drawAssetTrendChart() {
      setTimeout(() => {
        let earingsTrendChart = this.$echarts.init(document.getElementById("assetChart"));
        let option = {
          color: ["#79BAFA"],
          grid: {
            top: 35,
            bottom: 30,
          },
          title: {
            text: "资产(元)",
            left: "left",
            top: "top",
            textStyle: {
              fontSize: 12,
              color: "#999",
            },
          },
          xAxis: {
            type: "category",
            boundaryGap: false,
            data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
            axisTick: {
              show: false,
            },
          },
          yAxis: {
            splitNumber: 4,
            min: 0,
            max: 40,
            interval: 10,
            type: "value",
            axisLabel: {
              show: true,
              textStyle: {
                fontSize: "12",
              },
            },
            splitLine: {
              lineStyle: {
                type: "dashed",
              },
            },
          },
          series: [
            {
              type: "line",
              stack: "Total",
              smooth: true,
              symbol: "none",
              data: [24, 16, 6, 14, 36, 28, 19],
              itemStyle: {
                normal: {
                  lineStyle: {
                    width: 1,
                  },
                },
              },
            },
          ],
        };
        earingsTrendChart.setOption(option);
      }, 200);
    },
  },

4、效果如图:

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值