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

vue3.x對echarts的二次封裝之按需加載過程詳解

 更新時間:2023年09月13日 09:36:36   作者:SunnyRun!  
echarts是我們后臺系統(tǒng)中最常用的數(shù)據(jù)統(tǒng)計圖形展示,外界對它的二次封裝也不計層數(shù),這篇文章主要介紹了vue3.x對echarts的二次封裝之按需加載,需要的朋友可以參考下

效果展示

在這里插入圖片描述

1、echarts是我們后臺系統(tǒng)中最常用的數(shù)據(jù)統(tǒng)計圖形展示,外界對它的二次封裝也不計層數(shù);
2、在業(yè)務(wù)代碼內(nèi)每次的初始dom和綁定setOption導(dǎo)致代碼量的堆積不利于維護
3、拓展公共echarts組件,通過不同入?yún)㈤_啟對應(yīng)的功能更利于維護和排查問題
4、echarts的版本5.4.x

  • 當(dāng)前代碼示例只引用了:BarChart, LineChart, PieChart, GaugeChart 這幾種圖形類型,其余類型需按需引用,在useCharts.ts內(nèi)進行

1. 創(chuàng)建v-charts文件夾;

可自定義文件名

1.1 創(chuàng)建useCharts.ts 文件

該文件用作于處理和初始化echarts的公用邏輯,抽離出來使用vue3的hooks用來注水操作

1.2 創(chuàng)建v-charts.vue 文件

該文件用作于echarts的dom承載和相關(guān)api的入?yún)⒅挥猩倭窟壿嫞?/p>

2. 相關(guān)代碼的處理方式:

useCharts.ts文件

import * as echarts from 'echarts/core'
import {
  TitleComponent,
  LegendComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
  ToolboxComponent,
  MarkAreaComponent,
  MarkLineComponent,
  MarkPointComponent
} from 'echarts/components'
import { BarChart, LineChart, PieChart, GaugeChart } from 'echarts/charts'
import { LabelLayout, UniversalTransition } from 'echarts/features'
import { CanvasRenderer } from 'echarts/renderers'
import { ShallowRef, shallowRef, Ref, onBeforeUnmount, watch, useAttrs, shallowReactive } from 'vue'
interface ChartHookOption {
  theme?: Ref<string>
  el: ShallowRef<HTMLElement>
  options: any
}
/**
 *  視口變化時echart圖表自適應(yīng)調(diào)整
 */
class ChartsResize {
  #charts = new Set<echarts.ECharts>() // 緩存已經(jīng)創(chuàng)建的圖表實例
  #timeId = null
  constructor() {
    window.addEventListener('resize', this.handleResize.bind(this)) // 視口變化時調(diào)整圖表
  }
  getCharts() {
    return [...this.#charts]
  }
  handleResize() {
    clearTimeout(this.#timeId)
    this.#timeId = setTimeout(() => {
      this.#charts.forEach((chart) => {
        chart.resize()
      })
    }, 350)
  }
  add(chart: echarts.ECharts) {
    this.#charts.add(chart)
  }
  remove(chart: echarts.ECharts) {
    this.#charts.delete(chart)
  }
  removeListener() {
    window.removeEventListener('resize', this.handleResize)
  }
}
export const chartsResize = new ChartsResize()
export const useCharts = ({ el, theme, options }: ChartHookOption) => {
  echarts.use([
    BarChart,
    LineChart,
    BarChart,
    PieChart,
    GaugeChart,
    TitleComponent,
    LegendComponent,
    TooltipComponent,
    GridComponent,
    DatasetComponent,
    TransformComponent,
    LabelLayout,
    UniversalTransition,
    CanvasRenderer,
    ToolboxComponent,
    MarkAreaComponent,
    MarkLineComponent,
    MarkPointComponent
  ])
  const charts = shallowRef<echarts.ECharts>()
  const setOptions = (opt: echarts.EChartsCoreOption) => {
    charts.value.setOption(opt)
  }
  const initChart = () => {
    charts.value = echarts.init(el.value, theme)
    charts.value.setOption(options)
    chartsResize.add(charts.value) // 將圖表實例添加到緩存中
    initEvent() // 添加事件支持
  }
  // 初始化事件
  const attrs = useAttrs()
  const initEvent = () => {
    Object.keys(attrs).forEach((attrKey) => {
      if (/^on/.test(attrKey)) {
        const cb = attrs[attrKey]
        attrKey = attrKey.replace(/^on(Chart)?/, '')
        attrKey = `${attrKey[0]}${attrKey.substring(1)}`
        typeof cb === 'function' && charts.value?.on(attrKey, cb as () => void)
      }
    })
  }
  onBeforeUnmount(() => {
    chartsResize.remove(charts.value) // 移除緩存
  })
  return {
    charts,
    setOptions,
    initChart,
    initEvent
  }
}
export const chartsOptions = <T extends echarts.EChartsCoreOption>(option: T) => shallowReactive<T>(option)

