<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 引入echarts-->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
</head>
<body>
<div style="width: 500px; height: 500px" id="container"></div>
</body>
<script>
var dom = document.getElementById("container");
var myChart= echarts.init(dom);
var option = {};
option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [{
data: [120, 200, 150, 80, 70, 110, 130],
type: 'bar',
itemStyle: {
normal: {
color: function(params) {
//颜色列表长度需要设置大于柱的数量,颜色循环结束不会继续循环
var colorList = ['#c23531','#2f4554', '#61a0a8', '#d48265', '#91c7ae','#749f83', '#ca8622'];
return colorList[params.dataIndex]
}
}
}
}]
};
if (option&& typeof option === 'object') {
myChart.setOption(option);
}
</script>
</html>
在series中设置
itemStyle: {
normal: {
color: function(params) {
//颜色列表长度需要设置大于柱的数量,颜色循环结束不会继续循环
var colorList = ['#c23531','#2f4554', '#61a0a8', '#d48265', '#91c7ae','#749f83', '#ca8622'];
return colorList[params.dataIndex]
}
}
}