<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: '240px'
		},
		chartData: {
			type: Object
		},
		color: {
			type: String,
			default: '#FC243A'
		},
		yName: {
			type: String,
			default: '金额(￥)'
		},
		serviceName: {
			type: String,
			default: '收入统计'
		}
		// 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)
		},
		setOptions(chartData) {
			this.chart.setOption({
				// title: {
				//     text: '堆叠区域图'
				// },
				tooltip: {
					trigger: 'axis',
					axisPointer: {
						type: 'cross',
						label: {
							backgroundColor: '#6a7985'
						}
					}
				},
				legend: {
					x: 'right',
					y: 'top',
					data: [this.serviceName],
					textStyle: {
						color: '#999'
					}
				},
				grid: {
					left: '3%',
					right: '3%',
					top: '30',
					bottom: '0',
					containLabel: true
				},
				xAxis: [
					{
						type: 'category',
						boundaryGap: false,
						axisLabel: {
							show: true,
							textStyle: {
								color: '#999' // 这里用参数代替了
							}
						},
						axisLine: {
							lineStyle: {
								color: '#ddd' // 这里用参数代替了
							}
						},
						axisTick: {
							show: false
						},
						// 网格线
						splitLine: {
							show: true,
							lineStyle: {
								color: '#ddd' // 这里用参数代替了
							}
						},
						data: chartData.nameArr
					}
				],
				yAxis: [
					{
						name: this.yName,
						type: 'value',
						axisLabel: {
							show: true,
							textStyle: {
								color: '#999' // 这里用参数代替了
							}
						},
						axisLine: {
							lineStyle: {
								color: '#999' // 这里用参数代替了
							}
						},
						axisTick: {
							show: false
						},
						splitLine: {
							show: true,
							lineStyle: {
								color: '#ddd' // 这里用参数代替了
							}
						}
					}
				],
				series: [
					// 收入统计
					{
						name: this.serviceName,
						type: 'line',
						smooth: true,
						// stack: '总量',
						yAxisIndex: 0,
						itemStyle: {
							normal: {
								color: this.color // 线颜色
							}
						},
						lineStyle: {
							normal: {
								width: 2,
								shadowColor: 'rgba(0, 0, 0, 0.3)',
								shadowBlur: 4,
								shadowOffsetY: 4						}
						},
						data: chartData.numArr
					}
				]
			})
		}
	},

	filters: {}
}
</script>
