美文网首页
uniapp-安卓使用echarts

uniapp-安卓使用echarts

作者: 扶得一人醉如苏沐晨 | 来源:发表于2023-06-28 17:06 被阅读0次

前言

在uniapp-安卓中使用echarts与vue中使用echarts很不一样,
主要区别于在android中没办法直接操作dom需要借用render.js层来进行交互,于是对**echarts在安卓中的使用进行了一个封装。

tips:在uniapp-安卓中还有一些坑

1、在引入组件时的引入方式不生效;

//这种引入方式不行
components:{
 zujian:()=>import("@/pages/...")
}
//这种引入方式可以
import zujian from '@/pages/...'
components:{
    zujian
}

2、引入图片时不生效

//这种引入方式不行 
<img src="" height="220" >
//必须使用uniapp自带图片标签 但是以下这种方式也不行
<image class="image" src="@/static/images/wap-doing/doing.png"></image>
//这种引入方式可以
<image class="image" src="/static/images/wap-doing/doing.png"></image>

一、下载Echarts的依赖

这里推荐使用官网(ECharts 在线构建)定制下载,这样会方便我们使用。

1.1、选择柱状图,折线图,饼图;这三样是平常较常用到的

image.png

1.2、坐标系选择直角坐标系

image.png

1.3、组件可以全选,也可以选择自己所需要的,在这里个人建议除了工具栏不选,其他都选上

image.png

1.4、点击下载(成功后会下载一个echarts.min.js)

image.png

1.5、将echarts.min.js放入static

image.png

二、封装ECharts组件

2.1、在components新建echarts.vue

image.png

2.2、代码如下

<!--
    author:yangwenbin
    time:2023/6/29
    tips:属性:1.moduleParam:传id 以及宽高的对象 2.optionData:传渲染数据
 -->
<template>
    <view class="content">
        <!-- #ifdef APP-PLUS || H5 -->
        <view @click="echarts.onClick" :style="{ width:moduleParam.width, height:moduleParam.height}"
            :prop="optionData" :moduleParamProp="moduleParam"
            :change:moduleParamProp="echarts.moduleParamUp" :change:prop="echarts.updateEcharts"
            :id="moduleParam.id" class="echarts"></view>
        <!-- #endif -->
        <!-- #ifndef APP-PLUS || H5 -->
        <view>非 APP、H5 环境不支持</view>
        <!-- #endif -->
    </view>
</template>
<script>
    export default {
        data() {
            return {}
        },
        props: {
            moduleParam: {
                type: Object,
                default: () => {
                    return {
                        id: "myCharts",
                        width: "100%",
                        height: "374rpx"
                    }
                }
            },
            optionData: {
                type: Object,
                default: () => {}
            },
        },
        methods: {
            onViewClick(options) {
                this.$emit("getClickData", options)
            }
        }
    }
</script>
<script module="echarts" lang="renderjs">
    let myChart
    export default {
        data() {
            return {
                clickData: null
            }
        },
        mounted() {
            if (typeof window.echarts === 'function') {
                this.initEcharts()
            } else {
                // 动态引入较大类库避免影响页面展示
                const script = document.createElement('script')
                // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
                script.src = 'static/echarts/echarts.min.js'
                script.onload = this.initEcharts.bind(this)
                document.head.appendChild(script)
            }
        },
        methods: {
            initEcharts() {
                myChart = echarts.init(document.getElementById(this.moduleParam.id), "echartsConfig")
                // 观测更新的数据在 view 层可以直接访问到
                myChart.setOption(this.optionData)
                // 点击传参
                myChart.on('click', params => {
                    this.clickData = params
                })
            },
            updateEcharts(newValue, oldValue, ownerInstance, instance) {
                // 监听 service 层数据变更
                try {
                    myChart = echarts.init(document.getElementById(this.moduleParam.id), "echartsConfig")
                    myChart.setOption(newValue)
                } catch (error) {}
            },
            moduleParamUp(newvalue, oldvalue) {},
            onClick(event, ownerInstance) {
                ownerInstance.callMethod('onViewClick', {
                    value: this.clickData.value,
                    name: this.clickData.name,
                    dataIndex: this.clickData.dataIndex,
                    seriesName: this.clickData.seriesName
                })
            }
        }
    }
</script>
<style>
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }
</style>

三、使用

<template>
    <dom-echart :moduleParam="moduleParam1" :optionData="option"></dom-echart>
</template>
<script>
    import domEchart from '@/components/echarts.vue';
    export default {
        // 注册组件
        components: {
            domEchart
        },
        data() {
            return {
                moduleParam1: {
                    id: "moId1",
                    height: '50vh',
                    width: '100%'
                },
                option: {
                    title: {
                        text: '销售'
                    },
                    tooltip: {},
                    legend: {
                        data: ['销量']
                    },
                    xAxis: {
                        data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
                    },
                    yAxis: {},
                    series: [{
                        name: '销量',
                        type: 'bar',
                        data: [5, 20, 36, 10, 10, 20]
                    }]
                },
            }
        },
    }
</script>

相关文章

网友评论

      本文标题:uniapp-安卓使用echarts

      本文链接:https://www.haomeiwen.com/subject/iglgydtx.html