<template>
	<view class="charts-box">
		<qiun-data-charts type="column" :eopts="opts" :chartData="chartData" :ontouch="true" :onzoom="true"
			:echartsH5="true" />
	</view>
</template>

<script>
	import {
		nowDate,
		timeToDate
	} from '../../utils/date.js'
	export default {
		props: {
			echartsData: Object
		},
		data() {
			return {
				chartData: {},
				//您可以通过修改 config-ucharts.js 文件中下标为 ['column'] 的节点来配置全局默认参数，如都是默认参数，此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式，达到页面简洁的需求。
				opts: {
					color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
						"#ea7ccc"
					],
					padding: [15, 15, 0, 5],
					enableScroll: true,
					legend: {
						type: 'scroll',
						pageIconSize: 18,
						margin: 20,
						itemGap: 20,
						lineHeight: 20,
						top: 0
					},
					grid: {
						bottom: '25%',
						top: "50px",
						left: "45px",
						right: "30px"
					},
					xAxis: {
						disableGrid: true,
						scrollShow: false,
						itemCount: 2,
						scrollAlign: 'right'
						// rotateLabel: true,
						// rotateAngle: -20
					},
					yAxis: {
						data: [{
							min: 0
						}]
					},
					extra: {
						column: {
							type: "group",
							width: 30,
							activeBgColor: "#000000",
							activeBgOpacity: 0.08,
							seriesGap: 10
						}
					},
					tooltip: {
						trigger: 'axis',
						position:'inside'
						// axisPointer: {
						// 	type: 'cross',
						// 	label: {
						// 		backgroundColor: '#6a7985'
						// 	}
						// }
					},
					dataZoom: [{
							type: 'slider',
							show: true,
							xAxisIndex: [0],
							start: 90,
							end: 100
						},
						{
							type: 'slider',
							show: true,
							yAxisIndex: [0],
							left: '93%',
							start: 60,
							end: 100
						},
						{
							type: 'inside',
							xAxisIndex: [0],
							start: 90,
							end: 100
						},
						{
							type: 'inside',
							yAxisIndex: [0],
							start: 60,
							end: 100
						}
					],
				},
				timeNum: 10,
				barTime: null
			};
		},
		mounted() {
			this.getEmit();
		},
		beforeDestroy() {
			clearInterval(this.barTime)
			this.barTime = null
		},
		methods: {
			getServerData() {
				const chartData = this.echartsData.chartItemList
				const timeData = chartData[0].valueTimeList
				const time = timeData.map(item => {
					return item.substring(0, 19)
				})
				const series = chartData.map(item => {
					return {
						name: item.describe ? item.describe : item.itemName,
						data: item.valueList
					}
				})
				this.opts.xAxis.data = time
				let res = {
					categories: time,
					series: series
				};
				this.chartData = JSON.parse(JSON.stringify(res));
			},
			getEmit() {
				const {
					bucketType,
					bucketValue
				} = this.echartsData
				let time = null
				switch (bucketType) {
					case 0:
						time = 86400000 * bucketValue
						this.timeNum = 4
						break;
					case 1:
						time = 3600000 * bucketValue
						this.timeNum = 13
						break;
					case 2:
						time = 60000 * bucketValue
						this.timeNum = 16
						break;
					case 3:
						time = 1000 * bucketValue
						this.timeNum = 19
						break;
				}
				this.getServerData()
				this.barTime = setInterval(() => {
					let chartParams = {
						id: this.echartsData.id,
						startTime: timeToDate(new Date().getTime() - 3600000),
						endTime: nowDate()
					}
					uni.$http.get('/chart/getChartById', chartParams).then(res => {
						const data = res.data
						if (data.code === 200) {
							let chartItemList = data.chartItemList;
							chartItemList.forEach((item, index) => {
								if (item.itemId === this.echartsData[
										index].itemId) {
									if (item.valueList.length !== 0) {
										//执行将新数据添加进去，旧数据去除一个
										this.echartsData[index].valueList =
											this.echartsData[index].valueList
											.concat(item.valueList)
										this.echartsData[
												index]
											.valueList.splice(0, item
												.valueList.length)
										this.echartsData[index]
											.valueTimeList = this.echartsData[
												index]
											.valueTimeList.concat(item
												.valueTimeList)
										this.echartsData[
												index]
											.valueTimeList.splice(0,
												item
												.valueTimeList.length)
									}
								}
							})
							this.getServerData()
						}
					})
				}, time)
			}
		}
	};
</script>

<style scoped>
	/* 请根据实际需求修改父元素尺寸，组件自动识别宽高 */
	.charts-box {
		width: 100%;
		height: 540rpx;
	}
</style>