vue动态生成多个Echarts图表
首先先动态的获取到id
<div v-for="(item,index) in chartList" :key="index">
<div :id="`chart${index}`">div>
div>
其次获取后端返回的数据
this.chartList = res.data.data.chartList;
if (this.chartList.length > 0) {
this.$nextTick(() => {
this.initChart()
})
}
生成echarts图表
initChart() {
this.chartList.forEach((val, index) => {
const myChart = this.$echarts.init( document.getElementById(`chart${index}`))
this.chartList[index] = {
title: {top:"3%",position:'center',left:'60%',text: this.chartList[index].key,textStyle:{fontSize:'20',textBorderType: [5, 10],textBorderDashOffset: 5,textBorderType: 'solid',color:'#5b9bd5'}},
xAxis: {type: 'category',data: ['在线', '排队','小休', '示忙','空闲','通话','整理'],nameLocation:'end',splitArea:{show:false},splitLine:{show: false}},
yAxis: {type: 'value',splitArea:{show:false},splitLine:{show: false}},
series: [{data:this.chartList[index].list,type: 'bar',barWidth: '20%',color:'#5b9bd5',label:{show:true,position:'top'},
itemStyle: {
normal:{
color: function (params){
var colorList = ['#5b9bd5','red','orange','yellow','green','blue','purple'];
return colorList[params.dataIndex];
}
},}}],
grid:{left:"10%",right:"0%",top:"15%"}
}
myChart.setOption(this.chartList[index])
})
}
},