uni-app引入echarts-for-wx,formatter 自定义函数不起作用的问题

原因:vue的data里不能放函数,会被序列化

解决方法:

只能手动setOptions,需要通过获取实例$curChart后。

通过this.$refs['uni-ec-canvas'].$curChart.setOption来设置echart配置

 mounted() {
      this.$nextTick(function(){
        console.log(this.$refs['uni-ec-canvas'])
        console.log(this.$refs['uni-ec-canvas'].$curChart)
      })
    },

 

发现 this.$refs['uni-ec-canvas']这个对象中存在$curChart这个属性,但是输出确是undefined

 mounted() {
      this.$nextTick(function(){
        console.log(this.$refs['uni-ec-canvas'])
        setTimeout(()=>{
          console.log(this.$refs['uni-ec-canvas'].$curChart)
        }, 2000)
      })
    },

加了个延迟后,发现可以获取到

发现setOption又是undefined

 mounted() {
      this.$nextTick(function(){
        console.log(this.$refs['uni-ec-canvas'])
        console.log(this.$refs['uni-ec-canvas'].$curChart)
        setTimeout(()=>{
          console.log(this.$refs['uni-ec-canvas'].$curChart)
          console.log(this.$refs['uni-ec-canvas'].$curChart.setOption)
          // this.$refs['uni-ec-canvas'].$curChart.setOption(this.getOption())
        }, 2000)
      })
    },

此法行不通。。。

通过观察发现,this.$refs['uni-ec-canvas']中存在ec属性和init,尝试通过以下的方式

mounted() {
        this.initChart()
       
    },
    methods: {
      initChart() {
        this.$refs['uni-ec-canvas'].ec.option = this.getOption()
        this.$refs['uni-ec-canvas'].init()
      }
}

ojbk。。。成功了

自定义 的formatter生效

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值