uniapp vue3微信小程序echarts 组件封装

在上一篇的基础上,将图表封装成了组件,技术有限,大家凑合着看吧

<template>

<view class="chart-container">

<canvas id="myChart" type="2d" canvas-id="myChart" style="width: 100%; height: 400px;"></canvas>

</view>

</template>

<script setup>

import {

onMounted,

nextTick,

getCurrentInstance,

onUnmounted,

onBeforeUnmount

} from 'vue'

import * as echarts from 'echarts'

let chartInstance = null

const proxy = getCurrentInstance().proxy

const loopInit = () => {

nextTick(() => {

const query = uni.createSelectorQuery().in(proxy)

query.select('#myChart')

.fields({

node: true,

size: true

})

.exec(res => {

// 先判断节点是否获取成功,避免空指针报错

if (!res || !res0) return

const canvas = res0.node

canvas.addEventListener = () => {}

canvas.removeEventListener = () => {}

// 如果后续还遇到类似preventDefault的报错,也可以同步补全

canvas.preventDefault = () => {}

// 从selectorQuery结果中拿到容器的实际宽高

const {

width,

height

} = res0

// 传入小程序环境必需的初始化配置

const chartInstance = echarts.init(canvas, null, {

width: width,

height: height,

devicePixelRatio: uni.getSystemInfoSync().pixelRatio,

useDirtyRect: false, // 小程序环境关闭脏矩形优化,避免兼容问题

useCoarsePointer: true, // 适配移动端触摸交互

pointerSize: 40 // 适配触摸点击的热区大小

})

const option = {

tooltip: {

trigger: 'axis'

},

grid: {

top: '10%',

left: '3%',

right: '4%',

bottom: '20%',

containLabel: true

},

xAxis: {

type: 'category',

data: props.xData

},

yAxis: {

type: 'value'

},

legend: {

data: props.seriesList

},

series: props.seriesList.map(item => ({

name: item.name,

type: 'line', // 线性折线

data: item.data,

smooth: false, // 标准直线,true为平滑曲线

// 区域填充可选

// areaStyle: { opacity: 0.15 }

}))

}

// chartInstance.setOption(option)

/* const option = {

title: { text: '示例图表' },

tooltip: {},

xAxis: { data: 'A', 'B', 'C', 'D', 'E' },

yAxis: {},

series: [{

name: '销量',

type: 'bar',

data: 10, 20, 30, 40, 50

}]

}*/

chartInstance.setOption(option)

})

})

}

const props = defineProps({

xData: {

type: Array,

default: () => \[\]

},

seriesList: {

type: Array,

default: () => \[\]

},

legendlist: {

type: Array,

default: () => \[\]

}

})

onMounted(() => {

nextTick(() => {

// 延迟400ms再启动循环,给组件canvas渲染时间

setTimeout(loopInit, 400)

})

})

</script>

相关推荐
Splashtop高性能远程控制软件4 天前
微软2026年11月补丁星期二解析:1094个 CVE、Outlook 零日 CVE-2026-124194 与修复优先级
microsoft·网络安全·outlook·补丁管理·漏洞管理
公子小六7 天前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
Nerd(BUG版)7 天前
鸿蒙API18~26新增V2组件全解析
microsoft·华为·harmonyos
解决问题no解决代码问题12 天前
日志脱敏双链路统一优化:从两套枚举到单一规则源,兼顾安全与可维护性
microsoft
Dovis(誓平步青云)12 天前
启元 T1 Pro 人机相声搭档:语音-触摸双通道的启元机器人节奏引擎设计
开发语言·前端·javascript·microsoft·交互
宝桥南山12 天前
.NET 11 - 尝试一下Blazor中的新特性SupplyParameterFromTempData
microsoft·微软·c#·asp.net·.net·.netcore
Liangwei Lin13 天前
Exchange Server 2019 部署手册
microsoft
ms365copilot14 天前
Microsoft 365 Copilot跨工作资料整理
microsoft·copilot
中二骚年14 天前
Windows 安全中心 / 防火墙故障,calc和Microsoft Store,终端(管理员)无法正常打开。 原因 + 修复方案
windows·安全·microsoft
kk_net889914 天前
微软常用运行库合集免费下载安装教程(附运行库安装包)微软运行库安装图文教程
其他·microsoft