
在 Vue3 中使用 echarts
Vue 3 中管理 ECharts 生命周期 先确保容器有宽高,再在挂载后初始化;数据变化时更新配置,尺寸变化时调用 resize,卸载时 dispose。实例保存在普通变量里,不需要被 Vue 深度代理。 下面示例面向 Vue 3 与 ECharts 5/6 的常用公开 API;项目仍需锁定具体版本并运行集成测试。
Vue 3 中管理 ECharts 生命周期
先确保容器有宽高,再在挂载后初始化;数据变化时更新配置,尺寸变化时调用 resize,卸载时 dispose。实例保存在普通变量里,不需要被 Vue 深度代理。
下面示例面向 Vue 3 与 ECharts 5/6 的常用公开 API;项目仍需锁定具体版本并运行集成测试。
<script setup>
import { ref, onMounted, onBeforeUnmount, watch } from 'vue';
import * as echarts from 'echarts';
const props = defineProps({ option: { type: Object, required: true } });
const host = ref(null);
let chart;
let observer;
onMounted(() => {
chart = echarts.init(host.value);
chart.setOption(props.option, { notMerge: true });
observer = new ResizeObserver(() => chart?.resize());
observer.observe(host.value);
});
watch(() => props.option, (option) => {
chart?.setOption(option, { notMerge: true });
}, { deep: true, flush: 'post' });
onBeforeUnmount(() => {
observer?.disconnect();
chart?.dispose();
chart = undefined;
});
</script>
<template><div ref="host" class="chart"></div></template>
<style scoped>.chart { width: 100%; height: 20rem; }</style>
notMerge: true 适合完整 option 的替换,避免旧系列残留;需要增量合并时应为系列设置稳定 id 并按实际规则使用 replaceMerge。高频更新不一定适合深度 watch,可让父组件提交明确的新配置或版本号。
保留水球图实现细节
旧文使用 Vue 3.2.6、ECharts 5.3.3 与 echarts-liquidfill 3.1.0。原来的标题、波纹配色、graphic、边框和层级配置保留在下方历史案例里;这些版本是当时记录,不能把安装新版本等同于兼容性验证。
水球图是扩展系列,需要先导入插件:
import * as echarts from 'echarts';
import 'echarts-liquidfill';
const option = {
series: [{ type: 'liquidFill', data: [0.6, 0.5, 0.4], radius: '65%' }],
};
插件版本需与 ECharts 配套,不应直接假定第三方扩展已经支持所有新主版本。实际业务可以把该 option 交给上面的生命周期组件。
参考:ECharts 按需引入、容器与尺寸、liquidfill 源码。2026-11-11 修订。
旧水球图配置存档
下方保留当时的配色、边框与封装,生命周期问题以上方修订组件为准。不要直接复制旧版组件当作新版完整示例。
在 Vue3 中使用 echarts
地址
vue: 3.2.6 版本
echarts: 5.3.3 版本
echarts-liquidfill: 3.1.0 版本(水球图,个人觉得非常不错)
### 用水球图作为使用案例
<template>
<div ref="echartsRef" class="water-polo-echart"></div>
</template>
<script>
import * as echarts from 'echarts';
import 'echarts-liquidfill';
import { onMounted, onUnmounted, shallowRef } from 'vue';
export default {
props: {
data: Object // 后续新要求自己添加参数修改组件,目前只考虑常用
},
setup(props) {
/**
* shallowRef 能够创建一个跟踪自身 .value 变化的 ref,但不会使其值也变成响应式的
* 使用ref时,框架对属性的代理,会影响到ECharts实例底层的运行,所以建议使用shallowRef
* github参考地址:https://github.com/apache/echarts/issues/14974#issuecomment-842796847
*/
const echartsRef = shallowRef();
const myChart = shallowRef();
onMounted(() => {
myChart.value = echarts.init(echartsRef.value);
myChart.value.setOption({
title: {
text: props.data.title,
textStyle: {
align: 'center',
color: '#fff',
fontSize: 20
},
top: '5%',
left: 'center',
subtext: props.data.subtitle
},
backgroundColor: '#0F224C',
graphic: [{
type: 'text',
left: 'center',
bottom: '5%',
style: {
fill: '#6e7079',
text: props.data.graphicText
}
}],
series: [
{
type: 'liquidFill',
radius: '65%',
center: ['50%', '55%'],
color: [
{
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{
offset: 0,
color: '#446bf5'
},
{
offset: 1,
color: '#2ca3e2'
}
],
globalCoord: false
}
],
data: props.data.value, // data个数代表波浪数
backgroundStyle: {
borderWidth: 1,
color: 'RGBA(51, 66, 127, 0.7)'
},
label: {
normal: {
textStyle: {
fontSize: 50,
color: '#fff'
}
}
},
outline: {
show: false,
borderDistance: 10,
itemStyle: {
borderWidth: 2,
borderColor: '#112165'
}
}
}
]
});
});
onUnmounted(() => {
myChart.value.dispose();
});
const resize = () => {
myChart.value.resize();
};
return {
resize,
echartsRef
};
}
};
</script>
<style lang="less" scoped>
.water-polo-echart {
flex: 1;
min-width: 400px;
height: 400px;
}
</style>