vue封装echarts-002
文章目录
<template lang="html">
<div class="chart chart-bar">
<div ref="chartBar"></div></div>
</template>
<script>
import * as chart from 'assets/js/echart-config';
export default {
props: {
sourceInfo: {
type: Array,
required: true,
default: () => {
return []
}
},
barColors: {
type: Array,
required: false,
default: () => {
return ['#115bce', '#3988ff']
}
},
chartTitle: {
type: String,
required: false,
default: '使用权限 TOP10'
}},
data() {
return {
}},
computed: {
chartDom() {
return echarts.init(this.$refs.chartBar);
},
optionDom() {
const option = chart.barOption(this.sourceInfo, this.chartTitle, this.barColors);
return option;
}},
watch: {
sourceInfo: {
handler(cates) {
this.hideLoading();
if ( cates && cates.length ) {
this.doRender();
} else {
this.chartDom.setOption(chart.emptyOption(this.chartTitle), true);
}
},
deep: true
}},
methods: {
doRender() {
this.chartDom.setOption(this.optionDom, true);
},
resize() {
this.chartDom.resize();
},
showLoading() {
this.chartDom.showLoading({
text: '',
color: '#3988ff',
textColor: '#000',
maskColor: 'rgba(255, 255, 255, 0)',
zlevel: 0
});
},
hideLoading() {
this.chartDom.hideLoading();
}},
mounted() {
this.chartDom.resize(400, 400);
this.showLoading();
window.addEventListener('resize', this.resize);},
beforeDestroy() {
this.chartDom.off('click');
this.chartDom.clear();
this.chartDom.dispose();},
destroyed() {
window.removeEventListener('resize', this.resize);}
}
</script>
<style lang="scss">
</style>
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论