Files
admin-vben5/apps/web-antd/src/views/dashboard/analytics/analytics-trends.vue

133 lines
3.0 KiB
Vue
Raw Normal View History

2025-06-18 11:03:42 +08:00
<script lang="ts" setup>
import type { EchartsUIType } from '@vben/plugins/echarts';
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
import { onMounted, ref } from 'vue';
2025-09-05 19:12:46 +08:00
import { meterRecordTrend } from '#/api/property/energyManagement/meterRecord';
import dayjs from 'dayjs';
2025-06-18 11:03:42 +08:00
const chartRef = ref<EchartsUIType>();
const { renderEcharts } = useEcharts(chartRef);
2025-09-05 19:12:46 +08:00
// 添加日期选择相关逻辑
const selectedDate = ref<string>(dayjs().format('YYYY-MM'));
// 获取当月天数
const getDaysInMonth = (date: string) => {
const year = parseInt(date.split('-')[0]);
const month = parseInt(date.split('-')[1]);
const daysInMonth = new Date(year, month, 0).getDate();
return Array.from({ length: daysInMonth }, (_, i) => i + 1);
};
2025-06-18 11:03:42 +08:00
2025-09-05 19:12:46 +08:00
const handleDateChange = () => {
console.log('selectedDate', selectedDate.value);
getMeterRecordTrend();
};
const getMeterRecordTrend = async () => {
const res = await meterRecordTrend(
{
day: dayjs().format('YYYY-MM-DD'),
month: selectedDate.value || dayjs().format('YYYY-MM'),
year: dayjs().format('YYYY'),
meterType: 1,
meterId: null,
floorId: null,
}
);
console.log(res);
// 处理返回的数据
const chartData = res.day.nowMonth.data || [];
console.log(chartData);
// 创建一个映射,将日期和数值对应起来
const dataMap: Record<string, string> = {};
chartData.forEach(([day, value]: [string, string]) => {
dataMap[day] = value;
});
// 获取当月所有日期
const days = getDaysInMonth(selectedDate.value);
// 为没有数据的日期填充0构建完整的数据数组
const seriesData = days.map(day => {
return parseFloat(dataMap[day.toString()] || '0');
});
2025-06-18 11:03:42 +08:00
renderEcharts({
grid: {
bottom: 0,
containLabel: true,
left: '1%',
right: '1%',
top: '2 %',
},
series: [
{
areaStyle: {},
2025-09-05 19:12:46 +08:00
data: seriesData,
2025-06-18 11:03:42 +08:00
itemStyle: {
color: '#5ab1ef',
},
smooth: true,
type: 'line',
2025-09-05 19:12:46 +08:00
}
2025-06-18 11:03:42 +08:00
],
tooltip: {
axisPointer: {
lineStyle: {
color: '#019680',
width: 1,
},
},
trigger: 'axis',
},
// xAxis: {
// axisTick: {
// show: false,
// },
// boundaryGap: false,
// data: Array.from({ length: 18 }).map((_item, index) => `${index + 6}:00`),
// type: 'category',
// },
xAxis: {
axisTick: {
show: false,
},
boundaryGap: false,
2025-09-05 19:12:46 +08:00
data: days,
2025-06-18 11:03:42 +08:00
splitLine: {
lineStyle: {
type: 'solid',
width: 1,
},
show: true,
},
type: 'category',
},
yAxis: [
{
axisTick: {
show: false,
},
2025-09-05 19:12:46 +08:00
// max: 800,
2025-06-18 11:03:42 +08:00
splitArea: {
show: true,
},
splitNumber: 4,
type: 'value',
},
],
});
2025-09-05 19:12:46 +08:00
};
onMounted(async () => {
getMeterRecordTrend();
2025-06-18 11:03:42 +08:00
});
2025-09-05 19:12:46 +08:00
2025-06-18 11:03:42 +08:00
</script>
<template>
2025-09-05 19:12:46 +08:00
<div>
2025-06-18 11:03:42 +08:00
<EchartsUI ref="chartRef" />
2025-09-05 19:12:46 +08:00
</div>
2025-06-18 11:03:42 +08:00
</template>