UNIAPP怎么引入在项目里面引入echart

在uniapp项目中,没有所谓的标签,只有对应的组件,这里就要借助 renderjs-echarts-demo ,它能把对应的JS和HTML转换为对应的组件,只支持H5和APP

第一步 :引入renderjs-echarts-demo 

在插件市场找到renderjs-echarts-demo ,直接引入地址

这个插件也表示,限制条件为

 

第二步,在所需的页面加入渲染代码

创建一个vue页面, 添加多一个script,module="echarts" lang="renderjs"

<script module="echarts" lang="renderjs">
	let myChart
	export default {
		mounted() {
			if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script')
				// view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
				script.src = '你的eachartJS文件,比如static/echarts/echarts.js'
				script.onload = this.initEcharts.bind(this)
				document.head.appendChild(script)
			}
		},
		methods: {
			initEcharts() {
				myChart = echarts.init(document.getElementById('echarts'))
				// 观测更新的数据在 view 层可以直接访问到
				myChart.setOption(this.option)
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				// 监听 service 层数据变更
				myChart.setOption(newValue)
			},
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				ownerInstance.callMethod('onViewClick', {
					test: 'test'
				})
			}
		}
	}
</script>

在template写入对应的组件,prop就是对应的参数配置,具体参考echart官网,

<view ref="chart" @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts" class="echarts"></view>

这样我们就可以得到对应的图表了

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章