最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在Vue3中如何更優(yōu)雅的使用echart圖表詳解

 更新時(shí)間:2022年05月23日 09:04:43   作者:_island  
ECharts是一個(gè)強(qiáng)大的畫圖插件,在vue項(xiàng)目中,我們常??梢砸肊charts來(lái)完成完成一些圖表的操作,下面這篇文章主要給大家介紹了關(guān)于在Vue3中如何更優(yōu)雅的使用echart圖表的相關(guān)資料,需要的朋友可以參考下

前言

在大屏可視化項(xiàng)目中,我們常常需要用到很多的圖表組件,通常你會(huì)編寫很多的option對(duì)圖表進(jìn)行渲染,以及引入它們所需的一些組件并使用echart.use。

在Vue2中我們常常把可復(fù)用的組件單獨(dú)抽離出來(lái),再通過(guò)props、emit等方法向復(fù)用組件中傳入組件所需數(shù)據(jù),而在Vue3中我們可以將一些邏輯功能寫成hook進(jìn)行抽離和復(fù)用再傳入到視圖中,這會(huì)不僅讓你的組件中的代碼更加優(yōu)雅而且閱讀性更強(qiáng)。

封裝思路

引入模塊

我們先創(chuàng)建lib.ts文件,用于將echart圖表中所需要用到組件全部引入進(jìn)來(lái)并導(dǎo)出。

由于引入的模塊過(guò)多,所以我們把它引入的模塊的代碼抽離出來(lái),增加代碼的可閱讀性

// 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)聽(tīng)圖表元素變化及視圖,自動(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)聽(tīng)元素大小
    function watchEl() {
        // 給元素添加過(guò)渡
        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圖表庫(kù)時(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倉(cāng)庫(kù)地址(含例子):github.com/QC2168/useC…

總結(jié)

到此這篇關(guān)于在Vue3中如何更優(yōu)雅的使用echart圖表的文章就介紹到這了,更多相關(guān)Vue3使用echart圖表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

鄢陵县| 海林市| 乌兰察布市| 临邑县| 贡觉县| 黄浦区| 富平县| 黄冈市| 平昌县| 微山县| 兴安县| 阿尔山市| 纳雍县| 大厂| 汉沽区| 莆田市| 开远市| 彰化市| 巩留县| 岐山县| 科技| 务川| 美姑县| 云龙县| 永州市| 东兰县| 浦城县| 永新县| 象山县| 容城县| 阿勒泰市| 深水埗区| 石林| 天峻县| 廉江市| 山东省| 九台市| 乌拉特中旗| 新巴尔虎右旗| 青阳县| 洪泽县|