feat(property): 用电情况组件添加电表数据展示

This commit is contained in:
2025-08-29 17:05:25 +08:00
parent e25ec2d703
commit 5774b1224d

View File

@@ -1,14 +1,15 @@
<script setup lang="ts"> <script setup lang="ts">
import { RadioGroup, RadioButton } from 'ant-design-vue' import { RadioGroup, RadioButton, message } from 'ant-design-vue';
import { Page } from '@vben/common-ui' import { Page } from '@vben/common-ui';
import { ref, onMounted, onBeforeUnmount, reactive } from 'vue' import { ref, onMounted, onBeforeUnmount, reactive } from 'vue';
import * as echarts from 'echarts' import * as echarts from 'echarts';
import type { ECharts, EChartsOption } from 'echarts' import type { ECharts, EChartsOption } from 'echarts';
import FloorTree from "../components/floor-tree.vue" import FloorTree from '../components/floor-tree.vue';
import dayjs from "dayjs" import dayjs from 'dayjs';
import { meterRecordTrend } from '#/api/property/energyManagement/meterRecord';
// 左边楼层用 // 左边楼层用
const selectFloorId = ref<string[]>([]) const selectFloorId = ref<string[]>([]);
const chainData = reactive({ const chainData = reactive({
todayEnergy: '231.78', todayEnergy: '231.78',
@@ -23,27 +24,27 @@ const chainData = reactive({
lastYearSamePeriodEnergy: '--', lastYearSamePeriodEnergy: '--',
yearTrendPercentage: '--', yearTrendPercentage: '--',
yearTrendValue: '--', yearTrendValue: '--',
}) });
const peakData = reactive({ const peakData = reactive({
todayPeakPower: '2961.08', todayPeakPower: '2961.08',
todayPeakTime: dayjs().format('YYYY-MM-DD HH:mm:ss'), todayPeakTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
yesterdayPeakPower: '2993.89', yesterdayPeakPower: '2993.89',
yesterdayPeakTime: dayjs().subtract(1, 'day').format('YYYY-MM-DD HH:mm:ss'), yesterdayPeakTime: dayjs().subtract(1, 'day').format('YYYY-MM-DD HH:mm:ss'),
}) });
const energyTrendTime = ref('1') const energyTrendTime = ref('1');
// 能耗趋势图表容器 // 能耗趋势图表容器
const energyTrendChart = ref<HTMLElement | null>(null) const energyTrendChart = ref<HTMLElement | null>(null);
const energyTrendInstance = ref<ECharts | null>(null) const energyTrendInstance = ref<ECharts | null>(null);
const energyTrendOption: EChartsOption = { const energyTrendOption: EChartsOption = {
tooltip: { tooltip: {
trigger: 'item', trigger: 'item',
axisPointer: { axisPointer: {
type: 'shadow' type: 'shadow',
} },
}, },
xAxis: { xAxis: {
type: 'category', type: 'category',
@@ -61,73 +62,78 @@ const energyTrendOption: EChartsOption = {
markPoint: { markPoint: {
data: [ data: [
{ type: 'max', name: 'Max' }, { type: 'max', name: 'Max' },
{ type: 'min', name: 'Min' } { type: 'min', name: 'Min' },
] ],
}, },
} },
], ],
} };
const initEnergyTrendChart = () => { const initEnergyTrendChart = () => {
if (energyTrendChart.value) { if (energyTrendChart.value) {
// 销毁旧实例 // 销毁旧实例
energyTrendInstance.value?.dispose() energyTrendInstance.value?.dispose();
// 创建新实例 // 创建新实例
energyTrendInstance.value = echarts.init(energyTrendChart.value) energyTrendInstance.value = echarts.init(energyTrendChart.value);
// 设置配置项 // 设置配置项
energyTrendInstance.value.setOption(energyTrendOption) energyTrendInstance.value.setOption(energyTrendOption);
buildingEnergyTrendData('1') // buildingEnergyTrendData('1');
// 可选:添加窗口大小变化监听 // 可选:添加窗口大小变化监听
const resizeHandler = () => { const resizeHandler = () => {
energyTrendInstance.value?.resize() energyTrendInstance.value?.resize();
} };
window.addEventListener('resize', resizeHandler) window.addEventListener('resize', resizeHandler);
// 在组件卸载前移除监听 // 在组件卸载前移除监听
onBeforeUnmount(() => { onBeforeUnmount(() => {
window.removeEventListener('resize', resizeHandler) window.removeEventListener('resize', resizeHandler);
}) });
} }
} };
function buildingEnergyTrendData(val: string) { function buildingEnergyTrendData(val: string) {
const now = new Date() if (trendData.value.hour == null) {
let timeArr = [] message.warning('请先选择楼层或电表!');
let valArr = [] return;
let name = '时间' }
const now = new Date();
let timeArr = [];
let valArr = [];
let name = '时间';
if (val == '1') { if (val == '1') {
const hour = now.getHours() const hour = now.getHours();
for (let i = 0; i < hour; i++) { for (let i = 0; i < hour; i++) {
timeArr.push(i) timeArr.push(i + ':00');
valArr.push(parseFloat((Math.random() * 35).toFixed(2)))
} }
valArr = trendData.value.hour.today.data;
} else if (val == '2') { } else if (val == '2') {
const day = now.getDate() const day = now.getDate();
for (let i = 1; i < day; i++) { for (let i = 1; i < day; i++) {
timeArr.push(i) timeArr.push(i);
valArr.push(parseFloat((Math.random() * 800).toFixed(2)))
} }
name = '日期' name = '日期';
valArr = trendData.value.day.nowMonth.data;
} else { } else {
const month = now.getMonth() + 1 const month = now.getMonth() + 1;
for (let i = 1; i < month; i++) { for (let i = 1; i < month + 1; i++) {
timeArr.push(i) timeArr.push(i);
valArr.push(parseFloat((Math.random() * 21000).toFixed(2)))
} }
name = '月份' name = '月份';
valArr = trendData.value.month.nowYear.data;
} }
if (energyTrendInstance.value) { if (energyTrendInstance.value) {
energyTrendInstance.value.setOption({ energyTrendInstance.value.setOption({
xAxis: { data: timeArr, name }, xAxis: { data: timeArr, name },
series: [{ data: valArr }], series: [{ data: valArr }],
}) });
} }
} }
//日用电率 //日用电率
const powerCurveChart = ref<HTMLElement | null>(null) const powerCurveChart = ref<HTMLElement | null>(null);
const powerCurveInstance = ref<ECharts | null>(null) const powerCurveInstance = ref<ECharts | null>(null);
const powerCurveOption: EChartsOption = { const powerCurveOption: EChartsOption = {
tooltip: { tooltip: {
@@ -135,28 +141,53 @@ const powerCurveOption: EChartsOption = {
axisPointer: { axisPointer: {
type: 'cross', type: 'cross',
crossStyle: { crossStyle: {
color: '#999' color: '#999',
} },
} },
}, },
toolbox: { toolbox: {
feature: { feature: {
magicType: { show: true, type: ['line', 'bar'] }, magicType: { show: true, type: ['line', 'bar'] },
restore: { show: true }, restore: { show: true },
} },
}, },
legend: { legend: {
data: ['今日', '昨日'] data: ['今日', '昨日'],
}, },
xAxis: [ xAxis: [
{ {
type: 'category', type: 'category',
data: [], data: [
'0:00',
'1:00',
'2:00',
'3:00',
'4:00',
'5:00',
'6:00',
'7:00',
'8:00',
'9:00',
'10:00',
'11:00',
'12:00',
'13:00',
'14:00',
'15:00',
'16:00',
'17:00',
'18:00',
'19:00',
'20:00',
'21:00',
'22:00',
'23:00',
],
axisPointer: { axisPointer: {
type: 'shadow' type: 'shadow',
}, },
name: '时' name: '时',
} },
], ],
yAxis: [ yAxis: [
{ {
@@ -165,99 +196,144 @@ const powerCurveOption: EChartsOption = {
}, },
], ],
series: [ series: [
{
name: '今日',
type: 'line',
smooth: true,
data: [],
markPoint: {
data: [
{ type: 'max', },
{ type: 'min', }
]
},
},
{ {
name: '昨日', name: '昨日',
type: 'line', type: 'line',
smooth: true, smooth: true,
data: [], data: [],
markPoint: { markPoint: {
data: [ data: [{ type: 'max' }, { type: 'min' }],
{ type: 'max', },
{ type: 'min', }
]
}, },
itemStyle: { color: '#cbb0e3' }, // 数据点颜色 itemStyle: { color: '#cbb0e3' }, // 数据点颜色
lineStyle: { color: '#cbb0e3' } // 线条颜色 lineStyle: { color: '#cbb0e3' }, // 线条颜色
}, },
] ],
} };
const initPowerCurveChart = () => { const initPowerCurveChart = () => {
if (powerCurveChart.value) { if (powerCurveChart.value) {
// 销毁旧实例 // 销毁旧实例
powerCurveInstance.value?.dispose() powerCurveInstance.value?.dispose();
// 创建新实例 // 创建新实例
powerCurveInstance.value = echarts.init(powerCurveChart.value) powerCurveInstance.value = echarts.init(powerCurveChart.value);
// 设置配置项 // 设置配置项
powerCurveInstance.value.setOption(powerCurveOption) powerCurveInstance.value.setOption(powerCurveOption);
buildingPowerCurveData()
// 可选:添加窗口大小变化监听 // 可选:添加窗口大小变化监听
const resizeHandler = () => { const resizeHandler = () => {
powerCurveInstance.value?.resize() powerCurveInstance.value?.resize();
} };
window.addEventListener('resize', resizeHandler) window.addEventListener('resize', resizeHandler);
// 在组件卸载前移除监听 // 在组件卸载前移除监听
onBeforeUnmount(() => { onBeforeUnmount(() => {
window.removeEventListener('resize', resizeHandler) window.removeEventListener('resize', resizeHandler);
}) });
} }
} };
function buildingPowerCurveData() {
const now = new Date()
const hour = now.getHours()
let yesterday = []
let today = []
let timeDate = []
for (let i = 0; i < 24; i++) {
timeDate.push(i)
yesterday.push(parseFloat((Math.random() * 3000).toFixed(2)))
if (hour > i) {
today.push(parseFloat((Math.random() * 3000).toFixed(2)))
}
}
if (powerCurveInstance.value) {
powerCurveInstance.value.setOption({
xAxis: { data: timeDate },
series: [{ data: today }, { data: yesterday }],
})
}
}
// 组件挂载后初始化图表 // 组件挂载后初始化图表
onMounted(() => { onMounted(() => {
initEnergyTrendChart() initEnergyTrendChart();
initPowerCurveChart() initPowerCurveChart();
}) });
// 组件卸载前销毁图表实例 // 组件卸载前销毁图表实例
onBeforeUnmount(() => { onBeforeUnmount(() => {
energyTrendInstance.value?.dispose() energyTrendInstance.value?.dispose();
powerCurveInstance.value?.dispose() powerCurveInstance.value?.dispose();
}) });
function handleSelectFloor() { const trendData = ref<any>({});
console.log(selectFloorId.value[0]) async function handleSelectFloor(selectedKeys, info) {
const now = new Date();
// 获取年、月、日
const year = now.getFullYear();
// 月份从0开始所以要+1并格式化为两位数
const month = String(now.getMonth() + 1).padStart(2, '0');
// 日期格式化为两位数
const day = String(now.getDate()).padStart(2, '0');
let data = {
day: year + '-' + month + '-' + day,
month: year + '-' + month,
year: year,
meterType: 1,
meterId: null,
floorId: null,
};
if (info.node.level == 3) {
data.floorId = selectedKeys[0];
} else {
data.meterId = selectedKeys[0];
}
const trend = await meterRecordTrend(data);
trendData.value = trend;
// 趋势曲线
let timeArr = [];
let valArr = [];
let name = '时间';
if (energyTrendTime.value == '1') {
const hour = now.getHours();
for (let i = 0; i < hour; i++) {
timeArr.push(i + ':00');
}
valArr = trend.hour.today.data;
} else if (energyTrendTime.value == '2') {
const day = now.getDate();
for (let i = 1; i < day; i++) {
timeArr.push(i);
}
name = '日期';
valArr = trend.day.nowMonth.data;
} else {
const month = now.getMonth() + 1;
for (let i = 1; i < month + 1; i++) {
timeArr.push(i);
}
name = '月份';
valArr = trend.month.nowYear.data;
}
if (energyTrendInstance.value) {
energyTrendInstance.value.setOption({
xAxis: { data: timeArr, name },
series: [{ data: valArr }],
});
}
if (powerCurveInstance.value) {
powerCurveInstance.value.setOption({
series: [
{
name: '今日',
type: 'line',
smooth: true,
data: trend.hour.today.data,
markPoint: {
data: [{ type: 'max' }, { type: 'min' }],
},
},
{
name: '昨日',
type: 'line',
smooth: true,
data: trend.hour.yesterday.data,
markPoint: {
data: [{ type: 'max' }, { type: 'min' }],
},
},
],
});
}
} }
</script> </script>
<template> <template>
<Page :auto-content-height="true"> <Page :auto-content-height="true">
<div class="flex h-full gap-[8px]"> <div class="flex h-full gap-[8px]">
<FloorTree class="w-[260px]"></FloorTree> <FloorTree class="w-[260px]" @select="handleSelectFloor"></FloorTree>
<div class="flex-1 overflow-hidden"> <div class="flex-1 overflow-hidden">
<div class="row"> <div class="row">
<div class="energy-trend-container"> <div class="energy-trend-container">
@@ -265,26 +341,28 @@ function handleSelectFloor() {
<div class="section-header"> <div class="section-header">
<div class="header-title">能耗趋势</div> <div class="header-title">能耗趋势</div>
</div> </div>
<RadioGroup v-model:value="energyTrendTime" button-style="solid" size="small" <RadioGroup
@change="buildingEnergyTrendData(energyTrendTime)"> v-model:value="energyTrendTime"
button-style="solid"
size="small"
@change="buildingEnergyTrendData(energyTrendTime)"
>
<RadioButton value="1">当日</RadioButton> <RadioButton value="1">当日</RadioButton>
<RadioButton value="2">当月</RadioButton> <RadioButton value="2">当月</RadioButton>
<RadioButton value="3">当年</RadioButton> <RadioButton value="3">当年</RadioButton>
</RadioGroup> </RadioGroup>
</div> </div>
<div class="chart-placeholder" ref="energyTrendChart"> <div class="chart-placeholder" ref="energyTrendChart"></div>
</div>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="power-curve-container"> <div class="power-curve-container">
<div class="section-header"> <div class="section-header">
<div class="header-title">日用电功率曲线</div> <div class="header-title">平均电功率曲线</div>
</div>
<div class="power-chart" ref="powerCurveChart">
</div> </div>
<div class="power-chart" ref="powerCurveChart"></div>
</div> </div>
<div class="power-peak-container"> <!-- <div class="power-peak-container">
<div class="section-header"> <div class="section-header">
<div class="header-title">电功率峰值</div> <div class="header-title">电功率峰值</div>
</div> </div>
@@ -298,7 +376,7 @@ function handleSelectFloor() {
<p class="time">{{ peakData.yesterdayPeakTime }}</p> <p class="time">{{ peakData.yesterdayPeakTime }}</p>
<div class="bottom-text">昨日(kW)</div> <div class="bottom-text">昨日(kW)</div>
</div> </div>
</div> </div> -->
</div> </div>
</div> </div>
</div> </div>
@@ -332,7 +410,7 @@ function handleSelectFloor() {
.energy-trend-top { .energy-trend-top {
display: flex; display: flex;
justify-content: space-between justify-content: space-between;
} }
.section-header { .section-header {