mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:59:19 +08:00
fix: 店铺经营效率象限图数据为空时显示异常
- quadrants 为空时 fallback 到 shopComparisons 数据 - ROI/净利润转换为数字并加 Number.isFinite 防护 - 动态计算坐标轴 min/max 防止所有点重叠在原点 - 图表加 shopName 标签显示 - tooltip 访问加 optional chaining 防护
This commit is contained in:
@@ -360,26 +360,60 @@ const renderShopBarChart = () => {
|
||||
|
||||
// 渲染店铺效率象限图
|
||||
const renderShopScatterChart = () => {
|
||||
if (!scatterChartRef.value || !overviewData.value?.shopEfficiencyQuadrants)
|
||||
return;
|
||||
if (!scatterChartRef.value || !overviewData.value) return;
|
||||
|
||||
const quadrants = overviewData.value.shopEfficiencyQuadrants;
|
||||
const data = quadrants.map((q) => [
|
||||
q.roi,
|
||||
q.netProfit / 10_000, // 转换为万元
|
||||
q.shopName,
|
||||
]);
|
||||
const quadrants = overviewData.value.shopEfficiencyQuadrants ?? [];
|
||||
const scatterSource =
|
||||
quadrants.length > 0
|
||||
? quadrants.map((q) => ({
|
||||
shopId: q.shopId,
|
||||
shopName: q.shopName,
|
||||
roi: Number(q.roi),
|
||||
netProfit: Number(q.netProfit),
|
||||
}))
|
||||
: (overviewData.value.shopComparisons ?? []).map((shop) => ({
|
||||
shopId: shop.shopId,
|
||||
shopName: shop.shopName,
|
||||
roi: Number(shop.roi),
|
||||
netProfit: Number(shop.profitAfterTax),
|
||||
}));
|
||||
|
||||
const data: Array<[number, number, string]> = scatterSource.map((q) => {
|
||||
const roi = Number.isFinite(q.roi) ? q.roi : 0;
|
||||
const netProfit = Number.isFinite(q.netProfit) ? q.netProfit : 0;
|
||||
return [roi, netProfit / 10_000, q.shopName];
|
||||
});
|
||||
|
||||
const roiValues = data.map((item) => item[0]);
|
||||
const profitValues = data.map((item) => item[1]);
|
||||
const minRoi = roiValues.length ? Math.min(...roiValues) : 0;
|
||||
const maxRoi = roiValues.length ? Math.max(...roiValues) : 1;
|
||||
const minProfit = profitValues.length ? Math.min(...profitValues) : 0;
|
||||
const maxProfit = profitValues.length ? Math.max(...profitValues) : 1;
|
||||
const roiPadding = minRoi === maxRoi ? 1 : (maxRoi - minRoi) * 0.2;
|
||||
const profitPadding =
|
||||
minProfit === maxProfit ? 1 : (maxProfit - minProfit) * 0.2;
|
||||
|
||||
renderScatterChart({
|
||||
grid: {
|
||||
left: '6%',
|
||||
right: '4%',
|
||||
top: '8%',
|
||||
bottom: '16%',
|
||||
containLabel: true,
|
||||
},
|
||||
tooltip: {
|
||||
trigger: 'item',
|
||||
formatter: (params: any) => {
|
||||
const shop = quadrants[params.dataIndex];
|
||||
const shop = scatterSource[params.dataIndex];
|
||||
if (!shop) return '';
|
||||
return `<div style="padding:4px; font-weight:bold">${shop.shopName}</div><hr/>投资回报率 (ROI): ${shop.roi.toFixed(2)}<br/>净利润: ¥${(shop.netProfit / 10_000).toFixed(2)}万`;
|
||||
},
|
||||
},
|
||||
xAxis: {
|
||||
type: 'value',
|
||||
min: minRoi - roiPadding,
|
||||
max: Math.max(maxRoi + roiPadding, 3.5),
|
||||
name: '投资回报率 (ROI)',
|
||||
nameLocation: 'middle',
|
||||
nameGap: 35,
|
||||
@@ -389,6 +423,9 @@ const renderShopScatterChart = () => {
|
||||
},
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value',
|
||||
min: Math.min(minProfit - profitPadding, 0),
|
||||
max: Math.max(maxProfit + profitPadding, 10),
|
||||
name: '税后净利润 (单位: 万元)',
|
||||
nameLocation: 'middle',
|
||||
nameGap: 50,
|
||||
@@ -402,10 +439,17 @@ const renderShopScatterChart = () => {
|
||||
type: 'scatter',
|
||||
symbolSize: 45,
|
||||
data,
|
||||
label: {
|
||||
show: true,
|
||||
position: 'top',
|
||||
color: '#334155',
|
||||
fontWeight: 700,
|
||||
formatter: (params: any) => params.data?.[2] ?? '',
|
||||
},
|
||||
itemStyle: {
|
||||
color: (params: any) => {
|
||||
const roi = params.data[0];
|
||||
const profit = params.data[1];
|
||||
const roi = params.data?.[0];
|
||||
const profit = params.data?.[1];
|
||||
if (roi >= 3.5 && profit >= 10) return '#10b981'; // 高产区
|
||||
if (roi < 3 || profit < 0) return '#f43f5e'; // 风险区
|
||||
return '#6366f1'; // 潜力区
|
||||
|
||||
Reference in New Issue
Block a user