html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>智能速度仪表盘</title>
<script src="https://cdn.staticfile.org/echarts/5.4.0/echarts.min.js"></script>
</head>
<body>
<div id="main" style="width: 600px;height:500px;"></div>
<script>
var myChart = echarts.init(document.getElementById('main'));
// 自定义刻度生成函数
function intervalFunc(interval) {
return function(val) {
return val <= 60 ? (val % 10 === 0) : (val % 20 === 0);
};
}
var option = {
series: [{
type: 'gauge',
min: 0,
max: 200,
center: ['50%', '55%'],
radius: '90%',
splitNumber: 10,
// 进度条颜色分段
axisLine: {
lineStyle: {
width: 20,
color: [
[0.3, '#4dabf7'], // 0-60
[0.6, '#69db7c'], // 60-120
[1, '#ff6b6b'] // 120-200
]
}
},
// 刻度线配置
axisTick: {
length: 8,
lineStyle: {
color: '#666',
width: 1
},
interval: intervalFunc(10)
},
// 分割线配置
splitLine: {
length: 15,
distance: 0,
lineStyle: {
color: '#666',
width: 2
},
interval: intervalFunc(10)
},
// 刻度标签
axisLabel: {
distance: 25,
color: '#666',
fontSize: 12,
formatter: function(value) {
if (value <= 60) {
return value % 10 === 0 ? value : '';
} else {
return value % 20 === 0 ? value : '';
}
}
},
// 指针配置(动态颜色)
pointer: {
length: '60%',
width: 6,
itemStyle: {
color: function(param) {
if (param.value <= 60) {
return '#4dabf7'; // 0-60 蓝色
} else if (param.value <= 120) {
return '#69db7c'; // 60-120 绿色
} else {
return '#ff6b6b'; // 120+ 红色
}
}
}
},
// 中心文字
detail: {
fontSize: 30,
offsetCenter: [0, '30%'],
formatter: '{value} km/h',
color: 'auto'
},
// 警告图标(自定义系列)
markLine: {
symbol: 'circle', // 图标形状
symbolSize: 20, // 图标大小
itemStyle: {
color: 'red', // 图标颜色
borderColor: 'red'
},
data: [{
type: 'max',
name: '',
value: 120, // 警告阈值
label: {
formatter: '',
position: 'end',
color: 'red'
}
}]
},
data: [{
value: 75,
name: '当前速度'
}]
}]
};
// 动态更新指针颜色和警告图标
function updateChart(value) {
myChart.setOption({
series: [{
data: [{ value: value, name: '当前速度' }],
markLine: {
data: [{
value: value >= 120 ? value : 120, // 动态调整警告图标位置
label: {
show: value >= 120, // 动态显示/隐藏警告文本
formatter: value >= 120 ? '警告:超速' : ''
}
}]
}
}]
});
}
// 初始设置
myChart.setOption(option);
// 示例:模拟速度变化(可替换为实际数据更新逻辑)
let speed = 75;
setInterval(function() {
speed = (speed + 5) % 250; // 模拟速度变化
updateChart(speed);
}, 1000);
</script>
</body>
</html>