小程序使用echarts封装成自定义组件的实现方法
2021/6/7 20:58:30
本文主要是介绍小程序使用echarts封装成自定义组件的实现方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
小程序中自定义 echarts 组件,实现复用的方法。
1下载echarts
官网下载
2 json 中引入echarts 组件
"usingComponents": { "ec-canvas": "../ec-canvas/ec-canvas" }
3 js 中引入echarts 组件及方法
import * as echarts from '../ec-canvas/echarts' Component({ /** * 组件的属性列表 */ properties: { chartLineId: { type: String }, canvasId: { type: String }, height: { type: String }, options: { type: Object } }, /** * 组件的初始数据 */ data: { ec: { lazyLoad: true, // 延迟加载 }, }, lifetimes: { ready() { this[this.data.chartLineId] = this.selectComponent('#' + this.data.chartLineId); this.getData(); }, detached(e) { this[this.data.chartLineId] = null this[this.data.canvasId] = null }, }, /** * 组件的方法列表 */ methods: { getData() { this.initChart(); }, initChart() { this[this.data.chartLineId].init((canvas, width, height, dpr) => { const chart = echarts.init(canvas, null, { width: width, height: height, devicePixelRatio: dpr // new }) chart.setOption(this.getOption()) return chart }) }, getOption() { var option = this.data.options; return option; }, } })
4 wxml
子组件
<view style="height: {{ height }};width:100%"> <ec-canvas id="{{ chartLineId }}" canvas-id="{{ canvasId }}" ec="{{ec}}" options="{{options}}"></ec-canvas> </view>
父组件
<chart options="{{ options }}" canvasId="{{aa}}" chartLineId="{{bb}}" height="300px"></chart>
这篇关于小程序使用echarts封装成自定义组件的实现方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-30实现小程序内部扫码签到和微信扫一扫签到-icode9专业技术文章分享
- 2024-06-30微信内调用扫码扫描小程序码如何解析链接-icode9专业技术文章分享
- 2024-06-30小程序码链接加密了如何解-icode9专业技术文章分享
- 2024-05-302024年最新版云开发cms开通步骤,开始开发微信小程序前的准备工作,认真看完奥!
- 2024-03-30微信小程序的网络设置,及网络请求:wx.request(OBJECT)
- 2024-01-22基于taro搭建小程序多项目框架
- 2024-01-13小程序开发:在插件市场寻找步骤条组件并二开
- 2024-01-05钉钉小程序生态—企业机器人加互动卡片,改善用户体验的开始!
- 2023-12-29【UniApp】-uni-app-打包成小程序
- 2023-12-26性能翻倍!京东亿级体量小程序优化实践