Vue3?echarts組件化及使用hook進行resize方式
更新時間:2023年04月20日 09:11:32 作者:瞎跑的uice
這篇文章主要介紹了Vue3?echarts組件化及使用hook進行resize方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
echarts組件化及使用hook進行resize
hook 本質(zhì)是一個函數(shù),把setup函數(shù)中使用的 Composition API 進行了封裝

組件化echarts實例
<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(); // 獲取實例中的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} (vvxyksv9kd%)",
},
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é)點
echartInstance = echarts.init(echart, "macarons"); // 初始化 echart
echartInstance.setOption(option.value, true); // 繪制
// notMerge 可選。是否不跟之前設(shè)置的 option 進行合并。默認為 false。即表示合并。合并的規(guī)則,詳見 組件合并模式。如果為 true,表示所有組件都會被刪除,然后根據(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(); // 獲取實例中的proxy
window.addEventListener('resize', resize)
})
onBeforeUnmount(() => {
window.removeEventListener('resize', resize)
})
function resize() {
proxy.exposed.resize()
}
}
使用echarts和echarts自適應(yīng)
首先安裝echarts,這個就不介紹了,直接說怎么使用.
<!-- 創(chuàng)建一個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引用,使用時用main.value
onMounted(
() => {
init()
}
)
function init() {
// 基于準(zhǔn)備好的dom,初始化echarts實例
var myChart = echarts.init(main.value);
// 指定圖表的配置項和數(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: ['國家類型','非國家類型','個人','法人','可公式','非公式']
},
xAxis: {
type: 'category',
data: ['國家類型','非國家類型','個人','法人','可公式','非公式']
},
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'
}
]
};
// 使用剛指定的配置項和數(shù)據(jù)顯示圖表。
myChart.setOption(option);
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue2.x中的provide和inject用法小結(jié)
這篇文章主要介紹了vue2.x中的provide和inject用法小結(jié),本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-12-12
Vue中狀態(tài)管理器(vuex)詳解以及實際應(yīng)用場景
Vuex是一個專為Vue.js應(yīng)用程序開發(fā)的狀態(tài)管理模式,下面這篇文章主要給大家介紹了關(guān)于Vue中狀態(tài)管理器(vuex)詳解以及實際應(yīng)用場景的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-11-11
解決vue的變量在settimeout內(nèi)部效果失效的問題
今天小編就為大家分享一篇解決vue的變量在settimeout內(nèi)部效果失效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Vue單頁面應(yīng)用中實現(xiàn)Markdown渲染
這篇文章主要介紹了Vue單頁面應(yīng)用中如何實現(xiàn)Markdown渲染,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2021-02-02

