在Vue3中如何更優(yōu)雅的使用echart圖表詳解
前言
在大屏可視化項(xiàng)目中,我們常常需要用到很多的圖表組件,通常你會(huì)編寫很多的option對(duì)圖表進(jìn)行渲染,以及引入它們所需的一些組件并使用echart.use。
在Vue2中我們常常把可復(fù)用的組件單獨(dú)抽離出來,再通過props、emit等方法向復(fù)用組件中傳入組件所需數(shù)據(jù),而在Vue3中我們可以將一些邏輯功能寫成hook進(jìn)行抽離和復(fù)用再傳入到視圖中,這會(huì)不僅讓你的組件中的代碼更加優(yōu)雅而且閱讀性更強(qiáng)。
封裝思路
引入模塊
我們先創(chuàng)建lib.ts文件,用于將echart圖表中所需要用到組件全部引入進(jìn)來并導(dǎo)出。
由于引入的模塊過多,所以我們把它引入的模塊的代碼抽離出來,增加代碼的可閱讀性
// lib.ts import * as echarts from 'echarts/core'; import { BarChart, LineChart, PieChart, MapChart, PictorialBarChart, RadarChart, ScatterChart } from 'echarts/charts'; import { TitleComponent, TooltipComponent, GridComponent, PolarComponent, AriaComponent, ParallelComponent, LegendComponent, RadarComponent, ToolboxComponent, DataZoomComponent, VisualMapComponent, TimelineComponent, CalendarComponent, GraphicComponent } from 'echarts/components'; echarts.use([ LegendComponent, TitleComponent, TooltipComponent, GridComponent, PolarComponent, AriaComponent, ParallelComponent, BarChart, LineChart, PieChart, MapChart, RadarChart, PictorialBarChart, RadarComponent, ToolboxComponent, DataZoomComponent, VisualMapComponent, TimelineComponent, CalendarComponent, GraphicComponent, ScatterChart ]); export default echarts;
封裝功能
在同級(jí)目錄下創(chuàng)建一個(gè)useChart.ts文件,這是我們復(fù)用echart圖表hook文件。
封裝功能如下:
- 監(jiān)聽圖表元素變化及視圖,自動(dòng)重新渲染圖表適應(yīng)高度
- 可傳入主題、渲染模式(SVG、Canvas)
- loading效果
import { nextTick, onMounted, onUnmounted, Ref, unref } from "vue"; import type { EChartsOption } from 'echarts'; import echarts from "./lib"; import { SVGRenderer, CanvasRenderer } from "echarts/renderers"; import { RenderType, ThemeType } from "./types"; export default function useChart(elRef: Ref<HTMLDivElement>, autoChartSize = false, animation: boolean = false, render: RenderType = RenderType.SVGRenderer, theme: ThemeType = ThemeType.Default) { // 渲染模式 echarts.use(render === RenderType.SVGRenderer ? SVGRenderer : CanvasRenderer) // echart實(shí)例 let chartInstance: echarts.ECharts | null = null; // 初始化echart const initCharts = () => { const el = unref(elRef) if (!el || !unref(el)) { return } chartInstance = echarts.init(el, theme); } // 更新/設(shè)置配置 const setOption = (option: EChartsOption) => { nextTick(() => { if (!chartInstance) { initCharts(); if (!chartInstance) return; } chartInstance.setOption(option) hideLoading() }) } // 獲取echart實(shí)例 function getInstance(): echarts.ECharts | null { if (!chartInstance) { initCharts(); } return chartInstance; } // 更新大小 function resize() { chartInstance?.resize(); } // 監(jiān)聽元素大小 function watchEl() { // 給元素添加過渡 if (animation) { elRef.value.style.transition = 'width 1s, height 1s' } const resizeObserver = new ResizeObserver((entries => resize())) resizeObserver.observe(elRef.value); } // 顯示加載狀 function showLoading() { if (!chartInstance) { initCharts(); } chartInstance?.showLoading() } // 顯示加載狀 function hideLoading() { if (!chartInstance) { initCharts(); } chartInstance?.hideLoading() } onMounted(() => { window.addEventListener('resize', resize) if (autoChartSize) watchEl(); }) onUnmounted(() => { window.removeEventListener('resize', resize) }) return { setOption, getInstance, showLoading, hideLoading } }
// types.ts export enum RenderType { SVGRenderer = 'SVGRenderer', CanvasRenderer = 'SVGRenderer' } export enum ThemeType { Light = 'light', Dark = 'dark', Default = 'default' }
有了以上封裝好之后的代碼,我們?cè)诮M件中使用echart圖表庫時(shí)將會(huì)更加簡(jiǎn)單而高效。
使用例子
// index.vue <template> <div ref="chartEl" :style="{ width: `300px`, height: `300px` }"></div> </template> <script setup lang="ts"> import { onMounted, Ref, ref, computed, nextTick } from "vue"; import type { EChartsOption } from 'echarts' import useChart, { RenderType, ThemeType } from '../../useChart' import axios from 'axios' const option = computed<EChartsOption>(() => ({ // ...chart option })) const chartEl = ref<HTMLDivElement | null>(null) const { setOption, showLoading } = useChart(chartEl as Ref<HTMLDivElement>, true, true, RenderType.SVGRenderer, ThemeType.Dark) onMounted(() => { nextTick(() => { // 顯示loading showLoading() // 假裝有網(wǎng)絡(luò)請(qǐng)求 ... // 渲染圖表 setOption(option.value); }) }) </script>
Github倉庫地址(含例子):github.com/QC2168/useC…
總結(jié)
到此這篇關(guān)于在Vue3中如何更優(yōu)雅的使用echart圖表的文章就介紹到這了,更多相關(guān)Vue3使用echart圖表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue Element UI + OSS實(shí)現(xiàn)上傳文件功能
這篇文章主要為大家詳細(xì)介紹了Vue Element UI + OSS實(shí)現(xiàn)上傳文件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07vue中的.$mount(''#app'')手動(dòng)掛載操作
這篇文章主要介紹了vue中.$mount('#app')手動(dòng)掛載操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09vue使用Office?Web實(shí)現(xiàn)線上文件預(yù)覽
這篇文章主要為大家介紹了vue使用微軟的開發(fā)接口Office?Web,實(shí)現(xiàn)線上文件預(yù)覽,預(yù)覽word,excel,pptx,pdf文件,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Vite?Vue3?EsLint?Prettier配置步驟極簡(jiǎn)方法詳解
這篇文章主要為大家介紹了Vite?Vue3?EsLint?Prettier配置步驟的極簡(jiǎn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09解決vue-cli單頁面手機(jī)應(yīng)用input點(diǎn)擊手機(jī)端虛擬鍵盤彈出蓋住input問題
今天小編就為大家分享一篇解決vue-cli單頁面手機(jī)應(yīng)用input點(diǎn)擊手機(jī)端虛擬鍵盤彈出蓋住input問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08VUE實(shí)現(xiàn)一個(gè)Flappy Bird游戲的示例代碼
這篇文章主要介紹了VUE實(shí)現(xiàn)一個(gè)Flappy Bird的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Vue打包程序部署到Nginx 點(diǎn)擊跳轉(zhuǎn)404問題
這篇文章主要介紹了Vue打包程序部署到Nginx 點(diǎn)擊跳轉(zhuǎn)404問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02vue項(xiàng)目中data數(shù)據(jù)之間互相訪問的實(shí)現(xiàn)
本文主要介紹了vue項(xiàng)目中data數(shù)據(jù)之間互相訪問的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05