Vue3?echarts組件化及使用hook進(jìn)行resize方式
echarts組件化及使用hook進(jìn)行resize
hook 本質(zhì)是一個(gè)函數(shù),把setup函數(shù)中使用的 Composition API 進(jìn)行了封裝
組件化echarts實(shí)例
<template> <div ref="echart" :style="{ height: height, width: width }"></div> </template> <script setup> import * as echarts from "echarts"; import useResize from "@/hooks/useResize"; // hook 代碼見下方 const { proxy } = getCurrentInstance(); // 獲取實(shí)例中的proxy let echart; let echartInstance; const props = defineProps({ // 數(shù)據(jù) data: { type: Array, default: [ { value: 40, name: "rose 1" }, { value: 38, name: "rose 2" }, { value: 32, name: "rose 3" }, ], }, // 高度 height: { type: [Number, String], default: "300px", }, // 寬度 width: { type: [Number, String], default: "100%", }, }); const { data } = toRefs(props); const data1 = reactive({ option: { legend: { top: "bottom", }, toolbox: { show: false, feature: { mark: { show: true }, dataView: { show: true, readOnly: false }, restore: { show: true }, saveAsImage: { show: true }, }, }, tooltip: { trigger: "item", formatter: " : {c} (ublnpf9mb%)", }, series: [ { name: "Nightingale Chart", type: "pie", radius: [10, 120], center: ["50%", "45%"], roseType: "area", itemStyle: { borderRadius: 8, }, data: data.value, }, ], }, }); const { option } = toRefs(data1); // 觀察 data ,重新繪制 watch( data, (newValue) => { option.value.series[0].data = newValue; }, { deep: true } ); watch( option, (newValue) => { echartInstance.setOption(newValue, true); }, { deep: true } ); onMounted(() => { echart = proxy.$refs.echart; // 獲取的DOM根節(jié)點(diǎn) echartInstance = echarts.init(echart, "macarons"); // 初始化 echart echartInstance.setOption(option.value, true); // 繪制 // notMerge 可選。是否不跟之前設(shè)置的 option 進(jìn)行合并。默認(rèn)為 false。即表示合并。合并的規(guī)則,詳見 組件合并模式。如果為 true,表示所有組件都會(huì)被刪除,然后根據(jù)新 option 創(chuàng)建所有新組件。 // setOption 見 https://echarts.apache.org/zh/api.html#echartsInstance.setOption }); function resize() { echartInstance.resize(); } // 暴露函數(shù) (供hook調(diào)用) defineExpose({ resize, }); useResize(); </script>
hook (useResize)
export default function () { let proxy onMounted(() => { proxy = getCurrentInstance(); // 獲取實(shí)例中的proxy window.addEventListener('resize', resize) }) onBeforeUnmount(() => { window.removeEventListener('resize', resize) }) function resize() { proxy.exposed.resize() } }
使用echarts和echarts自適應(yīng)
首先安裝echarts,這個(gè)就不介紹了,直接說怎么使用.
<!-- 創(chuàng)建一個(gè)div去顯示echarts --> <div ref="main" style="height: 300px"></div>
import {ref, provide, inject, onMounted, reactive} from "vue"; import * as echarts from "echarts"; const main = ref() // 使用ref創(chuàng)建虛擬DOM引用,使用時(shí)用main.value onMounted( () => { init() } ) function init() { // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例 var myChart = echarts.init(main.value); // 指定圖表的配置項(xiàng)和數(shù)據(jù) var option = { /*title: { text: 'ECharts 入門示例' },*/ tooltip: {}, // color:['#779ffe','#a07dfe','#fc9b2e','#63f949','#fb6464','#fce481'], /*grid: { left: '30%', right: '4%', bottom: '3%', containLabel: true },*/ legend: { // data: ['國(guó)家類型','非國(guó)家類型','個(gè)人','法人','可公式','非公式'] }, xAxis: { type: 'category', data: ['國(guó)家類型','非國(guó)家類型','個(gè)人','法人','可公式','非公式'] }, yAxis: { type: 'value', scale: true, max: 150, min: 0, splitNumber: 3, }, series: [ { data: [ { value: 120, itemStyle: { color: '#7fa6fe' } }, { value: 90, itemStyle: { color: '#a17fff' } }, { value: 40, itemStyle: { color: '#fda630' } }, { value: 120, itemStyle: { color: '#93fc73' } }, { value: 120, itemStyle: { color: '#fb6666' } }, { value: 120, itemStyle: { color: '#fbe068' } } ], type: 'bar' } ] }; // 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。 myChart.setOption(option); }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue2.x中的provide和inject用法小結(jié)
這篇文章主要介紹了vue2.x中的provide和inject用法小結(jié),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12Vue中狀態(tài)管理器(vuex)詳解以及實(shí)際應(yīng)用場(chǎng)景
Vuex是一個(gè)專為Vue.js應(yīng)用程序開發(fā)的狀態(tài)管理模式,下面這篇文章主要給大家介紹了關(guān)于Vue中狀態(tài)管理器(vuex)詳解以及實(shí)際應(yīng)用場(chǎng)景的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11解決vue的變量在settimeout內(nèi)部效果失效的問題
今天小編就為大家分享一篇解決vue的變量在settimeout內(nèi)部效果失效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08Vue單頁(yè)面應(yīng)用中實(shí)現(xiàn)Markdown渲染
這篇文章主要介紹了Vue單頁(yè)面應(yīng)用中如何實(shí)現(xiàn)Markdown渲染,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2021-02-02vue中動(dòng)態(tài)參數(shù)與計(jì)算屬性的使用方法
在平時(shí)vue開發(fā)中,我們經(jīng)常會(huì)用到計(jì)算屬性(計(jì)算屬性只有在它的相關(guān)依賴發(fā)生改變時(shí)才會(huì)重新求值)來計(jì)算我們需要的數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于vue中動(dòng)態(tài)參數(shù)與計(jì)算屬性使用的相關(guān)資料,需要的朋友可以參考下2021-08-08