Vue 中循环渲染多个相同 echarts 图表,并传入不同的数据

在开发过程中我们常常需要,在一个页面中使用相同的图表来展示同级别的多个事物(如:同级别的多个不同id的仓库、同级别的多个不同id的设备等)。

例如:


<template>
  <div class="projectCost">
    <div class='overviewModel'> 
      <div class='one'  v-for="(item,index) in levelNameList" :key="index">
        <div class="roseChart" :id='forId(index)'></div> 
        <p class='title'>{{item.levelName}}</p>
        <div class='list'>
            <echarts-list 
            v-for="(el, i) in item.boilers"
            :key="i"
            :color="colorList[i]"
            :data="el"></echarts-list>
        </div>
      </div>
    </div>
  </div>
</template>



<script>
import EchartsList from './EchartList'
import { echartsFont } from "@/utils/util"; 
export default {
  props:{
    levelNameList:{   //父组件传递过来的数据
      type: Array,
      default: () => {return []}
    }
  },
  components: {EchartsList},
  data(){
    return {
        colorList: [
            "#0a9aea",
            "#FFC100",
            "#e50ec2",
            "#4fdd14",
            "#0780ed",
        ],
        getId: [],
    }
  },
  mounted(){
    //每一个echarts都做到自适应
    window.addEventListener("resize", () => {
        this.getId && this.getId.forEach((item,index) => {
            this.getId[index].resize()
        });
    });
  },
  methods:{
    forId(index) {
      return 'roseChart' + index
    },
    drawRose(){
      this.$nextTick(function(){
        for(var i = 0;i < this.levelNameList.length;i++ ){ 
          this.getId.push(echarts.init(document.getElementById('roseChart'+i)));
          this.getId[i].setOption({
              title: {
                            //这个地方是计算总数的
                  text: "{a|" + this.levelNameList[i].boilers.reduce((pre, cur) => {return pre + cur.value*1},0) + "}\n{c|" + "总数" + "}",
                  x: "center",
                  y: "center",
                  textStyle: {
                    rich: {
                      a: {
                          fontSize: echartsFont(0.25),
                          color: "#333",
                          fontWeight: "600",
                          lineHeight: 40,
                      },
                      c: {
                          fontSize: echartsFont(0.14),
                          color: "#666",
                          padding: [5, 0],
                      },
                    },
                  },
              },
              tooltip: {
                  trigger: 'item',
                  show: false,
              },
              legend: {
                  top: '5%',
                  left: 'center'
              },
              color: this.colorList,
              series: [
                {
                  name: '最外层细圆环',
                  type: 'pie',
                  radius: ['70%', '97%'],
                  center: ["50%", "50%"],
                  avoidLabelOverlap: false,
                  silent: true,
                  hoverAnimation: false,
                  label: {
                      show: false,
                  },
                  labelLine: {
                      show: false
                  },
                    //展示要渲染的数据,渲染不同的内容
                  data: this.levelNameList[i].boilers.map(item => item.value)
                }
              ]
          })
        }
      })
    }
  },
  watch: {
    levelNameList:{
      immediate: true,
      deep: true,
      handler(value) {
        if(!value) return
        this.getId = []
        console.log('监听levelNameList的变化',value);
        this.drawRose()
      }
    }
  }
}

</script>

<style lang="less" scoped>
.projectCost{
    width: 100%;
    height: 100%;
  .overviewModel{
    width: 100%;
    overflow-y: auto;
    height: 100%;
    padding-right: 13px;
    .one{
      position: relative;
      width: 100%;
      border-bottom: 2px solid #e9e9e9;
      .roseChart{
        width: 35%;
        margin-top: 1.25vw;
        height: 14vw !important;
      }
    }
    .one:last-child{
      border: none;
    }
    .title{
      position: absolute;
      font-size: 0.926vw;
      font-weight: 500;
      top: 0;
      left: 0;
      color: #333333;
      width: 100%;
      height: 30px;
      z-index: 1;
    }
    .list{
      z-index: 2;
      position: absolute;
      top: 0.925vw;
      width: calc(65% - 30px);
      right: 20px;
      box-sizing: border-box;
    }
  }
}
</style>

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值