下面我用**「从零到可运行」**的方式,教你用 ECharts 做仪表盘(Gauge),包含最常用场景和示例。
<script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
<div id="gauge" style="width: 400px;height:300px;"></div>
const chart = echarts.init(document.getElementById('gauge'));
const option = {
series: [
{
type: 'gauge',
min: 0,
max: 100,
data: [{ value: 72 }]
}
]
};
chart.setOption(option);
✅ 这样你就有一个最简单的仪表盘了。
series: [
{
type: 'gauge',
startAngle: 180,
endAngle: 0,
min: 0,
max: 100,
progress: {
show: true,
width: 18
},
axisLine: {
lineStyle: {
width: 18
}
},
data: [{ value: 65 }]
}
]
axisLine: {
lineStyle: {
width: 18,
color: [
[0.3, '#ff4d4f'],
[0.7, '#faad14'],
[1, '#52c41a']
]
}
}
detail: {
formatter: '{value} %',
fontSize: 24,
offsetCenter: [0, '70%']
},
title: {
offsetCenter: [0, '100%'],
fontSize: 14
},
data: [{ value: 88, name: '完成率' }]
setInterval(() => {
const value = Math.floor(Math.random() * 100);
chart.setOption({
series: [{ data: [{ value }] }]
});
}, 2000);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>ECharts 仪表盘</title>
<script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
</head>
<body>
<div id="gauge" style="width:400px;height:300px;"></div>
<script>
const chart = echarts.init(document.getElementById('gauge'));
chart.setOption({
series: [
{
type: 'gauge',
min: 0,
max: 100,
progress: { show: true, width: 20 },
axisLine: {
lineStyle: {
width: 20,
color: [
[0.3, '#ff4d4f'],
[0.7, '#faad14'],
[1, '#52c41a']
]
}
},
pointer: { width: 5 },
detail: {
formatter: '{value} %',
fontSize: 24,
offsetCenter: [0, '70%']
},
data: [{ value: 80, name: 'CPU使用率' }],
title: {
offsetCenter: [0, '100%']
}
}
]
});
</script>
</body>
</html>
如果你告诉我:
我可以直接给你对应版本代码。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。