feat: 首页数据对接
This commit is contained in:
@@ -2,43 +2,32 @@
|
||||
import type { EchartsUIType } from '@vben/plugins/echarts';
|
||||
|
||||
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
|
||||
import { onMounted, ref } from 'vue';
|
||||
import { onMounted, ref, defineExpose } from 'vue';
|
||||
import { meterRecordTrend } from '#/api/property/energyManagement/meterRecord';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
const chartRef = ref<EchartsUIType>();
|
||||
const { renderEcharts } = useEcharts(chartRef);
|
||||
// 添加日期选择相关逻辑
|
||||
const selectedDate = ref<string>(dayjs().format('YYYY-MM'));
|
||||
// 获取当月天数
|
||||
const getDaysInMonth = (date: string) => {
|
||||
const getDaysInMonth = (date: any) => {
|
||||
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);
|
||||
};
|
||||
|
||||
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'),
|
||||
const getMeterRecordTrend = async (selectedDate: any) => {
|
||||
const res = await meterRecordTrend({
|
||||
day: dayjs().format('YYYY-MM-DD'),
|
||||
month: selectedDate.value,
|
||||
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> = {};
|
||||
@@ -50,7 +39,7 @@ console.log(chartData);
|
||||
const days = getDaysInMonth(selectedDate.value);
|
||||
|
||||
// 为没有数据的日期填充0,构建完整的数据数组
|
||||
const seriesData = days.map(day => {
|
||||
const seriesData = days.map((day) => {
|
||||
return parseFloat(dataMap[day.toString()] || '0');
|
||||
});
|
||||
renderEcharts({
|
||||
@@ -70,7 +59,7 @@ console.log(chartData);
|
||||
},
|
||||
smooth: true,
|
||||
type: 'line',
|
||||
}
|
||||
},
|
||||
],
|
||||
tooltip: {
|
||||
axisPointer: {
|
||||
@@ -120,13 +109,16 @@ console.log(chartData);
|
||||
});
|
||||
};
|
||||
onMounted(async () => {
|
||||
getMeterRecordTrend();
|
||||
getMeterRecordTrend(selectedDate);
|
||||
});
|
||||
// 暴露方法给父组件调用
|
||||
defineExpose({
|
||||
getMeterRecordTrend,
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<EchartsUI ref="chartRef" />
|
||||
<EchartsUI ref="chartRef" />
|
||||
</div>
|
||||
</template>
|
||||
|
Reference in New Issue
Block a user