vue实现饼状图
vue3 使用 ECharts 实现饼状图
安装 ECharts 依赖
npm install echarts --save
在组件中引入 ECharts
import * as echarts from 'echarts';
import { onMounted } from 'vue';
准备 DOM 容器
<div id="pieChart" style="width: 600px; height: 400px;"></div>
初始化图表
const initPieChart = () => {
const chartDom = document.getElementById('pieChart');
const myChart = echarts.init(chartDom);
const option = {
title: {
text: '示例饼图',
subtext: '数据展示',
left: 'center'
},
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b}: {c} ({d}%)'
},
legend: {
orient: 'vertical',
left: 'left',
data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
},
series: [
{
name: '访问来源',
type: 'pie',
radius: '50%',
data: [
{ value: 335, name: '直接访问' },
{ value: 310, name: '邮件营销' },
{ value: 234, name: '联盟广告' },
{ value: 135, name: '视频广告' },
{ value: 1548, name: '搜索引擎' }
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
myChart.setOption(option);
};
onMounted(() => {
initPieChart();
});
vue2 使用 vue-echarts 实现饼状图
安装 vue-echarts

npm install echarts vue-echarts
全局注册组件
import Vue from 'vue';
import ECharts from 'vue-echarts/components/ECharts.vue';
import 'echarts/lib/chart/pie';
Vue.component('v-chart', ECharts);
组件中使用
<template>
<v-chart :options="pieOptions" autoresize />
</template>
<script>
export default {
data() {
return {
pieOptions: {
title: {
text: '销售统计',
left: 'center'
},
tooltip: {
trigger: 'item'
},
series: [
{
type: 'pie',
radius: '55%',
data: [
{ value: 235, name: '视频广告' },
{ value: 274, name: '搜索引擎' },
{ value: 310, name: '邮件营销' }
]
}
]
}
}
}
}
</script>
<style scoped>
.echarts {
width: 100%;
height: 400px;
}
</style>
自定义饼图样式
设置环形饼图

series: [{
type: 'pie',
radius: ['40%', '70%'],
avoidLabelOverlap: false,
label: {
show: false,
position: 'center'
},
emphasis: {
label: {
show: true,
fontSize: '18',
fontWeight: 'bold'
}
},
data: [...]
}]
添加动画效果
animationType: 'scale',
animationEasing: 'elasticOut',
animationDelay: function (idx) {
return Math.random() * 200;
}
响应式处理
监听窗口变化
window.addEventListener('resize', function() {
myChart.resize();
});
使用 vue-echarts 时自动响应
<v-chart :options="pieOptions" autoresize />
数据动态更新
使用 watch 监听数据变化
watch: {
chartData: {
handler(newVal) {
this.updateChart(newVal);
},
deep: true
}
},
methods: {
updateChart(data) {
this.pieOptions.series[0].data = data;
}
}






