Vue封装Echarts子组件 动态更新图表展示

1、Vue.js中封装Echarts图表组件

封装Echarts子组件,父组件可以多次调用
<template>
  <div :class="className" :id="id" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '200px'
    },
    chartData: {
      type: Object
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch: {
    chartData: {
      immediate: true,
      deep: true,
      handler(newVal, oldVal) {
        if (this.chart) {
          if (newVal) {
            this.setOption(newVal)
          } else {
            this.setOption(oldVal)
          }
        }
      }
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id), 'macarons')
      this.setOption(this.chartData)
    },
    setOption(option) {
      this.chart.setOption({
        xAxis: {
          data: this.chartData.xAxis.data,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: this.chartData.legend.data
        },
        series: [
          {
            name: this.chartData.series[0].name,
            type: this.chartData.series[0].type,
            data: this.chartData.series[0].data,
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>

2、父组件向子组件传值

<bar-chart v-if="chartFlag" :id="'mybarchart'" :height="'300px'" :chart-data="chartData"></bar-chart>
chartData: {
        xAxis: {
          data: ['星期一', '星期二', '星期三', '星期四', '星期五']
        },
        legend: {
          data: ['123']
        },
        series: [
          {
            name: '123',
            type: 'bar',
            data: [11, 22, 33, 44, 55]
          }
        ]
      }

3、子组件监听数据变化,更新图表

watch: {
    chartData: {
      immediate: true,
      deep: true,
      handler(newVal, oldVal) {
        if (this.chart) {
          if (newVal) {
            this.setOption(newVal)
          } else {
            this.setOption(oldVal)
          }
        }
      }
    }
  },
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言 您将在本文当中了解到,往网页中添加数据,从传统的dom操作过渡到数据层操作,实现同一个目标,两种不同的方式....
    itclanCoder阅读 26,281评论 1 12
  • 黄芪的作用 黄芪是豆科植物,它是一味常用的中药。 它的 主要药理作用是“益气固表”,可以“利水”,也可以 “托毒生...
    de7c69bfb64b阅读 1,053评论 0 0
  • 一群女生追着季楠翎,渐渐地她越跑越慢,最后被逼入了一个小巷子。 “站住,快把她拦住,千万不能让她跑了!” 季...
    旧世浮沉阅读 414评论 0 0
  • 人们说“有朝一日”的时候 其实意思就是 “不会再有” ——乔纳森·诺兰『西部世界』
    野有蔓草依稀阅读 199评论 0 0
  • 旅行的新意 不仅在于去见不同风格的美景 不同的旅行经历才是最宝贵的 你的人生还需要一次真正意义的露营 不管是想寻求...
    戥塔阅读 1,105评论 0 0

友情链接更多精彩内容