echarts 中图表实例 和 暂无数据切换,图表不显示问题

echarts 中 图表无数据于自定义暂无数据实现来回切换,图表不显示问题
// 图表实例对象
在这里插入图片描述
关键代码

// 场景当数据 indexList 为空时,显示暂无数据,其中 no-data 为封装的暂无数据的组件
<div
        id="chartLine"
        :class="[$style.chartLine, !indexList.length ? $style.noData : '']"
></div>
<no-data emptyText="暂无数据" v-if="!indexList.length"></no-data>

// 监听数据来源
watch: {
    lineData: {
      deep: true,
      handler(val) {
        if (val.length) {
          this.timeIndex = 0;
          this.indexList = val;
          this.$nextTick(() => {  //  重点,这里需要使用 $nextTick 函数,因为initChart 中操作了图表实例的 DOM,要放在 nextTick 回调函数中
          // 在数据变化后要执行某个回调函数,而这个操作需要使用随数据改变而改变的DOM结构的时候, 这个操作都应该放进Vue.nextTick () 回调函数中
            this.initChart();
          });
        } else {
          this.indexList = [];
        }
      }
    }
  },


// 配合css的高度
.chartLine {
    width: 100%;
    height: 80%;
    &.noData {
      height: 0;  // 重点,通过高度的有无,来显示图表和暂无数据盒子的切换
    }
  }
  
// 封装的暂无数据组件
<template>
  <div :class="$style.emptyData">
    <div :class="$style.emptyImg">
      <img src="@/assets/images/noData.png" alt="" />
    </div>
    <div :class="$style.emptyText">{{ emptyText }}</div>
  </div>
</template>
<script>
export default {
  name: 'NoData',
  props: {
    emptyText: {
      type: String,
      default: ''
    }
  },
  data() {
    return {};
  }
};
</script>
<style lang="scss" module>
.emptyData {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  .emptyImg {
    width: 45%;
    height: 50%;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .emptyText {
    font-size: 12px;
    color: rgba(0, 0, 0, 0.25);
  }
}
</style>

最终暂无数据效果,这样就解决了来回切换,图表正常展示问题
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值