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

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-21 09:36   912   0

在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>

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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP