<template>
	<view class="charts">
		<canvas canvas-id="ChkmnEIgSCrtkEvqLErNxjIEChnJNtRR" id="ChkmnEIgSCrtkEvqLErNxjIEChnJNtRR" class="charts"
			@touchend="tap" />
	</view>
</template>

<script>
	import uCharts from '@/uni_modules/qiun-data-charts/js_sdk/u-charts/u-charts.js';
	import moment from "moment"
	import {Consume} from '@/pages/api/lighting/lighting.js'
	var uChartsInstance = {};
	export default {
		data() {
			return {
				cWidth: 750,
				cHeight: 250,
				nowTime:'',
				beforeTime:'',
				Time:'',
				enValue:'',
				lightValue:'',
			};
		},
		onReady() {
			var date = new Date()
			var nowTime =
				`${date.getFullYear()}-${date.getMonth() + 1<10?`0${date.getMonth() + 1}`:date.getMonth() + 1}-${date.getDate() < 10?`0${date.getDate()}`:date.getDate()}`;
			var beforeTime = 
				`${date.getFullYear() - 1}-${date.getMonth() + 1<10?`0${date.getMonth() + 1}`:date.getMonth() + 1}-${date.getDate() < 10?`0${date.getDate()}`:date.getDate()}`;
			this.nowTime = nowTime
			this.beforeTime = beforeTime
			Consume('year',beforeTime,nowTime).then(res => {
				if(res.code == 200){
					let Time = []
					let enValue = []
					let lightValue = []
					let energy = res.data.energy
					var momentTime
					for (let i = 0; i < energy.length; i++) {
						enValue.push(energy[i].columnValue)
						momentTime = moment(energy[i].timeLine).format("YYYY-MM-DD HH:mm:ss")
						momentTime = momentTime.slice(0, 7)
						Time.push(momentTime)
					}
					let lightRate = res.data.lightRate
					for (let j = 0; j < lightRate.length; j++) {
						lightValue.push(lightRate[j].columnValue)
					}
					enValue.reverse()
					lightValue.reverse()
					Time.reverse()
					this.enValue = enValue
					this.Time = Time
					this.lightValue = lightValue
				}
			})
			//这里的 750 对应 css .charts 的 width
			this.cWidth = uni.upx2px(750);
			//这里的 500 对应 css .charts 的 height
			this.cHeight = uni.upx2px(250);
			this.getServerData();
		},
		methods: {
			getServerData() {
				//模拟从服务器获取数据时的延时
				setTimeout(() => {
					//模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
					let res = {
						categories: this.Time,
						series: [{
								index:0,
								name: "照明消耗",
								data: this.enValue
							},
							{
								index:1,
								name: "亮灯率",
								data: this.lightValue
							}
						]
					};
					this.drawCharts('ChkmnEIgSCrtkEvqLErNxjIEChnJNtRR', res);
				}, 1000);
			},
			drawCharts(id, data) {
				const ctx = uni.createCanvasContext(id, this);
				uChartsInstance[id] = new uCharts({
					type: "line",
					context: ctx,
					width: this.cWidth,
					height: this.cHeight,
					categories: data.categories,
					series: data.series,
					animation: true,
					timing: "easeOut",
					duration: 1000,
					rotate: false,
					rotateLock: false,
					background: "#FFFFFF",
					color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
						"#ea7ccc"
					],
					padding: [15, 10, 0, 15],
					fontSize: 13,
					fontColor: "#666666",
					dataLabel: false,
					dataPointShape: true,
					dataPointShapeType: "solid",
					touchMoveLimit: 60,
					enableScroll: false,
					enableMarkLine: false,
					legend: {
						show: true,
						position: "bottom",
						float: "center",
						padding: 5,
						margin: 5,
						backgroundColor: "rgba(0,0,0,0)",
						borderColor: "rgba(0,0,0,0)",
						borderWidth: 0,
						fontSize: 13,
						fontColor: "#666666",
						lineHeight: 11,
						hiddenColor: "#CECECE",
						itemGap: 10
					},
					xAxis: {
						disableGrid: true,
						disabled: false,
						axisLine: true,
						axisLineColor: "#CCCCCC",
						calibration: true,
						fontColor: "#666666",
						fontSize: 13,
						lineHeight: 20,
						marginTop: 10,
						rotateLabel: true,
						rotateAngle: 45,
						itemCount: 5,
						boundaryGap: "center",
						splitNumber: 5,
						gridColor: "#CCCCCC",
						gridType: "solid",
						dashLength: 4,
						gridEval: 1,
						scrollShow: false,
						scrollAlign: "left",
						scrollColor: "#A6A6A6",
						scrollBackgroundColor: "#EFEBEF",
						title: "",
						titleFontSize: 13,
						titleOffsetY: 0,
						titleOffsetX: 0,
						titleFontColor: "#666666",
						formatter: ""
					},
					yAxis: {
						gridType: "dash",
						dashLength: 2,
						disabled: false,
						disableGrid: false,
						splitNumber: 5,
						gridColor: "#CCCCCC",
						padding: 10,
						showTitle: true,
						data: [{
								type: "value",
								position: "left",
								disabled: false,
								axisLine: true,
								axisLineColor: "#CCCCCC",
								calibration: false,
								fontColor: "#666666",
								fontSize: 13,
								textAlign: "right",
								title: "照明消耗",
								titleFontSize: 13,
								titleOffsetY: 0,
								titleOffsetX: 0,
								titleFontColor: "#666666",
								min: 0,
								max: 2100,
								tofix: null,
								unit: "kW.h",
								formatter: ""
							},
							{
								type: "value",
								position: "right",
								disabled: false,
								axisLine: true,
								axisLineColor: "#CCCCCC",
								calibration: false,
								fontColor: "#666666",
								fontSize: 13,
								textAlign: "right",
								title: "亮灯率",
								titleFontSize: 13,
								titleOffsetY: 0,
								titleOffsetX: 0,
								titleFontColor: "#666666",
								min: 0,
								max: 100,
								tofix: null,
								unit: "%",
								formatter: ""
							}
						]
					},
					extra: {
						line: {
							type: "curve",
							width: 2,
							activeType: "hollow",
							linearType: "none",
							onShadow: false,
							animation: "vertical"
						},
						tooltip: {
							showBox: true,
							showArrow: true,
							showCategory: false,
							borderWidth: 0,
							borderRadius: 0,
							borderColor: "#000000",
							borderOpacity: 0.7,
							bgColor: "#000000",
							bgOpacity: 0.7,
							gridType: "solid",
							dashLength: 4,
							gridColor: "#CCCCCC",
							boxPadding: 3,
							fontSize: 13,
							lineHeight: 20,
							fontColor: "#FFFFFF",
							legendShow: true,
							legendShape: "auto",
							splitLine: true,
							horizentalLine: false,
							xAxisLabel: false,
							yAxisLabel: false,
							labelBgColor: "#FFFFFF",
							labelBgOpacity: 0.7,
							labelFontColor: "#666666"
						},
						markLine: {
							type: "solid",
							dashLength: 4,
							data: []
						}
					}
				});
			},
			tap(e) {
				uChartsInstance[e.target.id].touchLegend(e);
				uChartsInstance[e.target.id].showToolTip(e);
			}
		}
	};
</script>

<style scoped>
	.charts {
		width: 750rpx;
		height: 250rpx;
	}
</style>
