一、效果
二、代码展示
<template>
<div ref="main" style="width: 100%; height: 400px"></div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from "vue";
import * as echarts from "echarts"; // 按需引入 echarts
const main = ref(); // 使用ref创建虚拟DOM引用,使用时用main.value
onMounted(() => {
init();
});
function init() {
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(main.value);
var datas = [
[
{ name: "正常", value: 32 },
{ name: "异常", value: 1 },
],
];
// 指定图表的配置项和数据
var option = {
title: {
text: "{a|33}{c|%}", //主标题文本
subtext: "健康检测", //副标题文本
left: "center",
top: "18%",
textStyle: {
rich: {
a: {
fontSize: 24,
color: "#fff",
},
c: {
fontSize: 14,
color: "#ffffff",
padding: [5,0,0,0]
},
},
},
itemGap: 5,
subtextStyle: {
fontFamily: "微软雅黑",
fontSize: 12,
color: "#fff",
},
},
series: datas.map(function (data) {
return {
type: "pie",
radius: [35, 70],
top: "-10%", //距离顶部的距离(可使用百分比%)
left: "0px", //距离左侧的距离
right: "0px", //距离右侧的距离
bottom: "0px", //距离底部的距离
itemStyle: {
borderColor: "#fff",
borderWidth: 0,
normal: {
// 渐变色
color: (list) => {
var colorList = [
{
colorStart: "#1984f1",
colorEnd: "#28f9ca",
},
{
colorStart: "#FF4923",
colorEnd: "#FFBC4A",
},
];
return new echarts.graphic.LinearGradient(1, 0, 0, 0, [
{
//左、下、右、上
offset: 0,
color: colorList[list.dataIndex]["colorStart"],
},
{
offset: 1,
color: colorList[list.dataIndex]["colorEnd"],
},
]);
},
},
},
label: {
alignTo: "edge",
formatter: "{name|{b}}\n{time|{c} 人}",
minMargin: 5,
edgeDistance: 10,
lineHeight: 18,
rich: {
time: {
fontSize: 12,
color: "#fff",
},
name: {
fontSize: 12,
color: "#fff",
},
},
},
labelLine: {
length: 15,
length2: 0,
maxSurfaceAngle: 80,
},
labelLayout: function (params: any) {
const isLeft = params.labelRect.x < myChart.getWidth() / 2;
const points = params.labelLinePoints;
// Update the end point.
points[2][0] = isLeft
? params.labelRect.x
: params.labelRect.x + params.labelRect.width;
return {
labelLinePoints: points,
};
},
data: data,
};
}),
};
myChart.setOption(option);
// 使用刚指定的配置项和数据显示图表。
window.onresize = function () {
// 自适应大小
myChart.resize();
};
}
</script>
<style scoped></style>