vue3使用echarts并封裝echarts組件方式
前言
本文使用的echarts版本為5.3.1,詳細(xì)文檔可見:
文中案例基于vue3編寫,有關(guān)js部分使用的是"script setup"語法糖形式
一、安裝并導(dǎo)入echart
1.npm下載包
npm i echarts --save
2.配置echarts
這里介紹使用 provide 和 injec 配置 echartst
provide、injec 可以實(shí)現(xiàn)嵌套組件之間進(jìn)行傳遞數(shù)據(jù)和方法
在父組件使用provide可向子代組件傳遞數(shù)據(jù);子級(jí)組件使用inject來獲取上級(jí)組件傳遞過來的數(shù)據(jù)
注:這兩個(gè)函數(shù)都是在setup函數(shù)中使用的
在 app.vue 下
<script>
import { provide } from 'vue'
import * as echarts from 'echarts'
export default {
setup() {
provide('echarts', echarts)
},
};
</script>
在子代組件,使用 inject 獲取 echarts 后便可使用
<script setup>
import { inject } from "vue"
const echarts = inject("echarts")
</script>
二、使用echarts
這里舉一個(gè)小例子:
<template>
<div class="echart" ref="chartDom"></div>
</template><script setup>
//按需導(dǎo)入需要用到的 vue函數(shù) 和echarts
import { ref, inject, onMounted } from "vue";
const echarts = inject("echarts");
//獲取 dom 和 父組件數(shù)據(jù) 并定義"myChart"用于初始化圖表
const chartDom = ref()
let myChart = null;
let option = reactive ({
title: { text: "總用戶量" },
tooltip: {},
xAxis: {
data: ["12-3", "12-4", "12-5", "12-6", "12-7", "12-8"],
},
yAxis: {},
series: [
{
name: "用戶量",
type: "line",
data: [5, 20, 36, 10, 10, 20],
},
],
})
//頁面成功渲染,開始繪制圖表
onMounted(() => {
myChart = echarts.init(chartDom.value)
myChart.setOption(props.option, true);
})
</script>
<style lang="scss" scoped>
.echart {
width: 800px;
height: 500px;
}
</style>
效果:

三、封裝echarts為組件
echarts封裝組件:
<template>
<div class="echart" ref="chartDom"></div>
</template>
<script setup>
//按需導(dǎo)入需要用到的 vue函數(shù) 和 echarts
import { ref, inject, onMounted, onBeforeUnmount, defineProps, watch } from "vue";
const echarts = inject("echarts");
//獲取 dom 和 父組件數(shù)據(jù) 并定義"myChart"用于初始化圖表
const chartDom = ref()
let myChart = null;
const props = defineProps({
option: Object,
})
//重繪圖表函數(shù)
const resizeHandler = () => {
myChart.resize();
}
//設(shè)置防抖,保證無論拖動(dòng)窗口大小,只執(zhí)行一次獲取瀏覽器寬高的方法
const debounce = (fun, delay) => {
let timer;
return function () {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
fun();
}, delay);
}
};
const cancalDebounce = debounce(resizeHandler, 500);
//頁面成功渲染,開始繪制圖表
onMounted(() => {
//配置為 svg 形式,預(yù)防頁面縮放而出現(xiàn)模糊問題;圖表過于復(fù)雜時(shí)建議使用 Canvas
myChart = echarts.init(chartDom.value,null, {renderer:'svg'})
// myChart = echarts.init(chartDom.value)
myChart.setOption(props.option, true);
//自適應(yīng)不同屏幕時(shí)改變圖表尺寸
window.addEventListener('resize', cancalDebounce);
})
//頁面銷毀前,銷毀事件和實(shí)例
onBeforeUnmount(() => {
window.removeEventListener('resize', cancalDebounce)
myChart.dispose()
})
//監(jiān)聽圖表數(shù)據(jù)時(shí)候變化,重新渲染圖表
watch(() => props.option, () => {
myChart.setOption(props.option, true);
}, { deep: true })
</script>
<style lang="scss" scoped>
.echart {
width: 100%;
height: 100%
}
</style>
上面的在不同屏幕大小下自適應(yīng)重繪圖表需在 flexible.js 的配置下才有效,有關(guān) flexible 的配置可參考 “移動(dòng)端與大屏幕自適應(yīng)適配方案”
注:在調(diào)用該組件時(shí)需傳入 option配置值 和 設(shè)置組件大小
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中如何給el-table-column添加指定列的點(diǎn)擊事件
elementui中提供了點(diǎn)擊行處理事件,下面這篇文章主要給大家介紹了關(guān)于vue中如何給el-table-column添加指定列的點(diǎn)擊事件,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
基于vue v-for 循環(huán)復(fù)選框-默認(rèn)勾選第一個(gè)的實(shí)現(xiàn)方法
下面小編就為大家分享一篇基于vue v-for 循環(huán)復(fù)選框-默認(rèn)勾選第一個(gè)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Vue執(zhí)行方法,方法獲取data值,設(shè)置data值,方法傳值操作
這篇文章主要介紹了Vue執(zhí)行方法,方法獲取data值,設(shè)置data值,方法傳值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
VUE v-for循環(huán)中每個(gè)item節(jié)點(diǎn)動(dòng)態(tài)綁定不同函數(shù)的實(shí)例
今天小編就為大家分享一篇VUE v-for循環(huán)中每個(gè)item節(jié)點(diǎn)動(dòng)態(tài)綁定不同函數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
el-date-picker時(shí)間清空值為null處理方案
本文介紹關(guān)于Vue.js項(xiàng)目中時(shí)間選擇器組件的問題,當(dāng)選擇后清空導(dǎo)致值變?yōu)閚ull,進(jìn)而引發(fā)后臺(tái)接口報(bào)錯(cuò),通過監(jiān)聽`overallForm.time`的值并設(shè)置為空數(shù)組,成功解決此問題,確保了數(shù)據(jù)正確性,同時(shí),建議避免直接監(jiān)聽整個(gè)對(duì)象以優(yōu)化性能,感興趣的朋友一起看看吧2024-08-08
Nuxt.js nuxt-link與router-link的區(qū)別說明
這篇文章主要介紹了Nuxt.js nuxt-link與router-link的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue3編譯報(bào)錯(cuò)ESLint:defineProps is not defined&nbs
這篇文章主要介紹了vue3編譯報(bào)錯(cuò)ESLint:defineProps is not defined no-undef的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

