html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>自定义速度仪表盘</title>
<!-- 引入 ECharts -->
<script src="https://cdn.staticfile.org/echarts/5.4.0/echarts.min.js"></script>
</head>
<body>
<div id="main" style="width: 600px;height:400px;"></div>
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('main'));
var option = {
tooltip: {
formatter: '{a} <br/>{b} : {c}km/h'
},
series: [{
name: '速度',
type: 'gauge',
progress: {
show: true,
width: 18,
itemStyle: {
color: '#4dabf7'
}
},
axisLine: {
lineStyle: {
width: 18,
color: [
[0.3, '#4dabf7'], // 0-60
[0.7, '#69db7c'], // 60-140
[1, '#ff6b6b'] // 140-200
]
}
},
axisTick: {
length: 10,
lineStyle: {
color: 'auto',
width: 2
},
// 自定义刻度宽度
interval: function (value, index) {
if (value <= 60) {
return true; // 0-60 每个刻度显示
} else if (value <= 140) {
return (index % 5 === 0); // 60-140 每5个刻度显示一个
} else {
return (index % 10 === 0); // 140-200 每10个刻度显示一个
}
}
},
splitLine: {
length: 20,
lineStyle: {
color: 'auto',
width: 4
},
// 自定义分割线宽度
interval: function (value, index) {
if (value <= 60) {
return true;
} else if (value <= 140) {
return (index % 5 === 0);
} else {
return (index % 10 === 0);
}
}
},
axisLabel: {
formatter: function (value) {
if (value <= 60) {
return value + ' km/h\n低速';
} else if (value <= 140) {
return value + ' km/h\n巡航';
} else {
return value + ' km/h\n高速';
}
},
distance: 40,
fontSize: 16,
color: function (value) {
if (value <= 60) {
return '#4dabf7';
} else if (value <= 140) {
return '#69db7c';
} else {
return '#ff6b6b';
}
}
},
anchor: {
show: true,
size: 20,
itemStyle: {
borderColor: '#4dabf7',
borderWidth: 2
}
},
title: {
show: true,
offsetCenter: [0, '70%'],
color: '#868e96',
fontSize: 20
},
detail: {
valueAnimation: true,
formatter: '{value} km/h',
color: 'auto',
fontSize: 40,
offsetCenter: [0, '40%']
},
data: [{
value: 120,
name: '当前速度'
}],
min: 0,
max: 200,
startAngle: 200,
endAngle: -20
}]
};
myChart.setOption(option);
</script>
</body>
</html>