調整圖表格式化邏輯 | 根據 Type 動態決定時間格式 | 僅顯示整數刻度的 y 軸標籤

This commit is contained in:
koko1108 2025-10-28 18:09:57 +08:00
parent 8d8349a251
commit 3329c581f6

View File

@ -2,9 +2,11 @@
import { ref, computed, inject, watch } from "vue";
import Table from "@/components/customUI/Table.vue";
import LineChart from "@/components/chart/LineChart.vue";
import useSearchParam from "@/hooks/useSearchParam";
import dayjs from "dayjs";
import { SECOND_CHART_COLOR } from "@/constant";
const { searchParams } = useSearchParam();
//
const { tableData, loading } = inject("history_table_data");
@ -35,14 +37,27 @@ const defaultChartOption = {
splitLine: { show: false },
axisLabel: {
color: "#ffffff",
formatter: (value) => dayjs(value).format("HH:mm"), //
formatter: function (value) {
// Type
return searchParams.value.Type == 1
? dayjs(value).format("MM-DD")
: dayjs(value).format("HH:mm");
}, //
},
data: [],
},
yAxis: {
type: "value",
splitLine: { show: false },
axisLabel: { color: "#ffffff" },
axisLabel: {
color: "#ffffff",
formatter: function (value) {
//
return Number.isInteger(value) ? value : "";
},
},
min: "dataMin",
max: "dataMax",
},
series: [],
};
@ -57,7 +72,8 @@ const columns = [
//
const formatChartData = (data) => {
return data.reduce((acc, { building_name, device_name, points, timestamp, value }) => {
return data.reduce(
(acc, { building_name, device_name, points, timestamp, value }) => {
// "" null
const numericValue = value == "無資料" ? null : parseFloat(value);
@ -65,17 +81,21 @@ const formatChartData = (data) => {
if (value === "true" || value === "false") return acc;
// series key (building_name + device_name + points)
const seriesKey = `${building_name || ''} ${device_name || ''} ${points}`;
const seriesKey = `${building_name || ""} ${device_name || ""} ${points}`;
if (!acc[seriesKey]) {
acc[seriesKey] = { timestamps: [], values: [] };
}
acc[seriesKey].timestamps.push(dayjs(timestamp).format("YYYY-MM-DD HH:mm"));
acc[seriesKey].timestamps.push(
dayjs(timestamp).format("YYYY-MM-DD HH:mm")
);
acc[seriesKey].values.push(numericValue);
return acc;
}, {});
},
{}
);
};
//
@ -101,7 +121,9 @@ watch(
groupedData.value[seriesKey].timestamps = sortedData.map(
(item) => item.timestamp
);
groupedData.value[seriesKey].values = sortedData.map((item) => item.value);
groupedData.value[seriesKey].values = sortedData.map(
(item) => item.value
);
});
// series
@ -110,7 +132,9 @@ watch(
type: "line",
data: groupedData.value[seriesKey].values,
showSymbol: false,
itemStyle: { color: SECOND_CHART_COLOR[index % SECOND_CHART_COLOR.length] },
itemStyle: {
color: SECOND_CHART_COLOR[index % SECOND_CHART_COLOR.length],
},
}));
//