<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,
			default: () => {}
		}
		// autoResize: {
		//     type: Boolean,
		//     default: true
		// }
	},
	data() {
		return {
			chart: null,
			dataShadow: []
		}
	},
	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) {
			let dataShadow = []
			// let yMax = chartData.total || 500
			let yMax = Math.max.apply(null, chartData.numArr)
			for (var i = 0; i < chartData.numArr.length; i++) {
				dataShadow.push(yMax)
			}

			this.chart.setOption({
				color: ['#3385FF'],
				grid: {
					left: '3%',
					right: '3%',
					top: '30',
					bottom: '0',
					containLabel: true
				},
				xAxis: {
					type: 'category',
					data: chartData.nameArr,
					axisTick: {
						show: false,
						alignWithLabel: true
					},
					axisLine: {
						show: false
					}
				},
				yAxis: {
					axisLine: {
						show: false
					},
					axisTick: {
						show: false
					},
					axisLabel: {
						textStyle: {
							color: '#999'
						}
					}
				},
				dataZoom: [
					{
						type: 'inside'
					}
				],
				series: [
					{
						// For shadow
						type: 'bar',
						itemStyle: {
							color: 'rgba(0,0,0,0.05)'
						},
						barGap: '-100%',
						barCategoryGap: '50%',
						data: dataShadow,
						animation: false
					},
					{
						type: 'bar',
						data: chartData.numArr
					}
				]
			})
		}
	},

	filters: {}
}
</script>
