效果要求如下:
代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="echarts.js"></script>
<style>
#main {
width: 700px;
height: 300px;
}
</style>
</head>
<body>
<div id="main"></div>
<script>
var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
var dataList = [1850, 1200, 1500, 1600, 1900, 1300, 1250, 1500, 1600, 1900];
var max = Math.max.apply(null,dataList) + 200;
var min = Math.min.apply(null,dataList) - 400;
var dataListCopy = Array.from({length:dataList.length}).map(item=>{
return max;
})
var xCategory = ['低保救助', '重灾救助', '临时救助', '医疗救助', '住房救助', '教育救助', '就业救助', '特困救助', '社会里救助', '一站式救助'];
var endValue = dataList.length>10 ? 22 : 100;
var option = {
backgroundColor: '#111c22',
xAxis: {
type: 'category',
data: xCategory,
axisTick: {show: false},
axisLabel: {
show: true,
interval: 0,
color: 'rgba(255,255,255,0.7)',
formatter: function(value, index){
var arr = value.split("");
var str = '';
for (let index = 0; index < arr.length; index++) {
str += arr[index];
if(arr.length == 4 && str.length == 2) {
str += '\n';
} else if(arr.length == 5 && str.length == 3) {
str += '\n';
}
}
return str;
}
},
axisLine: {
show: true,
lineStyle: {
color: '#282e32'
}
},
},
yAxis: {
name: '单位:次',
nameTextStyle: {
align: "right",
padding: [0, -10, 0, 0]
},
nameGap: 30,
max: max,
interval: Math.ceil(max / 6),
min: min,
splitLine: {
show: true,
lineStyle: {
type: "dashed",
color: '#4f606a'
}
},
axisLabel: {
show: true,
color: 'rgba(255,255,255,0.7)'
},
},
dataZoom: {
start: 0,
end: endValue,
type: "inside",
zoomOnMouseWheel: false,
moveOnMouseMove: false,
zoomLock: false,
moveOnMouseWheel: true
},
series: [{
type: 'bar',
itemStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
offset: 0,
color: "rgba(50, 80, 90, 0.5)"
},{
offset: 1,
color: "rgba(19, 35, 50, 0.2)"
}], false)
}
},
silent: true,
barWidth: 35,
barGap: '-73%',
data: dataListCopy
}, {
type: 'bar',
barWidth: 15,
z: 10,
itemStyle: {
color: function(param){
if(param.dataIndex%2 == 0) {
return new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
offset: 0,
color: "rgba(6, 204, 255, 1)"
},{
offset: 1,
color: "rgba(191, 238, 254, 1)"
}], false)
} else {
return new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
offset: 0,
color: "rgba(254, 203, 78, 1)"
},{
offset: 1,
color: "rgba(255, 252, 236, 1)"
}], false)
}
}
},
data: dataList
// data: [45, 60, 13, 25,45, 60, 13, 25,45, 60, 13, 25,45, 60, 13, 25,45, 60, 13, 25,45, 60, 13, 25,45, 60, 13, 25,45, 60, 13, 25,45, 60, 13, 25,45, 60, 13, 25,45, 60, 13, 25, 45, 60, 13, 25]
}]
};
option && myChart.setOption(option);
</script>
</body>
</html>