vue项目中引用Echarts图表及遇到的问题

vue项目中引用Echarts图表及遇到的问题

一、初始化使用npm安装echarts:

npm install echarts -S
//或
cnpm install echarts -S

二、在main.js中引入echarts:

import Vue from 'vue'
import echarts from 'echarts'
Vue.prototype.$echarts = echarts;

三、单页面引用案例:

<template>
  <div class="exercisesList">
    <div
      style="width: auto;height: 400px;margin-top: 20px;"
      ref="echart"
      class="echart"
    >
    </div>
  </div>
</template>
<script>
export default {
  name: "echart",
  data() {
    return {};
  },
  mounted() {
    this.$nextTick(() => {
      this.getEcharts();
    });
  },
  methods: {
    getEcharts() {
      let myChart = this.$echarts.init(this.$refs.echart);
      myChart.setOption({
        title: { text: "难易度分析" },
        tooltip: {
          trigger: "item",
        },
        legend: {
          top: "5%",
          left: "center",
        },
        series: [
          {
            type: "pie",
            radius: "50%",
            data: [
              { value: 104, name: "容易" },
              { value: 73, name: "较易" },
              { value: 58, name: "一般" },
              { value: 48, name: "较难" },
              { value: 30, name: "困难" },
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      });
    },
  },
};
</script>
<style  lang="scss" scoped>
</style>
  

效果图如下:

在这里插入图片描述
四、使用中遇到的问题:

在这里插入图片描述
尝试了好几种办法都未果,最后使用this.$nextTick(() => {});成功解决,产生问题原因是,在标签还没有加载的时候该方法就被调用了,解决办法:

 mounted() {
    this.$nextTick(() => {
      this.getEcharts();
    });
  },

其他办法(自行选择):
1.如果不需要操作dom就在created,操作dom就在mounted里面。
2.使用v-show来控制是否显示图形,v-show是控制图形是否显示,只是dom是否隐藏了的问题,dom树也是创建完成了的
3.this.$nextTick(() => {this.getEcharts()});
4.定时器setTimeout(() => { this.getEcharts() }, 0)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值