vue动态生成多个Echarts图表

4 篇文章 0 订阅
4 篇文章 0 订阅

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:{  
            //每个柱子的颜色即为colorList数组里的每一项,如果柱子数目多于colorList的长度,则柱子颜色循环使用该数组
                    color: function (params){
                        var colorList = ['#5b9bd5','red','orange','yellow','green','blue','purple'];
                        return colorList[params.dataIndex];
                    }
                },}}],
            grid:{left:"10%",right:"0%",top:"15%"}
            }
      //注意this.chartList[index]这是我们后台拼好数据直接set就行了
      myChart.setOption(this.chartList[index])
    })
  }
},
  • 3
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值