From 21cbd0b457e01263880fa0697d9aded145806cff Mon Sep 17 00:00:00 2001 From: huangjinysf Date: Sun, 4 Jan 2026 10:52:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor(RealTimeInventory):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E9=94=80=E5=94=AE=E6=95=B0=E6=8D=AE=E5=88=86=E7=BB=84?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构销售数据图表处理函数,将数据按日期分组并添加时间偏移以避免重叠。同一天的多个销售记录现在会以30分钟间隔显示,提高图表可读性。 --- src/views/plan/RealTimeInventory.vue | 94 ++++++++++++++++++---------- 1 file changed, 60 insertions(+), 34 deletions(-) diff --git a/src/views/plan/RealTimeInventory.vue b/src/views/plan/RealTimeInventory.vue index a143fcf..927ff62 100644 --- a/src/views/plan/RealTimeInventory.vue +++ b/src/views/plan/RealTimeInventory.vue @@ -825,43 +825,69 @@ const prepareInventoryChartData = () => { console.log('开始处理销售数据'); console.log('第一条销售数据示例:', salesData.value[0]); - const salesSeriesData = salesData.value.map((item, index) => { - console.log(`处理销售第${index}条数据:`, item); - - // 销售数据字段映射 + // 按日期分组销售数据 + const salesByDate = new Map(); + salesData.value.forEach((item, index) => { const timeField = item.date || item.create_time || item.createTime || item.time || item.timestamp; - const boxCountField = item.box_count || item.boxCount || item.box || item.count; - const weightField = item.weight || item.total_weight || item.totalWeight; - - console.log('销售字段映射检查:', { - timeField, - boxCountField, - weightField - }); - - if (!timeField) { - console.warn(`销售第${index}条数据缺少时间字段`); - return null; - } - - const time = new Date(timeField).getTime(); - let value; - - if (chartUnit.value === 'box') { - value = boxCountField || 0; - } else { - value = weightField || 0; - } - - console.log(`销售时间戳: ${time}, 值: ${value}`); - - if (isNaN(time) || time <= 0) { - console.warn(`销售第${index}条数据时间格式无效:`, timeField); - return null; + if (timeField) { + const dateKey = new Date(timeField).toISOString().split('T')[0]; // YYYY-MM-DD格式 + if (!salesByDate.has(dateKey)) { + salesByDate.set(dateKey, []); + } + salesByDate.get(dateKey).push(item); } + }); + + const salesSeriesData = []; + + // 处理每个日期的销售记录 + salesByDate.forEach((items, dateKey) => { + console.log(`处理日期 ${dateKey} 的 ${items.length} 条销售记录`); - return [time, value]; - }).filter(item => item !== null); // 过滤掉无效数据 + items.forEach((item, dayIndex) => { + console.log(`处理销售第${dayIndex + 1}条数据:`, item); + + // 销售数据字段映射 + const timeField = item.date || item.create_time || item.createTime || item.time || item.timestamp; + const boxCountField = item.box_count || item.boxCount || item.box || item.count; + const weightField = item.weight || item.total_weight || item.totalWeight; + + console.log('销售字段映射检查:', { + timeField, + boxCountField, + weightField + }); + + if (!timeField) { + console.warn(`销售第${dayIndex + 1}条数据缺少时间字段`); + return; + } + + // 基础时间戳 + const baseTime = new Date(timeField).getTime(); + + // 为同一天的多个记录分配不同的时间点(间隔30分钟) + const timeOffset = dayIndex * 30 * 60 * 1000; // 30分钟间隔 + const adjustedTime = baseTime + timeOffset; + + let value; + + if (chartUnit.value === 'box') { + value = boxCountField || 0; + } else { + value = weightField || 0; + } + + console.log(`销售时间戳: ${adjustedTime}, 值: ${value}`); + + if (isNaN(adjustedTime) || adjustedTime <= 0) { + console.warn(`销售第${dayIndex + 1}条数据时间格式无效:`, timeField); + return; + } + + salesSeriesData.push([adjustedTime, value]); + }); + }); console.log('销售转换后的图表数据:', salesSeriesData); console.log('销售有效数据点数量:', salesSeriesData.length);