92 lines
3.0 KiB
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: 'bar', // 使用条形图
|
|
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/ordernamecount') // 替换为你的实际API地址
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
// 统计每个订单名称的数量
|
|
let orderNames = {};
|
|
data.forEach(order => {
|
|
const name = order.order_name; // 获取订单名称
|
|
const count = order.order_name_count; // 获取订单数量
|
|
|
|
// 累加相同订单名称的数量
|
|
if (orderNames[name]) {
|
|
orderNames[name] += count;
|
|
} else {
|
|
orderNames[name] = count;
|
|
}
|
|
});
|
|
|
|
// 更新图表数据
|
|
chartData.labels = Object.keys(orderNames); // X轴标签为订单名称
|
|
chartData.datasets[0].data = Object.values(orderNames); // Y轴数据为订单数量
|
|
|
|
// 更新图表
|
|
orderChart.update();
|
|
})
|
|
.catch(error => console.error('获取数据失败:', error));
|
|
}
|
|
|
|
// 每5秒更新一次数据
|
|
setInterval(fetchDataAndUpdateChart, 5000);
|
|
|
|
// 初始数据加载
|
|
fetchDataAndUpdateChart();
|
|
</script>
|
|
</body>
|
|
</html>
|