Vue封裝EChat組件

目的

  1. 減少重復(fù)代碼量
  2. 統(tǒng)一風(fēng)格
  3. 全局事件處理方便維護(hù)
  4. 可以根據(jù)實(shí)際需求暴露對(duì)應(yīng)的事件和方法
### 代碼
<template>
  <div ref="chart" class="my-chart"></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'EChart',
  props: {
    option: {
      type: Object,
      default: () => {
        return null
      }
    }
  },
  data() {
    return {
      myChart: null
    }
  },
  watch: {
    option(newValue) {
      if (newValue) {
        this.loadChart()
      }
    }
  },
  mounted() {
    this.myChart = echarts.init(this.$refs.chart)
    if (this.option) {
      this.loadChart()
    } else {
      this.myChart.showLoading({
        text: '數(shù)據(jù)加載中...',
        color: '#81B5FD',
        textColor: '#81B5FD',
        maskColor: 'rgba(255, 255, 255, 0.2)',
        showSpinner: false,
        zlevel: 0
      })
    }
  },
  methods: {
    loadChart() {
      this.myChart.hideLoading()
      this.myChart.setOption(this.option)
      window.addEventListener('resize', () => {
        this.myChart.resize()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.my-chart {
  width: 100%;
  height: 100%;
}
</style>

使用

<template>
  <my-card title="業(yè)務(wù)類別">
    <e-chart :option="option" />
  </my-card>
</template>
<script>
export default {
  name: 'BusinessCategory',
  data() {
    return {
      option: null,
      seriesData: [
        { value: 1048, name: '房建' },
        { value: 735, name: '鐵路' },
        { value: 580, name: '公路' },
        { value: 484, name: '貿(mào)易' },
        { value: 300, name: '境外開礦' }
      ]
    }
  },
  mounted() {
    this.handleChartInit()
  },
  methods: {
    handleChartInit() {
      this.option = {
        tooltip: {
          trigger: 'item',
          formatter: ' </br>{c}(u0z1t8os%)'
        },
        label: {
          alignTo: 'edge',
          formatter: '{name|}\n{time|{c}} ({time|u0z1t8os}%)',
          minMargin: 5,
          edgeDistance: 10,
          lineHeight: 15,
          rich: {
            time: {
              fontSize: 10,
              color: '#999'
            }
          }
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['45%', '70%'],
            data: this.seriesData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
    }
  }
}
</script>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 13,997評(píng)論 2 59
  • Android組件化項(xiàng)目地址:Android組件化項(xiàng)目AndroidModulePattern Android組件...
    半灬邊灬天閱讀 3,003評(píng)論 4 37
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒(méi)有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,658評(píng)論 1 32
  • 不怕跌倒,所以飛翔 組件化開發(fā) 參考資源 Android組件化方案 為什么要組件化開發(fā) 解決問(wèn)題 實(shí)際業(yè)務(wù)變化非常...
    筆墨Android閱讀 3,096評(píng)論 0 0
  • 前言 您將在本文當(dāng)中了解到,往網(wǎng)頁(yè)中添加數(shù)據(jù),從傳統(tǒng)的dom操作過(guò)渡到數(shù)據(jù)層操作,實(shí)現(xiàn)同一個(gè)目標(biāo),兩種不同的方式....
    itclanCoder閱讀 26,244評(píng)論 1 12

友情鏈接更多精彩內(nèi)容