<template>
<div
ref="chartContainer3"
style="width: 100%; height: 400px; margin-top: 20px"
></div>
</template>
<script setup>
import * as echarts from "echarts";
import { onMounted, onUnmounted, ref, shallowRef } from "vue";
import request from "@/utils/request";
import api from "@/api";
import formatTime from "@/utils/formatTime";
const chartInstance3 = shallowRef(null); //Define store entry details chart instantiation
const chartContainer3 = shallowRef();
const ServiceCategoryList = ref([]); //Shop category list
// Get all store categories
const getShopCategory = async () => {
const res = await request.get(api.getShopCategory);
res.data.forEach((item) => {
ServiceCategoryList.value.push({
name: item.name,
type: "line", //The type of icon, line: line chart
// stack: "total",
shopType: item.type,
smooth: "true",
data: [],
});
});
};
const shopList = ref([]); //Shop list
const startTime = ref(""); //The earliest time to create a store when checking in
const endTime = new Date(); //Current time
const dateArray = ref([]); //Date array
// Create a time list
const createTimeList = (start, end) => {
while (start <= end) {
dateArray.value.push(start.toISOString().substring(0, 10)); // Only retain the YYYY-MM-DD format
start.setDate(start.getDate() + 1); // Add one day
}
};
// Get all the stores on the platform
const getShopList = async () => {
await request.get(api.shopList).then((res) => {
res.data.forEach((item) => {
item.createTime = formatTime(item.createTime).nohour;
item.createTimeObj = new Date(item.createTime);
shopList.value.push({
shopName: item.shopName,
createTime: item.createTime,
shopType: item.shopType,
});
});
// Use the sort method to sort shopList by creation time and find out the earliest time to settle in
res.data.sort((a, b) => a.createTimeObj - b.createTimeObj);
startTime.value = new Date(res.data[0].createTime);
createTimeList(startTime.value, endTime); // Generate time list
// Group all stores by store category
const shopsGroupedByTypeAndTime = res.data.reduce((groups, shop) => {
const shopType = shop.shopType;
const shopTime = shop.createTime;
// If there is no grouping for the corresponding store type, create it
if (!groups[shopType]) {
groups[shopType] = {};
}
// If there is no grouping for the corresponding time, create it
if (!groups[shopType][shopTime]) {
groups[shopType][shopTime] = 0;
}
// Increase the count of the corresponding time and type by 1
groups[shopType][shopTime]++;
return groups;
}, {});
ServiceCategoryList.value.forEach((item) => {
if (shopsGroupedByTypeAndTime[item.shopType]) {
item.data = shopsGroupedByTypeAndTime[item.shopType];
}
dateArray.value.forEach((date) => {
if (!item.data[date]) {
item.data[date] = 0;
}
});
item.data = Object.entries(item.data)
.sort(([keyA], [keyB]) => new Date(keyA) - new Date(keyB))
.map(([key, value]) => value);
});
});
};
const setOptions3 = () => {
chartInstance3.value.setOption({
title: {
text: "Merchant entry data details",
},
tooltip: {
trigger: "axis",
axisPointer: {
type: "cross",
},
},
legend: {
data: ServiceCategoryList.value.map((item) => item.name),
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true,
},
xAxis: {
type: "category",
boundaryGap: false,
data: dateArray.value,
},
yAxis: {
type: "value",
},
series: [...ServiceCategoryList.value],
});
};
onMounted(async () => {
await getShopCategory();
await getShopList();
chartInstance3.value = echarts.init(chartContainer3.value);
setOptions3();
});
onUnmounted(() => {
// Destroy the chart instance when component is uninstalled
chartInstance3.value.dispose();
});
</script>