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