<template>
	<view>
		<view class="title">
			<view class="icon">
				<image src="./icon/icon_o.png" mode=""></image>
			</view>
			<view class="text">当日总报警</view>
		</view>
		<canvas canvas-id="canvasRing" id="canvasRing" @touchstart="touchRing" style="width: 700rpx; height:1100rpx;"></canvas>
	</view>
</template>

<script>
	import uCharts from '@/components/u-charts/u-charts.js';
	var _self;
	var canvaRing = null;

	export default {
		data() {
			return {
				cWidth: '',
				cHeight: '',
				pixelRatio: 1,
				serverData: '',
			}
		},
		mounted() {
			_self = this;
			this.cWidth = uni.upx2px(700);
			this.cHeight = uni.upx2px(1100);
			this.getServerData();
		},
		methods: {
			getServerData() {
				this.$api.jt_safety_alarm_count({

				}).then((res) => {
					// console.log(res.data.content)

					let data = res.data.content.data

					let Ring = {
						series: [{
							name: '',
							data: 0
						}],
						total: 0
					}

					data.map(function(item, index) {
						//新数组的项，用来盛放每一项中的各个参数，每次清空，这样避免改变sevm的值
						var sevm = {};
						//给每一项中的参数初始化并赋值
						sevm['name'] = item.mine_name + " " + item.count + " 个";
						sevm['data'] = item.count;
						//将项放进新的数组
						Ring.series[index] = sevm
					})

					Ring.total = res.data.content.total

					// console.log(Ring)

					_self.showRing("canvasRing", Ring);

				})
			},
			showRing(canvasId, chartData) {
				canvaRing = new uCharts({
					$this: _self,
					canvasId: canvasId,
					type: 'ring',
					fontSize: 12,
					legend: {
						show: true,
						position: 'bottom',
						lineHeight: 30,
					},
					title: {
						name: chartData.total + "个",
						color: '#000000',
						fontSize: 28 * _self.pixelRatio,
						offsetY: 30 * _self.pixelRatio,
					},
					subtitle: {
						name: '总报警量',
						color: '#666666',
						fontSize: 18 * _self.pixelRatio,
						offsetY: -30 * _self.pixelRatio,
					},
					extra: {
						pie: {
							offsetAngle: -90,
							ringWidth: 40 * _self.pixelRatio,
							labelWidth: 20
						}
					},
					background: '#FFFFFF',
					pixelRatio: _self.pixelRatio,
					series: chartData.series,
					animation: true,
					width: _self.cWidth * _self.pixelRatio,
					height: _self.cHeight * _self.pixelRatio,
					disablePieStroke: true,
					dataLabel: false,
				});
			},
			touchRing(e) {
				canvaRing.showToolTip(e, {
					format: function(item) {
						// return item.name + ' : ' + item.data + ' 个'
						return item.name
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.title {
		display: flex;
		align-items: center;

		.icon {
			margin-right: 10rpx;
			image{
				width: 30rpx;
				height: 30rpx;
			}
		}

		.text {}
	}
</style>
