有数据时:
没数据时:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Echarts图表没数据的时候,用图片代替暂无数据</title>
<!-- 引入 echarts.js -->
<script src="js/echarts.min.js"></script>
<script src="js/jquery.min.js"></script>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main" style="width: 600px;height:400px;"></div>
<script type="text/javascript">
var obj = {
aoiBadList: [
{ AOI_NO: "1H", AOIBAD_NUM: "250", AOIBAD_RATE: "50" },
{ AOI_NO: "2H", AOIBAD_NUM: "200", AOIBAD_RATE: "70" },
{ AOI_NO: "3H", AOIBAD_NUM: "160", AOIBAD_RATE: "85" },
{ AOI_NO: "4H", AOIBAD_NUM: "120", AOIBAD_RATE: "97" },
],
}
var aoiBad;
aoiBad = obj.aoiBadList;
//aoiBad = [];
var aoiNo = aoiBad.map(x => { return x.AOI_NO });
var aoiBadNum = aoiBad.map(x => { return x.AOIBAD_NUM });
var aoiBadRate = aoiBad.map(x => { return x.AOIBAD_RATE });
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
var option;
if (aoiBad.length == '0' || aoiBad.length == 0) {
// option = {
// title: {
// text: '暂无数据',
// x: 'center',
// y: 'center',
// textStyle: {
// color: '#65ABE7',
// fontWeight: 'normal',
// fontSize: 16
// }
// }
// }
option = {};
let elementImg='http://img.mp.itc.cn/upload/20170724/cf678e09eb384401aa616ba134126357_th.jpg';
//图表的容器
$('#main').css({'width':600+'px','height':400+'px','border':'1px solid #ccc','display':'flex','justify-content':'center','align-items':'center'});
//创建标签
let mainImg = document.createElement("img");
main.appendChild(mainImg);
$('#main img').css({'width':'auto','height':'auto','max-width':'100%','max-height':'100%'});
//加上src属性
$("#main img").attr("src",""+elementImg+"");
$('#main div').remove();
} else {
option = {
"grid": {
"top": "15%",
"bottom": "15%",
"right": "15%",
"left": "12%"
},
tooltip: {
trigger: 'axis',
formatter: function (param) {
let str = "";
let dada = 0;
for (let i = param.length - 1; i >= 0; i--) {
if (param[i].seriesName === 'AOI不良率') {
param[i].data = param[i].data + "%";
}
str = str + param[i].marker + param[i].seriesName + " : " + param[i].data + '<br>';
}
return str
},
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985'
}
}
},
xAxis: {
//data: ["1H", "2H", "3H", "4H", "5H", "6H","7H"],
data: aoiNo,
axisLine: { show: false },
axisTick: {
show: false
},
axisLabel: { //X轴标签
show: true,
textStyle: {
color: '#65ABE7',//字体颜色
fontSize: 12 //字体大小
}
}
},
yAxis: [
{
type: 'value',
name: '(数量)',
nameTextStyle: {
color: "#65ABE7",
fontSize: 12,
},
min: 0,
max: 500,
interval: 100,
axisLabel: {
textStyle: {
color: '#65ABE7',//字体颜色
fontSize: 12 //字体大小
}
},
axisTick: { show: false },
axisLine: { show: false }
},
{
type: 'value',
name: '(周转率%)',
nameTextStyle: {
color: "#65ABE7",
fontSize: 12,
},
min: 0,
max: 100,
interval: 20,
lineStyle: {
color: '#65ABE7',
},
axisLabel: {
formatter: '{value} %',
textStyle: {
color: '#65ABE7',//字体颜色
fontSize: 12 //字体大小
}
},
axisTick: { show: false },
axisLine: { show: false },
splitLine: { //Y轴分隔符
show: true,
lineStyle: {
color: '#65ABE7',
}
}
},
],
series: [
{
name: 'AOI不良数量',
type: 'bar',
stack: '总量',
//data: [250, 200, 180, 150, 120, 80,60],
data: aoiBadNum,
itemStyle: {
color: new echarts.graphic.LinearGradient(
0, 0, 0, 1,
[
{ offset: 0, color: '#07eff2' },
{ offset: 1, color: '#093553' }
]
)
}
},
{
yAxisIndex: 1,//根据右边轴显示
name: 'AOI不良率',
smooth: true,
type: 'line',
//data: [50, 60, 70, 77,85, 90,97],
data: aoiBadRate,
itemStyle: {
normal: {
color: '#5b7fee',
label: {
show: false, //开启显示
formatter: '{c}%',
position: 'bottom', //在下方显示
textStyle: { //数值样式
color: '#65ABE7',
fontSize: 16
}
}
}
}
}
]
};
}
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
</script>
</body>
</html>