wepy 小程序 echarts 搭配使用

本文介绍了如何在微信小程序中利用wepy框架与echarts库进行结合,实现数据可视化效果。通过配置wepy组件,导入echarts模块,并设置图表配置项,展示了在小程序环境下创建动态更新的图表实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

组件:
canvas-id不能自定义貌似是wepy的原因
<style>
</style>
<template>
  <canvas class="ec-canvas" canvas-id="eChart" ></canvas>
</template>
<script>
  import wepy from 'wepy'
  import * as echarts from '../plugins/ec-canvas/echarts'
  import WxCanvas from '../plugins/ec-canvas/wx-canvas'
  export default class ecCanvas extends wepy.component {
    props = {
      ec: {
        type: Object
      },
      canvasId: {
        type: String,
        default: 'eChart'
      }
    }
    data = {
      ec: null,
      canvasId: null,
      chart: null
    }
    onLoad() {
      const version = wx.version.version.split('.').map(n => parseInt(n, 10))
      const isValid = version[0] > 1 || (version[0] === 1 && version[1] >= 9) || (version[0] === 1 && version [1] === 9 && version[2] >= 91)
      if (!isValid) {
        console.error('This version of Wexin is not supported by ECharts. Please update Wexin with versions after 1.9.91')
        return
      }
      const ctx = wx.createCanvasContext(this.canvasId, this.$wxpage)

      const canvas = new WxCanvas(ctx)
      echarts.setCanvasCreator(() => {
        return canvas
      })
      var query = wx.createSelectorQuery().in(this.$wxpage)
      query.select('.ec-canvas').boundingClientRect(res => {
        if (this.ec && this.ec.onInit) {
          this.chart = this.ec.onInit(canvas, res.width, res.height)
        }
      }).exec()
    }
    methods = {
      touchStart: function(e) {
        if (this.chart && e.touches.length > 0) {
          var touch = e.touches[0]
          this.chart._zr.handler.dispatch('mousedown', {
            zrX: touch.x,
            zrY: touch.y
          })
          this.chart._zr.handler.dispatch('mousemove', {
            zrX: touch.x,
            zrY: touch.y
          })
        }
      },
      touchMove: function(e) {
        if (this.chart && e.touches.length > 0) {
          var touch = e.touches[0];
          this.chart._zr.handler.dispatch('mousemove', {
            zrX: touch.x,
            zrY: touch.y
          })
        }
      },
      touchEnd: function (e) {
        if (this.chart) {
          var touch = e.changedTouches ? e.changedTouches[0] : {}
          this.chart._zr.handler.dispatch('mouseup', {
            zrX: touch.x,
            zrY: touch.y
          })
          this.chart._zr.handler.dispatch('click', {
            zrX: touch.x,
            zrY: touch.y
          })
        }
      }
    }
  }
</script>

page:

</template>
  <view class="container">
    <ecCanvas :ec.sync="ec"></ecCanvas>
  </view>
</template>
<script>
  import wepy from 'wepy'
  import echarts from '../plugins/ec-canvas/echarts'
  import ecCanvas from '../components/eCanvas'
  export default class page extends wepy.page {
和echarts一致
components = {
  ecCanvas: ecCanvas
}
}
未经允许请勿转载
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值