refactor: ♻️ 访问趋势代码优化

This commit is contained in:
Ray.Hao
2025-02-06 21:55:08 +08:00
parent 8e7f62aeb2
commit b9da1e7f71

View File

@@ -23,7 +23,7 @@
</div> </div>
</template> </template>
<div :id="id" :class="className" :style="{ height, width }" /> <div :id="id" :style="{ height, width }" />
</el-card> </el-card>
</template> </template>
@@ -32,33 +32,27 @@ import * as echarts from "echarts";
import LogAPI, { VisitTrendVO } from "@/api/system/log"; import LogAPI, { VisitTrendVO } from "@/api/system/log";
import { dayjs } from "element-plus"; import { dayjs } from "element-plus";
// 日期范围
const recentDaysRange = ref(7);
// 图表对象
const chart: Ref<echarts.ECharts | null> = ref(null);
const props = defineProps({ const props = defineProps({
id: { id: {
type: String, type: String,
default: "VisitTrend", default: "VisitTrend",
}, },
className: {
type: String,
default: "",
},
width: { width: {
type: String, type: String,
default: "200px", default: "100%",
required: true,
}, },
height: { height: {
type: String, type: String,
default: "200px", default: "500px",
required: true,
}, },
}); });
/** 设置图表 */ // 日期范围
const recentDaysRange = ref(7);
// 图表对象
const chart: Ref<echarts.ECharts | null> = ref(null);
// 图表配置
const setChartOptions = (data: VisitTrendVO) => { const setChartOptions = (data: VisitTrendVO) => {
if (!chart.value) { if (!chart.value) {
return; return;
@@ -69,7 +63,7 @@ const setChartOptions = (data: VisitTrendVO) => {
trigger: "axis", trigger: "axis",
}, },
legend: { legend: {
data: ["浏览量(PV)", "IP"], data: ["浏览量(PV)", "访客数(UV)"],
bottom: 0, bottom: 0,
}, },
grid: { grid: {
@@ -108,7 +102,7 @@ const setChartOptions = (data: VisitTrendVO) => {
}, },
}, },
{ {
name: "IP", name: "访客数(UV)",
type: "line", type: "line",
data: data.ipList, data: data.ipList,
areaStyle: { areaStyle: {
@@ -194,14 +188,10 @@ const handleResize = () => {
onMounted(() => { onMounted(() => {
chart.value = markRaw(echarts.init(document.getElementById(props.id) as HTMLDivElement)); chart.value = markRaw(echarts.init(document.getElementById(props.id) as HTMLDivElement));
loadData(); loadData();
// 监听窗口大小变化
window.addEventListener("resize", handleResize); window.addEventListener("resize", handleResize);
}); });
onBeforeUnmount(() => {
window.removeEventListener("resize", handleResize);
});
onActivated(() => { onActivated(() => {
handleResize(); handleResize();
}); });