pie2.vue 2.08 KB
Newer Older
xiexingan's avatar
xiexingan committed
1 2
<template lang="pug">
div(:id="id", :style="{ width: '100%', height: '90%' }")
xiexingan's avatar
xiexingan committed
3 4 5
</template>

<script>
xiexingan's avatar
xiexingan committed
6 7
import elementResizeDetectorMaker from 'element-resize-detector'

xiexingan's avatar
xiexingan committed
8
export default {
xiexingan's avatar
xiexingan committed
9 10 11 12 13 14 15 16 17 18 19
	props: ['data', 'id', 'legendData'],
	data() {
		return {
			list: {}
		}
	},
	watch: {
		data() {
			this.initPie2()
		}
	},
xiexingan's avatar
xiexingan committed
20

xiexingan's avatar
xiexingan committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	mounted() {
		this.initPie2()
		const erd = elementResizeDetectorMaker()
		erd.listenTo(document.getElementById(this.id), () => {
			this.$nextTick(() => {
				//监听到事件后执行的业务逻辑
				this.initPie2()
			})
		})
	},
	destroyed() {
		window.onresize = null
	},
	methods: {
		initPie2() {
			let dom = document.getElementById(this.id)
			if (!dom) return
			let myCharts = this.$echarts.init(dom)
			let option = {
				color: ['skyblue', '#F2637B', '#8EE0E0', '#FFCC00', '#4ECB74'],
				tooltip: {
					trigger: 'item',
					formatter: '{a} <br/>{b} : {c} ({d}%)'
				},
				legend: {
					type: 'scroll',
					orient: 'vertical',
					right: 10,
					top: 20,
					bottom: 20,
					// data: ['风冷热泵机组', '室内机风柜', '水泵', '多联室内机']
					data: this.legendData
				},
				series: [
					{
						name: '',
						type: 'pie',
						radius: '55%',
						center: ['40%', '50%'],
						data: this.data,
						// data: [
						//   {
						//     name: '风冷热泵机组',
						//     value: '100',
						//     itemStyle: { color: 'skyblue' }
						//   },
						//   {
						//     name: '室内机风柜',
						//     value: '130',
						//     itemStyle: { color: '#F2637B' }
						//   },
						//   { name: '水泵', value: '400', itemStyle: { color: '#8EE0E0' } },
						//   {
						//     name: '多联室内机',
						//     value: '100',
						//     itemStyle: { color: '#FFCC00' }
						//   }
						// ],
						emphasis: {
							itemStyle: {
								shadowBlur: 10,
								shadowOffsetX: 0,
								shadowColor: 'rgba(0, 0, 0, 0.5)'
							}
						}
					}
				]
			}
			myCharts.setOption(option)
			myCharts.resize()
			window.addEventListener('resize', function() {
				myCharts.resize()
			})
		}
	}
xiexingan's avatar
xiexingan committed
96 97 98
}
</script>

xiexingan's avatar
xiexingan committed
99
<style lang="scss" scoped></style>