<template>
	<view class="echarts-content" style="background-color: #fff;padding-bottom: 20rpx;">
		<!-- 标题 -->
		<view style="border-bottom: 1.5px solid rgb(204, 204, 204);margin-bottom: 20rpx;padding: 10rpx 0;">
			<u-row>
				<u-col span="6" style="text-align: center;">{{title}}</u-col>
				<u-col span="3">环比 -28%</u-col>
				<u-col span="3">同比 -32%</u-col>
			</u-row>
		</view>
		<!-- 图表 -->
		<view class="charts-box">
			<qiun-data-charts type="column" :opts="opts" :chartData="chartData" />
		</view>
	</view>
</template>

<script>
	export default {
		name: 'ZhuZhuangTu',
		// 父组件接收的datalist值
		props: {
			datalist: {
				type: [Array],
				default: () => []
			},
			title: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				chartData: {}, //图表数据
				minHeight: 0,
				maxHeight: 0,
				opts: {
					color: ["#1890FF", "#91CB74", "#FAC858"],
					padding: [15, 15, 0, 5],
					enableScroll: false,
					legend: {
						show: false
					},
					xAxis: {
						disableGrid: true
					},
					yAxis: {
						scale: true,
						data: []
					},
					extra: {
						column: {
							type: "group",
							width: 50, //每一列的宽度
							activeBgColor: "#000000",
							activeBgOpacity: 0.08
						}
					}
				}
			};
		},
		mounted() {
			this.getServerData();
		},
		methods: {
			getServerData() {
				// 计算y轴刻度线的最大值最小值
				if (Math.min(...this.datalist) < 0) {
					this.opts.yAxis.data.push({
						min: Math.floor(Math.min(...this.datalist))
					})
				}
				//模拟从服务器获取数据时的延时
				// setTimeout(() => {
				//模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
				let res = {
					// 数据名字
					categories: ["去年同期", "上月", "本月"],
					series: [{
						name: "目标值",
						// 图形数据
						data: this.datalist
					}]
				};
				this.chartData = JSON.parse(JSON.stringify(res));
				// }, 500);
			}
		}
	}
</script>

<style>
	/* 图表样式 */
	.charts-box {
		width: 100%;
		height: 500rpx;
	}
</style>