101 lines
3.3 KiB
HTML
101 lines
3.3 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: [], // 时间戳标签
|
|
datasets: [{
|
|
label: '累计订单数量',
|
|
data: [], // 累计的订单数量
|
|
borderColor: 'rgb(75, 192, 192)', // 线条颜色
|
|
backgroundColor: 'rgba(75, 192, 192, 0.2)', // 背景填充色
|
|
borderWidth: 1,
|
|
fill: false
|
|
}]
|
|
};
|
|
|
|
const config = {
|
|
type: 'line', // 使用折线图
|
|
data: chartData,
|
|
options: {
|
|
responsive: true,
|
|
scales: {
|
|
x: {
|
|
type: 'linear', // X轴为线性类型
|
|
position: 'bottom',
|
|
title: {
|
|
display: true,
|
|
text: '时间 (秒)'
|
|
}
|
|
},
|
|
y: {
|
|
beginAtZero: true, // Y轴从0开始
|
|
ticks: {
|
|
stepSize: 1
|
|
},
|
|
title: {
|
|
display: true,
|
|
text: '订单数量'
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
const orderChart = new Chart(ctx, config);
|
|
|
|
// 获取数据并更新图表
|
|
function fetchDataAndUpdateChart() {
|
|
fetch('/api/stream/summary')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
// 累计订单数量
|
|
let cumulativeCount = 0;
|
|
let labels = [];
|
|
let counts = [];
|
|
|
|
data.forEach(order => {
|
|
const status = order.status; // 获取订单时间
|
|
const count = order.count; // 获取订单数量
|
|
|
|
cumulativeCount += count; // 累加数量
|
|
labels.push(status); // 保存时间戳
|
|
counts.push(cumulativeCount); // 保存累计数量
|
|
});
|
|
|
|
// 更新图表数据
|
|
chartData.labels = labels; // 设置时间戳为X轴标签
|
|
chartData.datasets[0].data = counts; // 设置累计的订单数量为Y轴数据
|
|
|
|
// 更新图表
|
|
orderChart.update();
|
|
})
|
|
.catch(error => console.error('获取数据失败:', error));
|
|
}
|
|
|
|
// 每5秒更新一次数据
|
|
setInterval(fetchDataAndUpdateChart, 10000);
|
|
|
|
// 初始数据加载
|
|
fetchDataAndUpdateChart();
|
|
</script>
|
|
</body>
|
|
</html>
|