vue中echart封装组件

echart封装组件

最近有个需求页面,有很多echart图表一个一个写太麻烦,自己琢磨+借鉴大佬的代码封装了一个

echart组件代码

<template>
    <div :id=id :data=data></div>
</template>
<script>
export default {
  props: ['id', 'data'],
  data () {
    return {
      ChartLineGraph: null
    }
  },
  watch: {
    data: {
      handler (newValue, oldValue) {
        this.drawLineGraph(this.id, newValue)
      },
      deep: true
    }
  },
  mounted () {
    this.drawLineGraph(this.id, this.data)
  },
  methods: {
    drawLineGraph (id, data) {
      // eslint-disable-next-line no-unused-vars
      let _this = this
      let muChart = document.getElementById(id)
      this.ChartLineGraph = this.$echarts.init(muChart)
      this.ChartLineGraph.setOption(data)
      window.addEventListener('resize', function () {
        _this.ChartLineGraph.resize()
      })
    }
  },
  beforeDestroy () {
    if (this.ChartLineGraph) {
      this.ChartLineGraph.clear()
    }
  },
  components: {
  }
}
</script>

<style scoped>

</style>

调用组件

import linegraph from '../components/methods'
data () {
	return{
	// 这里就是你传的echart数据
	option: {
        xAxis: {
	          type: 'category',
	          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
	        },
	        yAxis: {
	          type: 'value'
	        },
	        series: [{
	          data: [820, 932, 901, 934, 1290, 1330, 1320],
	          type: 'line'
	        }],
	        tooltip: {}
	      }
	}
}
 components: {
    linegraph
  }

页面调用


<linegraph :id="'ss'" :data='option' style="width:500px;height:300px"</linegraph>
发布了6 篇原创文章 · 获赞 6 · 访问量 239

猜你喜欢

转载自blog.csdn.net/qq_41359437/article/details/105199779
今日推荐