在Vue3中如何更優(yōu)雅的使用echart圖表詳解
前言
在大屏可視化項目中,我們常常需要用到很多的圖表組件,通常你會編寫很多的option對圖表進行渲染,以及引入它們所需的一些組件并使用echart.use。
在Vue2中我們常常把可復用的組件單獨抽離出來,再通過props、emit等方法向復用組件中傳入組件所需數(shù)據(jù),而在Vue3中我們可以將一些邏輯功能寫成hook進行抽離和復用再傳入到視圖中,這會不僅讓你的組件中的代碼更加優(yōu)雅而且閱讀性更強。
封裝思路
引入模塊
我們先創(chuàng)建lib.ts文件,用于將echart圖表中所需要用到組件全部引入進來并導出。
由于引入的模塊過多,所以我們把它引入的模塊的代碼抽離出來,增加代碼的可閱讀性
// 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;封裝功能
在同級目錄下創(chuàng)建一個useChart.ts文件,這是我們復用echart圖表hook文件。
封裝功能如下:
- 監(jiān)聽圖表元素變化及視圖,自動重新渲染圖表適應(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實例
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實例
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'
}有了以上封裝好之后的代碼,我們在組件中使用echart圖表庫時將會更加簡單而高效。
使用例子
// 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ò)請求 ...
// 渲染圖表
setOption(option.value);
})
})
</script>Github倉庫地址(含例子):github.com/QC2168/useC…
總結(jié)
到此這篇關(guān)于在Vue3中如何更優(yōu)雅的使用echart圖表的文章就介紹到這了,更多相關(guān)Vue3使用echart圖表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3?HighCharts自定義封裝之徑向條形圖的實戰(zhàn)過程
highcharts是國外知名基于javascript的圖表庫,下面這篇文章主要給大家介紹了關(guān)于vue3?HighCharts自定義封裝之徑向條形圖的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09
vue watch深度監(jiān)聽對象實現(xiàn)數(shù)據(jù)聯(lián)動效果
這篇文章主要介紹了vue watch深度監(jiān)聽對象實現(xiàn)數(shù)據(jù)聯(lián)動的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08
如何使用Vue3+elementPlus的Tree組件實現(xiàn)一個拖拽文件夾管理
最近在做一個文件夾管理的功能,要實現(xiàn)一個樹狀的拖拽文件夾面板,里面包含兩種元素,文件夾以及文件,這篇文章主要介紹了使用Vue3+elementPlus的Tree組件實現(xiàn)一個拖拽文件夾管理?,需要的朋友可以參考下2023-09-09
Vue分別運用class綁定和style綁定通過點擊實現(xiàn)樣式切換
這篇文章主要為大家介紹了Vue分別運用class綁定和style綁定通過點擊實現(xiàn)樣式切換,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
vue實現(xiàn)路由跳轉(zhuǎn)動態(tài)title標題信息
這篇文章主要介紹了vue實現(xiàn)路由跳轉(zhuǎn)動態(tài)title標題信息,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06

