目的
- 減少重復(fù)代碼量
- 統(tǒng)一風(fēng)格
- 全局事件處理方便維護(hù)
- 可以根據(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ù)。