web-backend/templates/streamordersummary.html

92 lines
3.0 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>动态订单数量图表</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
canvas {
width: 100% !important;
height: 400px !important;
}
</style>
</head>
<body>
<h2>订单数量动态图表</h2>
<canvas id="orderChart"></canvas>
<script>
// 初始化图表
const ctx = document.getElementById('orderChart').getContext('2d');
const chartData = {
labels: [], // X轴标签
datasets: [{
label: '订单数量',
data: [], // Y轴数据
borderColor: 'rgb(75, 192, 192)', // 线条颜色
backgroundColor: 'rgba(75, 192, 192, 0.2)', // 背景填充色
borderWidth: 1
}]
};
const config = {
type: 'line', // 使用折线图
data: chartData,
options: {
responsive: true,
scales: {
x: {
type: 'category', // X轴使用类别型
position: 'bottom',
},
y: {
beginAtZero: true, // Y轴从0开始
ticks: {
stepSize: 1
}
}
}
}
};
const orderChart = new Chart(ctx, config);
// 获取数据并更新图表
function fetchDataAndUpdateChart() {
fetch('/api/stream/ordersummary') // 替换为你的实际API地址
.then(response => response.json())
.then(data => {
// 统计每个时间点的订单数量
let orderCounts = {};
data.forEach(order => {
const status = order.status; // 获取订单时间
const count = order.count; // 获取订单数量
// 按时间统计数量
if (orderCounts[status]) {
orderCounts[status] += count;
} else {
orderCounts[status] = count;
}
});
// 更新图表数据
chartData.labels = Object.keys(orderCounts); // 设置X轴标签为时间
chartData.datasets[0].data = Object.values(orderCounts); // 设置Y轴数据为订单数量
// 更新图表
orderChart.update();
})
.catch(error => console.error('获取数据失败:', error));
}
// 每5秒更新一次数据
setInterval(fetchDataAndUpdateChart, 5000);
// 初始数据加载
fetchDataAndUpdateChart();
</script>
</body>
</html>