效果图:
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Echars柱状图嵌套 title>
<style type="text/css">
body{
height: 100%;
width: 100%;
background-color: #0a3b76;
}
style>
head>
<body>
<div style="width: 100%;height: 400px;" id="echarts">div>
body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.3.2/echarts.min.js" type="text/javascript" charset="utf-8">script>
<script type="text/javascript">
echars()
function echars() {
let xAxis = ['08:00~09:00', '09:00~10:00', '10:00~11:00', '11:00~12:00', '12:00~13:00', '13:00~14:00',
'14:00~15:00', '15:00~16:00', '16:00~17:00', '17:00~18:00', '18:00~19:00',
'19:00~20:00'
]
var dl1 = [];
var dl2 = [];
var dl3 = [];
for (let item of xAxis) {
dl1.push(750 * 2)
dl2.push(750)
dl3.push(parseInt(Math.random() * 300 + 600))
}
var legend = ["小时目标数", "当前目标数", "实际产出"];
let color = [
['rgba(255,255,255,0.4)', '#00cbff', '#a5d6a7'],
['rgba(255,255,255,0.4)', '#00cbff', '#4caf50']
]
var icon = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAdBJREFUWEfFlz9Lw0AYh39vC0VRXBQcxMVBFCdRxMFeUgtFBIXmUnFQ1E0KLkrBQTDgIBRBEKT5EuIncPADCZqrlWqbkysESrG2zb9mDcnz3O/e996EMOSL4uBLy0qQZbl/sSIXqBb0LbgyN/70eh67gDDYNYgsAu5jFajts9nGD9kEbKtVxypQ4xnelK4NwpQXeWwCDtfKBJQ69zpygTrXF75bqyatS6FFVwMOZ0cEqgAY7dbSkSUgDO0RhGKvsyR0gc9CZqXpuqrKV3vBQ+8CYWhFEB77AYfaBfJYHxFCrZqOBoGHksCXwViDyAawOCg8sIDDtRIBZT/gQFvg5LOTlGzYkDCDwH0n8M435pJIqv7ODUXAgwqT3UDSVRCJwOeAw7UdAlQaM35EAgsoaHU3Oy1TjQok8oNKhCLgQR2uXxLk7SASoQoo8HuebSYTrQE0349I6AIKKg9zY6JWV/PgoJdEJAIe9MPQzhKEh/8kIhVQ4FohvdZ0E+qoXo79g6QdKDirAHTaKRF5Au3AqqmdSCltgFKBZkGvwvrvvuCZJcBVXZL2PQuCCHjPVk39Tkp5EesWdIoLzvYItB7rn1GnhBrtE88vb106JIyw/b/jF+LXtiFzcqxuAAAAAElFTkSuQmCC';
let myChart = echarts.getInstanceByDom(document.getElementById('echarts'));
if (myChart === undefined) {
myChart = echarts.init(document.getElementById('echarts'));
};
myChart.showLoading({
text: 'loading',
color: '#19ecff',
textColor: '#fff',
maskColor: 'rgba(58, 186, 255, 0.1)',
zlevel: 0,
});
var option = {
title: {
text: "产出",
textStyle: {
color: '#fff',
fontSize: 18
}
},
tooltip: {
trigger: 'axis',
backgroundColor: 'rgba(42, 47, 53, 0.9)',
borderColor: '#ffffff',
textStyle: {
color: "#fff"
},
axisPointer: {
lineStyle: {
color: '#fff'
}
},
formatter: function(params) {
let html = "";
html = params[0].name + '
';
for (let i = 0; i < params.length; i++) {
html +=
'+
params[i].color.colorStops[0].color + ';">'
html += params[i].seriesName + " : " + params[i].value + " PCS
";
}
return html;
},
},
legend: {
data: legend,
top: "0%",
x: 'center',
textStyle: {
"color": "rgba(255,255,255,0.9)"
}
},
grid: {
containLabel: true,
left: '3%',
right: '3%',
bottom: '4%',
top: '25%',
},
xAxis: [{
axisLabel: {
color: '#fff',
fontSize: 14,
interval: 0,
formatter: function(value) {
value = value.replace(/~/g, "");
var ret = "";
var max = 5;
var val = value.length;
var rowN = Math.ceil(val / max);
if (rowN > 1) {
for (var i = 0; i < rowN; i++) {
var temp = "";
var start = i * max;
var end = start + max;
temp = value.substring(start, end) + "\n";
ret += temp;
}
return ret;
}
return value
}
},
axisTick: {
show: true,
alignWithLabel: true,
},
splitLine: {
show: false,
},
axisLine: {
show: false
},
data: xAxis,
type: 'category',
},
{
type: 'category',
axisLine: {
show: false
},
axisTick: {
show: false
},
axisLabel: {
show: false
},
splitArea: {
show: false
},
splitLine: {
show: false
},
data: xAxis
},
{
type: 'category',
axisLine: {
show: false
},
axisTick: {
show: false
},
axisLabel: {
show: false
},
splitArea: {
show: false
},
splitLine: {
show: false
},
data: xAxis
},
{
type: 'category',
axisLine: {
show: false
},
axisTick: {
show: false
},
axisLabel: {
show: false
},
splitArea: {
show: false
},
splitLine: {
show: false
},
data: xAxis
}],
yAxis: {
type: "value",
name: "数量(PCS)",
axisLabel: {
"show": true,
formatter: function(param) {
let value = (param / 1000).toFixed(1);
return value;
}
},
axisTick: {
show: true,
alignWithLabel: true,
},
axisLine: {
show: false,
lineStyle: {
color: '#ffffff'
}
},
splitLine: {
show: false,
lineStyle: {
color: "#b0c1d0"
}
},
},
series: [
{
name: legend[0],
data: dl1,
type: 'bar',
barWidth: '40%',
barGap: 0,
xAxisIndex: 0,
itemStyle: {
normal: {
color: {
type: "linear",
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [{
offset: 0,
color: color[0][0]
},
{
offset: 1,
color: color[1][0]
}
],
globalCoord: false
}
}
},
label: {
normal: {
show: false,
position: "inside",
textStyle: {
color: "#fff",
fontSize: 10
}
}
},
},
{
name: legend[1],
data: dl2,
type: 'bar',
barWidth: '30%',
barGap: 0,
xAxisIndex: 1,
itemStyle: {
normal: {
color: {
type: "linear",
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [{
offset: 0,
color: color[0][1]
},
{
offset: 1,
color: color[1][1]
}
],
globalCoord: false
}
}
},
label: {
normal: {
show: false,
position: "inside",
textStyle: {
color: "#fff",
fontSize: 10
}
}
},
},
{
name: legend[2],
data: dl3,
type: 'bar',
barWidth: '20%',
barGap: 0,
xAxisIndex: 2,
itemStyle: {
normal: {
color: {
type: "linear",
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [{
offset: 0,
color: color[0][2]
},
{
offset: 1,
color: color[1][2]
}
],
globalCoord: false
}
}
},
label: {
normal: {
show: false,
position: "inside",
textStyle: {
color: "#fff",
fontSize: 10
}
}
},
},
{
name: legend[2],
data: dl3,
type: 'bar',
barWidth: '20%',
barGap: 0,
xAxisIndex: 3,
itemStyle: {
normal: {
color: {
type: "linear",
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [{
offset: 0,
color: color[0][2]
},
{
offset: 1,
color: color[1][2]
}
],
globalCoord: false
}
}
},
label: {
show: true,
position: ['120%',-11],
color: '#DB5E6A',
backgroundColor: {
image: icon
},
formatter: function(params) {return ""},
padding: [15, 15, 8, 5],
},
}
]
};
myChart.hideLoading();
myChart.clear();
myChart.setOption(option, true);
}
script>
html>