Vue3 echarts 组件化以及使用 hook 进行resize

 hook 本质是一个函数,把setup函数中使用的 Composition API 进行了封装

在这里插入图片描述

封装echarts组件基底

<template>
	<div :style="{ height, width }"></div>
</template>

<script setup>
import * as echarts from 'echarts';
import useResize from '@/hooks/useResize'; // hook
const { proxy } = getCurrentInstance(); // 获取实例中的proxy

let echartInstance;

const props = defineProps({
	// 数据
	option: {
		type: Object,
		default: () => ({
			xAxis: {
				type: 'category',
				data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
			},
			yAxis: {
				type: 'value'
			},
			series: [
				{
					data: [150, 230, 224, 218, 135, 147, 260],
					type: 'line'
				}
			]
		})
	},
	// 高度
	height: {
		type: [Number, String],
		default: '300px'
	},
	// 宽度
	width: {
		type: [Number, String],
		default: '100%'
	}
});

const { option, height, width } = toRefs(props);

// 观察 option ,重新绘制
watch(
	option,
	(newValue) => {
		//	console.log(newValue)
		//	console.log(height.value)
		// resize();
		nextTick(() => {
			echartInstance.setOption(
				{
					...newValue,
					backgroundColor: 'rgba(0,0,0,0)'
				},
				true
			);
			resize();
		});
	},
	{
		deep: true
	}
);

watch(height, (newValue) => {
	//	console.log(newValue)
	resize();
});

watch(
	() => proxy.$store.state.settings.sideTheme.split('-')[1],
	(newValue) => {
		//	console.log(newValue)
		// 释放资源
		echartInstance.dispose();
		// 重新渲染图表
		echartInstance = echarts.init(proxy.$el, newValue, { renderer: 'svg' });
		echartInstance.setOption(
			{
				...option.value,
				backgroundColor: 'rgba(0,0,0,0)'
				// darkMode: newValue === "dark",
			},
			true
		);
	}
);

onMounted(() => {
	//	console.log(option.value)
	nextTick(() => {
		echartInstance = echarts.init(proxy.$el, proxy.$store.state.settings.sideTheme.split('-')[1], { renderer: 'svg' });
		echartInstance.setOption(option.value, true);
		echartInstance.on('click', (params) => {
			console.log(params)
			proxy.$emit('clicked', params);
		});
		// resize();
	});
});

onUnmounted(() => {
	// 释放资源
	echartInstance && echartInstance.dispose();
});

function resize() {
	echartInstance.resize();
}

// 暴露函数 (供hook调用)
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()
    }
}

二次封装调用

<template>
	<Echart :option="option" :height="props.height" @clicked="clicked" />
</template>

<script setup>
	import Echart from "@/components/Echarts/index" // 导入上面的echarts组件基底
	const {
		proxy
	} = getCurrentInstance(); // 获取实例中的proxy

	const props = defineProps({ // 数据
		data: {
			type: Object,
			default: () => ({
				max: 3000,
				value: 0,
				name: "name",
				show: false,
				unit: "",
				radius: "90%"
			})
		},
		title: {
			type: Object,
			default: () => ({
				text: null,
				left: 'center'
			})
		},
		legend: {
			type: Object,
			default: () => ({
				left: 'center',
				bottom: '10',
			})
		},
		// 高度
		height: {
			type: [Number, String],
			default: "300px",
		},
	});

	const option = computed(() => ({
		title: props.title,
		tooltip: {
			trigger: 'item',
			formatter: '{b} : {c} ({d}%)'
		},
		legend: props.legend,
		series: [{
			// name: null,
			type: 'pie',
			radius: '50%',
			data: props.data,
			emphasis: {
				itemStyle: {
					shadowBlur: 10,
					shadowOffsetX: 0,
					shadowColor: 'rgba(0, 0, 0, 0.5)'
				}
			}
		}]
	}));

	const handleGetColorByTheme = () => proxy.$store.state.settings.sideTheme.indexOf("dark") != -1 ? "#fff" : "#000"
	
	const clicked = (event)=>{
		proxy.$emit('clicked', event);
	}
</script>
```

# 最外层调用
```html
<PieChart :data="data" height="100%" @clicked="handlePieChartClicked" />

<script setup>
	import PieChart from '@/components/Echarts/pieChart/index';// 导入上面的二次封装pie组件
</script>
```


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3.2和Echarts 5.4都是非常流行的前端开发工具,下面我会分别给你介绍一下它们的组件特性。 Vue 3.2是Vue.js框架的一个版本,它引入了一些新的特性和改进。在Vue 3.2中,组件是一个非常重要的概念。Vue组件允许开发者将页面拆分成独立的、可复用的组件,每个组件都有自己的模板、样式和逻辑。这种组件的方式使得开发更加模块、可维护性高,并且可以提高代码的复用性。 在Vue 3.2中,可以使用`defineComponent`函数来定义一个组件组件可以包含模板、样式和逻辑,并且可以通过props来接收父组件传递的数据。你还可以使用`emit`函数来触发自定义事件,以便与父组件进行通信。另外,Vue 3.2还引入了`teleport`组件,可以方便地将组件渲染到DOM树的其他位置。 而Echarts 5.4是一个强大的数据可视库,它提供了丰富的图表类型和交互功能。在Echarts 5.4中,也支持组件的方式来使用图表。你可以将Echarts图表封装成一个独立的组件,然后在需要的地方引用这个组件。 在Echarts 5.4中,你可以使用`echarts`对象来创建和配置图表。你可以将图表的配置项作为props传递给Echarts组件,并在组件内部使用`echarts.init`方法来初始图表。你还可以通过监听事件来实现与图表的交互,比如点击、鼠标移动等。 总结一下,Vue 3.2和Echarts 5.4都支持组件的开发方式。Vue 3.2可以帮助你将页面拆分成独立的、可复用的组件,而Echarts 5.4可以帮助你将图表封装成独立的组件。这样的组件开发方式可以提高代码的可维护性和复用性,同时也方便了开发和维护大型应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值