pie.vue 1.7 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 20 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
	props: ['data', 'id'],
	watch: {
		data() {
			this.initPie()
		}
	},
	mounted() {
		this.initPie()
		const erd = elementResizeDetectorMaker()
		erd.listenTo(document.getElementById(this.id), () => {
			this.$nextTick(() => {
				//监听到事件后执行的业务逻辑
				this.initPie()
			})
		})
	},
	destroyed() {
		window.onresize = null
	},
	methods: {
		initPie() {
			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'
				},
				legend: {
					orient: 'horizontal',
					top: '1%'
				},
				series: [
					{
						type: 'pie',
						radius: '50%',
						data: this.data
						// data: [
						//   {
						//     value: 1048,
						//     name: '风冷热泵机组',
						//     itemStyle: { color: 'skyblue' }
						//   },
						//   {
						//     value: 735,
						//     name: '室内机风柜',
						//     itemStyle: { color: '#F2637B' }
						//   },
						//   { value: 580, name: '水泵', itemStyle: { color: '#8EE0E0' } },
						//   {
						//     value: 484,
						//     name: '多联室内机',
						//     itemStyle: { color: '#4ECB74' }
						//   },
						//   { value: 300, name: '排风机', itemStyle: { color: '#FFCC00' } }
						// ]
					}
				]
			}
			myCharts.setOption(option)
			myCharts.resize()
			window.addEventListener('resize', function() {
				myCharts.resize()
			})
		}
	}
xiexingan's avatar
xiexingan committed
76 77 78
}
</script>

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