实现效果如图
老规矩先创建占位子的div
<div id="pieReport" style="width: 400px;height: 300px;"></div>
然后引入Echarts,并且模拟数据
export default {
name: "",
data() {
return {
charts: "",
opinion: ["及格人数", "未及格人数"],
opinionData: [
{ value: 12, name: "及格人数", itemStyle: "#1ab394" },
{ value: 18, name: "未及格人数", itemStyle: "#79d2c0" }
]
};
},
}
在methods中定义方法
drawPie(id) {
this.charts = this.$echarts.init(document.getElementById(id));
this.charts.setOption({
tooltip: {
trigger: "item",
formatter: "{a}<br/>{b}:{c} ({d}%)"
},
legend: {
bottom: 10,
left: "center",
data: this.opinion
},
series: [
{
name: "状态",
type: "pie",
radius: "65%",
center: ["50%", "50%"],
avoidLabelOverlap: false,
itemStyle: {
emphasis: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: "rgba(0, 0, 0, 0.5)"
},
color: function(params) {
//自定义颜色
var colorList = ["#1ab394", "#79d2c0"];
return colorList[params.dataIndex];
}
},
data: this.opinionData
}
]
});
},
在mounted中调用
mounted() {
this.$nextTick(function() {
this.drawPie("pieReport");
});
}
懒得写了注释了,将就看吧