关于Echarts图表复用解决id唯一性

文章介绍了在使用Echarts进行可视化大屏开发时遇到的复用问题,即因为ID重复导致的渲染异常。为解决这个问题,作者提出了一种方案:通过生成唯一的id(如uuid或随机数)来避免ID冲突,确保每个组件实例的独立性。在Vue组件中,作者展示了如何封装Echarts子组件,以及如何在父组件中传递动态id和配置选项,从而实现正确渲染和更新图表。
摘要由CSDN通过智能技术生成

        目前市面上许多项目都会接触到可视化大屏的开发,用得最多的三方组件库Echarts,在进行封装后复用许多同学会发现页面渲染异常问题,只会渲染第一次复用的内容.究其原因在于我们通过Id获取了页面节点进行渲染造成了id的重复.网上也有许多的解决方案.在此,给大家分享一个自己测试后的可行的实施方案.我们可以通过uuid生成不同的id向子组件传值指定唯一性,也可以通过随机数的方式.具体的操作我们接着往下看.

        首先我们需要将需要复用的组件进行简单的封装.  commonEcharts.vue(子组件)

<template>
  <div :id="id"></div>
</template>

<script setup>
import { number } from 'echarts';
import { ref, inject, onMounted, onUnmounted, defineProps } from 'vue'
import useDraw from '/src/util/useDraw.js'
const { appRef, calcRate, windowDraw, unWindowDraw } = useDraw()
const echarts = inject('$echarts')
let { id, option } = defineProps({
  option: Object,
  id: String
})
let tuChart = () => {
  let eg = document.getElementById(id)
  eg.removeAttribute('_echarts_instance_')
  const myChart = echarts.init(eg)

  myChart.setOption(option)

  window.addEventListener("resize", function () {
    myChart.resize()
  })
}

onMounted(() => {
  windowDraw()
  calcRate()
  tuChart()
})
onUnmounted(() => {
  unWindowDraw()
})
</script>

<style lang="scss" scoped>
#tu {
  width: 615px;
  height: 300px;
  border: 1px solid gray;
  margin-top: 15px;
}
</style>

 然后在需要使用的组件中父组件()引入对应的组件.expControl.vue

import charts from '../../../components/copycompnents/commonEcharts.vue'

接下来解决id复用的问题,新建一个js或ts文件,对方法进行封装并导出引入.

新建createID.js文件

export function createId(){
  return 'id' +Math.random()
}

引入js文件

import { createId } from '../../../api/createId'

在父组件中传入配置项option,这儿一折线图为例

let lineTwo = reactive({
  color: ['#0298DB', '#F59A23'],
  title: {
    text: '出院人次数',
    left: 80,
    top: 20,
    textStyle: {
      fontWeight: 400,
      fontFamily: 'Arial Normal',
      fontStyle: 'normal',
      fontSize: 20,
      color: '#555555'
    }
  },
  grid: {
    top: 70,
    bottom: 40,
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross',
      crossStyle: {
        color: '#999'
      }
    }
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
  },

  yAxis: {
    type: 'value',
    show: true,
    min: 0,
    max: 5000,
    iterval: 1000,
    axisLine: {
      show: true
    },
    axisLabel: {
      formatter: function (value, index) {
        return value >= 1000 ? parseInt(value / 1000) + '千' : value;
      }
    },
    axisPointer: {
      type: 'shadow'
    }
  },

  legend: {
    // itemStyle: {},  //继承系列中属性值的颜色
    right: 80,
    top: 15,
    itemGap: 20,
    itemWidth: 9,
    itemHeight: 10,
    data: [
      { icon: 'rect', name: '今年' },
      { icon: 'rect', name: '去年' }
    ],
    textStyle: {
      fontWeight: 400,
      fontFamily: 'Arial Normal',
      fontStyle: 'normal',
      fontSize: 20,
      color: '#555555',
    }
  },

  series: [
    {
      name: '今年',
      symbol: 'none',
      data: [1500, 2300, 2240, 2180, 1350, 1470, 2600, 1110, 1230, 1450, 1680, 2360],
      type: 'line'
    },
    {
      name: '去年',
      symbol: 'none',
      data: [1000, 1300, 2504, 3080, 1050, 1740, 2200, 2120, 1230, 1550, 1460, 1530],
      type: 'line'
    }
  ]
})

在父组件中传值

     <div class="atlas flex">
          <charts :option="lineOne" :id="createId()" />
     </div>

子组件通过difineprops接收父组件传过来的值

<template>
  <div :id="id"></div>
</template>
let { id, option } = defineProps({
  option: Object,
  id: String
})

效果图:

个人经验.分享一下.共同学习,欢迎指正. 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小叶coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值