v-charts.vue代碼模塊

<template>
  <div class="v-charts" ref="chartRef" />
</template>
<script lang="ts" setup>
import * as echarts from 'echarts/core'
import { useCharts, chartsResize } from './useCharts'
import { PropType, toRefs, shallowRef, onMounted, watch, nextTick } from 'vue'
const props = defineProps({
  theme: String,
  delay: [String, Boolean],
  isWatch: [String, Boolean, Object],
  options: {
    type: Object as PropType<echarts.EChartsCoreOption>,
    default: () => ({})
  },
})
const { theme, options } = toRefs(props)
const chartRef = shallowRef()
const { charts, setOptions, initChart } = useCharts({ theme, el: chartRef, options })
// 開啟默認放大縮放功能
const turnOndataZoom = () => {
  charts.value.dispatchAction({
    type: 'takeGlobalCursor',
    key: 'dataZoomSelect',
    dataZoomSelectActive: true
  })
}
onMounted(async () => {
  await initChart()
  setOptions(options.value)
})
watch(
  options,
  () => {
    setOptions(options.value)
    nextTick(() => turnOndataZoom())
  },
  {
    deep: true
  }
)
watch(
  () => props.isWatch, // 是否開啟外部左側(cè)菜單引起的布局適配問題
  () => {
    chartsResize.handleResize()
  },
  {
    deep: true,
    immediate: true
  }
)
defineExpose({
  chartRef: chartRef,
  $charts: charts
})
</script>
<script lang="ts">
export default { name: "v-charts" };
</script>
<style lang="scss" scoped>
.v-charts {
  width: 100%;
  height: 100%;
  clear: both;
  min-height: 360px;
}
</style>

3、引用組件或者全局注入

3.1 創(chuàng)建index.js文件

import vCharts from './v-charts/v-charts.vue'
export * from './v-charts/useCharts';
const components = [vCharts]; // 可添加需要全局注入的公共組件
const install = function (Vue: any) {
  components.forEach((app) => {
    Vue.component(app.name, app);
  });
};
export default install;

3.2 在main.ts文件內(nèi)進行引用

import CustomUi from '@/components/index'
app.use(CustomUi)

4、在業(yè)務(wù)文件中的使用

<template>
	<v-charts ref="myCharts" :isWatch="isActiveName" :options="setOptions" />
</template>
<script lang="ts" setup>
import { ref, reactive, shallowRef, onBeforeMount } from 'vue'
const option = {
  toolbox: {
    feature: {
      dataZoom: {
        icon: null
      }
    }
  },
  xAxis: {
    data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  },
  yAxis: {},
  series: [
    {
      type: "line",
      data: [23, 24, 18, 25, 27, 28, 25],
    },
  ],
};
const myCharts = ref(null)
const isActiveName = ref<boolean>(false)
const setOptions = shallowRef<Record<string, any>>({})
onBeforeMount(() => {
  setOptions.value = option
})
</script>

5、完結(jié)

