<template>
	<view class="content">

		<view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts2"
			class="echarts"></view>

	</view>
</template>

<script>
	var dwtype = "2";
	var tes = '0';
	var tes2 = '';
	var tyss = "";
	if (dwtype == 2) {
		tes = '一周设备数值变化';
		tes2 = '水压值';
		tyss = "兆帕";
	}
	if (dwtype == 5) {
		tes = '一周水位值变化 (m)';
		tes2 = '水位值';
		tyss = "米";
		document.getElementById('dev_title_lab').innerHTML = '当前水位：';
		document.getElementById('dev_title_unit').innerHTML = 'm';
	}

	export default {
		props: {
			bindData: {
				type: Object,
				default: ''
			}
		},
		data() {
			return {
				option: {
					title: {
						x: 'center',
						text: tes
					},
	
					tooltip: {
						trigger: 'axis',
						axisPointer: {
							type: 'cross',
							animation: false,
							label: {
								backgroundColor: '#505765'
							}
						}
					},

					grid: {
						left: '7%',
						right: '4%',
						bottom: '15%',
						containLabel: true
					},
					toolbox: {
						// feature: {
						//     saveAsImage: {}
						// }
					},
					legend: {
						data: ['水压/水位(兆帕/米)', '电量/信号(%)'],
						// left: 10
					},
					xAxis: {
						type: 'category',
						axisTick: {
							alignWithLabel: true
						},
						data:this.bindData.stiem.map( function(value,index){
							return `${value.substring(0,10)}\n${value.substring(11,20)}`
						})
					},
					yAxis: [{
						name: '水压/水位',
						type: 'value',
						max: 1
					}, {
						name: '电量/信号',
						// nameLocation: 'start',
						max: 100,
						type: 'value',
						// inverse: true
					}],
					// dataZoom: [{
					// 	type: 'slider',
					// 	xAxisIndex: 0,
					// 	filterMode: 'empty',
					// 	start: 0,
					// 	end: 100
					// }, {
					// 	type: 'inside',
					// 	xAxisIndex: 0,
					// 	filterMode: 'empty',
					// 	start: 0,
					// 	end: 100
					// }],

					series: [{
						name: tes2,
						type: 'line',
						stack: 'Pa',
						animation: false,
						data: this.bindData.water_data,
						color: ['#2699fb'],
						yAxisIndex: 0
					}, {
						name: "信号强度",
						type: 'line',
						stack: '%',
						animation: false,
						data: this.bindData.signal_data,
						color: ['#ffff00'],
						yAxisIndex: 1
					}, {
						name: "电池电量",
						type: 'line',
						stack: 'a',
						animation: false,
						data: this.bindData.battery_data,
						color: ['#ff0000'],
						yAxisIndex: 1
					}]
				}
			}
		},
		onLoad() {
			

		},
		methods: {
			changeOption() {
				const data = this.option.series[0].data
				// 随机更新示例数据
				data.forEach((item, index) => {
					data.splice(index, 1, Math.random() * 40)
				})
			},
			onViewClick(options) {
				console.log(options)
			}
		}
	}
</script>

<script module="echarts" lang="renderjs">
	let myChart
	export default {
		mounted() {
			
			console.log('this.bindData')
			console.log(this.bindData)
			
			
			if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script')
				// view 层的页面运行在 www 根目录，其相对路径相对于 www 计算
				script.src = 'static/echarts.min.js'
				script.onload = this.initEcharts.bind(this)
				document.head.appendChild(script)
			}
		},
		methods: {
			initEcharts() {
				myChart = echarts.init(document.getElementById('echarts2'))
				// 观测更新的数据在 view 层可以直接访问到
				myChart.setOption(this.option)
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				// 监听 service 层数据变更
				myChart.setOption(newValue)
			},
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				ownerInstance.callMethod('onViewClick', {
					test: 'test'
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.echarts {
		width: 100%;
		height: 500rpx;
		margin-bottom: 70rpx;
	}
</style>
