fix: 能源管理修正顯示的日期區間

This commit is contained in:
MJM_2025_05\polly 2025-08-08 10:21:18 +08:00
parent 14467e15df
commit 94568c3f86
2 changed files with 49 additions and 10 deletions

View File

@ -66,21 +66,35 @@ watch(
() => elecDay_data.value, () => elecDay_data.value,
(newData) => { (newData) => {
if (newData.length > 0) { if (newData.length > 0) {
// time YYYY-MM-DD HH:mm:ss
const sortedData = [...newData].sort(
(a, b) => new Date(a.time) - new Date(b.time)
);
// 7
const recent7Data = sortedData.slice(-7);
const categories = []; const categories = [];
const seriesData = []; const seriesData = [];
newData.forEach((item) => { recent7Data.forEach((item) => {
categories.push(item.time.split(" ")[0]); categories.push(item.time.split(" ")[0]); // x
const dailyUsage =(item.degreePeak + item.degreeHalfPeak + item.degreeOffPeak).toFixed(1); const dailyUsage = (
item.degreePeak +
item.degreeHalfPeak +
item.degreeOffPeak
).toFixed(1);
seriesData.push(dailyUsage); seriesData.push(dailyUsage);
}); });
//
demand_chart.value.chart.setOption({ demand_chart.value.chart.setOption({
xAxis: { xAxis: {
data: categories, data: categories,
}, },
series:{ series: {
data: seriesData, data: seriesData,
} },
}); });
todayUsage.value = seriesData[seriesData.length - 1]; todayUsage.value = seriesData[seriesData.length - 1];
@ -98,7 +112,7 @@ watch(
> >
<div class="card-body"> <div class="card-body">
<h2 class="card-title"> <h2 class="card-title">
{{$t("energy.latest_elec_consumption")}} {{ $t("energy.latest_elec_consumption") }}
<p>{{ todayUsage }} kWh</p> <p>{{ todayUsage }} kWh</p>
</h2> </h2>
<LineChart <LineChart

View File

@ -17,6 +17,28 @@ const daysInMonth = (month) => {
watch( watch(
() => elecMonth_data.value, () => elecMonth_data.value,
(newData) => { (newData) => {
// 🔍 Log 1
console.log("📦 elecMonth_data 原始資料:", newData);
const latestData = newData[newData.length - 1];
// 🔍 Log 2
console.log("🧾 latestData用於 IntervalElecBills 的來源):", latestData);
// 🔍 Log 3
console.log("💰 各時段費用:", {
costPeak: latestData.costPeak,
costHalfPeak: latestData.costHalfPeak,
costOffPeak: latestData.costOffPeak,
costBase: latestData.costBase,
});
// 🔍 Log 4
const rawIntervalElec =
latestData.costPeak +
latestData.costHalfPeak +
latestData.costOffPeak +
latestData.costBase;
console.log("💵 IntervalElecBills 原始總額(未格式化):", rawIntervalElec);
// //
totalElecBills.value = newData totalElecBills.value = newData
.reduce((sum, item) => { .reduce((sum, item) => {
@ -36,7 +58,7 @@ watch(
.replace(/\B(?=(\d{3})+(?!\d))/g, ","); .replace(/\B(?=(\d{3})+(?!\d))/g, ",");
// //
const latestData = newData[newData.length - 1];
IntervalElecBills.value = ( IntervalElecBills.value = (
latestData.costPeak + latestData.costPeak +
latestData.costHalfPeak + latestData.costHalfPeak +
@ -52,12 +74,15 @@ watch(
) )
.toFixed(2) .toFixed(2)
.replace(/\B(?=(\d{3})+(?!\d))/g, ","); .replace(/\B(?=(\d{3})+(?!\d))/g, ",");
const monthDays = latestData.time ? daysInMonth(latestData.time) : 0; const today = new Date();
const todayStr = today.toISOString().split("T")[0];
IntervalDate.value = latestData.time IntervalDate.value = latestData.time
? `${latestData.time}-01~${latestData.time}-${monthDays}` ? `${latestData.time}-01 ~ ${todayStr}`
: ""; : "";
totalDate.value = `${newData[0].time}-01 ~ ${latestData.time}-${monthDays}`; // totalDate 1 1
const startYear = newData[0]?.time?.split("-")[0] ?? "";
totalDate.value = startYear ? `${startYear}-01-01 ~ ${todayStr}` : "";
}, },
{ deep: true } { deep: true }
); );