第一版的時候setOption這塊的內(nèi)容是在組件內(nèi)部;因為我們的業(yè)務(wù)偏復(fù)雜,操作setOption內(nèi)容較多又涉及到輪詢處理數(shù)據(jù)結(jié)構(gòu);所以將該內(nèi)容放置外了;這塊可以依據(jù)自身需求而定
封裝echarts沒有過于封裝,主要是針對按需情況和初始化綁定,自適應(yīng)屏幕而定的封裝
不通的需求處理方式不通,有不合理之處還請各位諒解

到此這篇關(guān)于vue3.x對echarts的二次封裝之按需加載的文章就介紹到這了,更多相關(guān)vue3.x按需加載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue實現(xiàn)生成二維碼的簡單方式

    Vue實現(xiàn)生成二維碼的簡單方式

    與后端生成二維碼相比,前端生成二維碼更具有靈活性,下面這篇文章主要給大家介紹了關(guān)于Vue實現(xiàn)生成二維碼的簡單方式,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • vue實力踩坑?當(dāng)前頁push當(dāng)前頁無效的解決

    vue實力踩坑?當(dāng)前頁push當(dāng)前頁無效的解決

    這篇文章主要介紹了vue實力踩坑?當(dāng)前頁push當(dāng)前頁無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue實現(xiàn)hash模式網(wǎng)址方式(就是那種帶#的網(wǎng)址、井號url)

    Vue實現(xiàn)hash模式網(wǎng)址方式(就是那種帶#的網(wǎng)址、井號url)

    這篇文章主要介紹了Vue實現(xiàn)hash模式網(wǎng)址方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 前端+接口請求實現(xiàn)vue動態(tài)路由

    前端+接口請求實現(xiàn)vue動態(tài)路由

    在Vue應(yīng)用中,結(jié)合前端和后端接口請求實現(xiàn)動態(tài)路由,可根據(jù)用戶權(quán)限動態(tài)生成路由,提高安全性與靈活性,本文就來介紹一下前端+接口請求實現(xiàn)vue動態(tài)路由,感興趣的可以了解一下
    2024-09-09
  • vue前后端端口不一致的問題解決

    vue前后端端口不一致的問題解決

    本文主要介紹了vue前后端端口不一致的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-10-10
  • vue之bus總線簡單使用講解

    vue之bus總線簡單使用講解

    這篇文章主要介紹了vue之bus總線簡單使用講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • VUE中使用Vue-resource完成交互

    VUE中使用Vue-resource完成交互

    本篇文章主要介紹了VUE中使用Vue-resource完成交互,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • vue調(diào)用谷歌授權(quán)登錄獲取用戶通訊錄的實現(xiàn)示例

    vue調(diào)用谷歌授權(quán)登錄獲取用戶通訊錄的實現(xiàn)示例

    本文主要介紹了vue調(diào)用谷歌授權(quán)登錄獲取用戶通訊錄的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 基于Vue3和Canvas實現(xiàn)一個水印工具

    基于Vue3和Canvas實現(xiàn)一個水印工具

    在公眾號運營和內(nèi)容創(chuàng)作中,圖片版權(quán)保護是一個非常重要的問題,今天我分享一個基于Vue3圖片水印添加的源碼和實現(xiàn)步驟,即使你是前端新手也能輕松掌握,需要的朋友可以參考下
    2025-10-10
  • 詳解vee-validate的使用個人小結(jié)

    詳解vee-validate的使用個人小結(jié)

    本篇文章主要介紹了詳解vee-validate的使用個人小結(jié),具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-06-06

最新評論

定边县| 汶上县| 肇东市| 平阴县| 昭通市| 永顺县| 泸定县| 屯门区| 瑞安市| 顺昌县| 米林县| 松潘县| 万宁市| 眉山市| 平和县| 日喀则市| 牙克石市| 交口县| 宁强县| 那坡县| 扶余县| 天峨县| 广饶县| 陆河县| 平江县| 东明县| 西乡县| 东乡| 哈尔滨市| 昭通市| 昭觉县| 阳信县| 武山县| 吴川市| 伊金霍洛旗| 乐山市| 长武县| 石林| 乐东| 大同市| 扶绥县|