<template>
	<div :class="className" :style="{height:height,width:width}"></div>
</template>
<script>
import echarts from 'echarts'
// require('echarts/theme/macarons') // echarts theme

export default {
	props: {
		className: {
			type: String,
			default: 'chart'
		},
		width: {
			type: String,
			default: '100%'
		},
		height: {
			type: String,
			default: '130px'
		},
		chartData: {
			type: Object
		}
		// autoResize: {
		//     type: Boolean,
		//     default: true
		// }
	},
	data() {
		return {
			chart: null
		}
	},
	watch: {
		chartData: {
			deep: true,
			handler: function(val) {
				if (!this.chart) {
					return false
				}
				let chartData = JSON.parse(JSON.stringify(val))
				this.setOptions(chartData)
			}
		}
	},
	mounted() {
		// this.initChart()
	},
	methods: {
		initChart() {
			// this.chart = echarts.init(this.$el, 'macarons')
			this.chart = echarts.init(this.$el)
			let chartData = JSON.parse(JSON.stringify(this.chartData))
			this.setOptions(chartData)
			// this.setOptions(data)
		},
		setOptions(chartData) {
			this.chart.setOption({
				tooltip: {
					trigger: 'item',
					formatter: '{a} <br/>{b} : {c} ({d}%)'
				},
				legend: {
					orient: 'vertical',
					x: '40%',
					y: '24%',
					data: chartData.nameArr
				},
				series: [
					{
						name: '访问来源',
						type: 'pie',
						radius: '80%',
						center: ['20%', '50%'],
						data: chartData.list,
						labelLine: {
							normal: {
								show: false
							}
						},
						label: {
							normal: {
								show: false
							}
						},
						itemStyle: {
							emphasis: {
								shadowBlur: 10,
								shadowOffsetX: 0,
								shadowColor: 'rgba(0, 0, 0, 0.5)'
							},
							normal: {
								color: function(params) {
									return ['#40C0FF', '#F58236', '#54CE96'][params.dataIndex]
								}
							}
						},
						emphasis: {
							itemStyle: {
								shadowBlur: 10,
								shadowOffsetX: 0,
								shadowColor: 'rgba(0, 0, 0, 0.5)'
							}
						}
					}
				]
			})
		}
	},

	filters: {}
}
</script>
