使用Vue3封裝一個通用echarts組件詳解
實現(xiàn)這個組件需要引入echarts和vue-echarts插件,使用vue-echarts是因為它幫我們封裝了一些很常用的功能,比如監(jiān)聽頁面resize后重新渲染功能,本次組件只使用到了autoresize配置,其它可以根據(jù)官方文檔按需選配
https://github.com/ecomfe/vue-echarts
首先引入echarts和vue-echarts插件
npm install echarts vue-echarts -S
組件定義參數(shù)如下
import type { ECBasicOption } from 'echarts/types/dist/shared'
const props = defineProps({
// echarts options 傳參
option: {
type: Object as PropType<ECBasicOption>,
required: true,
},
// 容器寬度
width: {
type: String,
default: '100%',
},
// 容器高度
height: {
type: String,
default: '400px',
},
})組件代碼如下
<script setup lang="ts">
import { PropType, provide } from 'vue'
import type { ECBasicOption } from 'echarts/types/dist/shared'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
// 按需引入
import { PieChart, LineChart, FunnelChart, CustomChart } from 'echarts/charts'
import {
TitleComponent,
TooltipComponent,
LegendComponent,
GridComponent,
ToolboxComponent,
} from 'echarts/components'
import VChart, { THEME_KEY } from 'vue-echarts'
use([
CanvasRenderer,
PieChart,
TitleComponent,
TooltipComponent,
LegendComponent,
GridComponent,
LineChart,
ToolboxComponent,
FunnelChart,
CustomChart,
])
// 傳入主題
provide(THEME_KEY, 'light')
const props = defineProps({
option: {
type: Object as PropType<ECBasicOption>,
required: true,
},
width: {
type: String,
default: '100%',
},
height: {
type: String,
default: '400px',
},
})
</script>
<template>
<div class="chart-container" :style="{ width, height }">
<VChart class="w-full h-full" :option="props.option" autoresize />
</div>
</template>到此這篇關(guān)于使用Vue3封裝一個通用echarts組件詳解的文章就介紹到這了,更多相關(guān)Vue3封裝通用echarts組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項目中如何實現(xiàn)網(wǎng)頁的截圖功能?(html2canvas)
這篇文章主要介紹了vue項目中如何實現(xiàn)網(wǎng)頁的截圖功能?(html2canvas),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
element ui 對話框el-dialog關(guān)閉事件詳解
下面小編就為大家分享一篇element ui 對話框el-dialog關(guān)閉事件詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
vue中v-for循環(huán)選中點擊的元素并對該元素添加樣式操作
這篇文章主要介紹了vue中v-for循環(huán)選中點擊的元素并對該元素添加樣式操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Vuex進行Echarts數(shù)據(jù)頁面初始化后如何更新dom
這篇文章主要為大家詳細介紹了使用Vuex做Echarts數(shù)據(jù)時,當頁面初始化后如何更新dom,文中的示例代碼講解詳細,有需要的小伙伴可以跟隨小編一起學習一下2023-11-11
如何解決Vue3組合式API模式下動態(tài)組件不渲染問題
這篇文章主要介紹了如何解決Vue3組合式API模式下動態(tài)組件不渲染問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教<BR>2024-03-03
Vue axios 中提交表單數(shù)據(jù)(含上傳文件)
本篇文章主要介紹了Vue axios 中提交表單數(shù)據(jù)(含上傳文件),具有一定的參考價值,有興趣的可以了解一下2017-07-07

