uniapp 引入echarts(app端、H5)

文章介绍了如何在uniapp中引入echarts.js,创建echarts.vue组件,并在需要的地方使用。通过动态引入库,设置图表选项,监听数据变化,以及调用方法进行交互。还提供了在有多个echarts实例时的处理建议。
摘要由CSDN通过智能技术生成

uniapp 引入 echarts

1.首先引入echarts.js

可在百度网盘上获取该文件
链接:https://pan.baidu.com/s/1Bv-1OKh3h5by8II3P9vLZA
提取码:8624

2.将echarts.js文件直接放入static文件里

在这里插入图片描述
3.在components里新建echarts.vue

在这里插入图片描述
在echarts.js里写如下代码:

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

<script>
    export default {
        data() {
            return {}
        },
        props: ['option'],
        onLoad() {
        },
        methods: {
            onViewClick(options) {
                console.log(options)
            }
        }
    }
</script>

<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 = 'static/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>

<style>
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

    .echarts {
        margin-top: 100px;
        width: 100%;
        height: 300px;
    }
</style>

4.然后在需要echarts的地方引入就可以了。

可以直接修改他的 option值

<template>
    <view>
        <echarts style="background-color: #ccc;" :option='option'></echarts>
    </view>
</template>

<script>
    import echarts from '@/components/echarts.vue'
    export default {
        name: 'overview',
        data() {
            return {
                option: {
                    type: 'line',
                    xAxis: {
                        type: 'category',
                        data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        data: [160, 170, 184, 198, 200, 234, 260, 260, 270, 271, 278, 298],
                        type: 'line'
                    }, {
                        data: [60, 70, 84, 98, 100, 114, 160, 169, 170, 171, 178,230],
                        type: 'line'
                    }],
                    color:['#ff0000', '#0DCE00']
                }
            }
        },
        components: {
            echarts
        },
        mounted() {

        },
        methods: {

        }
    }
</script>

<style scoped lang="scss">
.circle{
    width: 750rpx;
    height: 373rpx;
    background-color: lavender;
}
.data{
    width: 721rpx;
    height: 617rpx;
}
</style>

注意:这是建议的封装,如果一个页面需要加载两个echarts时,option数据需要放置在 《script module=“echarts” lang=“renderjs”》标签中。

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值