<template>
	<view>
		<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,
				agoDay: '', //一个月前的年月日
				nowDay: '', //现在的年月日
				//照明能耗
				enValue: [],
				Time: [],
				//亮灯率
				lightValue: [],
			};
		},
		onReady() {
			var that = this
			var date1 = new Date();
			var date2 = new Date(date1);
			//-30为30天前，+30可以获得30天后的日期
			date2.setDate(date1.getDate() - 30);
			//30天前（月份判断是否小于10，小于10的前面+0）
			var agoDay = `${date2.getFullYear()}-${date2.getMonth() + 1<10?`0${date2.getMonth() + 1}`:date2.getMonth() + 1}-${date2.getDate() < 10?`0${date2.getDate()}`:date2.getDate()}`;
			//当前日期
			var nowDay = `${date1.getFullYear()}-${date1.getMonth() + 1<10?`0${date1.getMonth() + 1}`:date1.getMonth() + 1}-${date1.getDate() < 10?`0${date1.getDate()}`:date1.getDate()}`;
			that.agoDay = agoDay
			that.nowDay = nowDay
			Consume('month', agoDay, nowDay).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, 10)
						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()
					that.enValue = enValue
					that.lightValue = lightValue
					that.Time = Time
				}
			})
			//这里的 750 对应 css .charts 的 width
			that.cWidth = uni.upx2px(750);
			//这里的 500 对应 css .charts 的 height
			that.cHeight = uni.upx2px(250);
			that.getServerData();
		},
		methods: {
			getServerData() {
				var that = this
				//模拟从服务器获取数据时的延时
				setTimeout(() => {
					//模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
					let res = {
						categories: that.Time,
						series: [{
								index: 0,
								name: "照明消耗",
								data: that.enValue
							},
							{
								index: 1,
								name: "亮灯率",
								data: that.lightValue
							}
						]
					};
					that.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: 10,
						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: 70,
								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: true,
							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>
