fix(company-overview): 修复店铺经营健康度分层散点图不显示的问题

- 散点图改用 shopComparisons 数据,x轴=达成率,y轴=税后盈亏,气泡大小=销售额,颜色=费比
- 添加 watch + onMounted 确保图表正确渲染
- echarts 插件补充 ScatterChart 支持
This commit is contained in:
小蟑螂
2026-04-22 15:34:56 +08:00
parent cc5058db3b
commit 41302dfa05
3 changed files with 120 additions and 44 deletions
@@ -3,7 +3,7 @@ import type { EchartsUIType } from '@vben/plugins/echarts';
import type { CompanyOverviewVO } from '#/api/ecommerce/company-overview';
import { ref } from 'vue';
import { nextTick, onMounted, ref, watch } from 'vue';
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
defineOptions({ name: 'ShopCharts' });
@@ -11,6 +11,7 @@ defineOptions({ name: 'ShopCharts' });
interface Props {
shopSalesRatios?: CompanyOverviewVO.ShopSalesRatio[];
shopComparisons?: CompanyOverviewVO.ShopComparison[];
shopEfficiencyQuadrants?: CompanyOverviewVO.ShopEfficiencyQuadrant[];
}
const props = defineProps<Props>();
@@ -24,6 +25,11 @@ const formatAmount = (amount: number) => {
return `¥${amount.toLocaleString('zh-CN')}`;
};
const safeNumber = (value: number | string | undefined) => {
const num = Number(value);
return Number.isFinite(num) ? num : 0;
};
// 渲染店铺销售占比排行图表
const renderShopBarChart = () => {
if (!barChartRef.value || !props.shopSalesRatios) return;
@@ -101,33 +107,77 @@ const renderShopBarChart = () => {
});
};
// 渲染店铺效率象限图
// 渲染店铺经营健康度分层图(效率 × 结果 × 规模)
const renderShopScatterChart = () => {
if (!scatterChartRef.value || !props.shopComparisons) return;
const scatterSource = props.shopComparisons.map((shop) => ({
shopId: shop.shopId,
shopName: shop.shopName,
roi: Number(shop.roi),
netProfit: Number(shop.profitAfterTax),
}));
const source = props.shopComparisons;
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 data: Array<[number, number, string, number, number, number, number]> =
source.map((shop) => [
// x: 销售额达成率(%)
safeNumber(shop.salesAmountAchievementRate),
// y: 税后盈亏(万元)
safeNumber(shop.profitAfterTax) / 10_000,
// label: 店铺名
shop.shopName,
// size: 销售额(元)
safeNumber(shop.salesAmount),
// color metric: 费比(0~1)
safeNumber(shop.feeRatio),
// tooltip metric: ROI
safeNumber(shop.roi),
// tooltip metric: 订单达成率
safeNumber(shop.orderCountAchievementRate),
]);
const roiValues = data.map((item) => item[0]);
if (data.length === 0) {
renderScatterChart({
title: {
text: '暂无店铺经营数据',
left: 'center',
top: 'middle',
textStyle: {
color: document.documentElement.classList.contains('dark')
? 'hsl(240deg 5% 64.9%)'
: '#64748b',
fontSize: 14,
fontWeight: 'bold',
},
},
xAxis: { show: false },
yAxis: { show: false },
series: [],
});
return;
}
const achieveValues = 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 salesValues = data.map((item) => item[3]);
const minAchieve = Math.min(...achieveValues);
const maxAchieve = Math.max(...achieveValues);
const minProfit = Math.min(...profitValues);
const maxProfit = Math.max(...profitValues);
const minSales = Math.min(...salesValues);
const maxSales = Math.max(...salesValues);
const achievePadding =
minAchieve === maxAchieve ? 10 : (maxAchieve - minAchieve) * 0.2;
const profitPadding =
minProfit === maxProfit ? 1 : (maxProfit - minProfit) * 0.2;
const calcBubbleSize = (salesAmount: number) => {
if (maxSales === minSales) return 36;
const ratio = (salesAmount - minSales) / (maxSales - minSales);
return 28 + ratio * 26;
};
const getFeeRatioColor = (feeRatio: number) => {
if (feeRatio <= 0.08) return '#10b981'; // 低费比-健康
if (feeRatio <= 0.15) return '#6366f1'; // 中费比-可控
return '#f43f5e'; // 高费比-风险
};
renderScatterChart({
grid: {
left: '6%',
@@ -139,28 +189,32 @@ const renderShopScatterChart = () => {
tooltip: {
trigger: 'item',
formatter: (params: any) => {
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)}万`;
const item = params.data;
if (!item) return '';
const feeRatioPercent = (item[4] * 100).toFixed(2);
return `<div style="padding:4px; font-weight:bold">${item[2]}</div><hr/>销售额达成率: ${item[0].toFixed(2)}%<br/>税后盈亏: ¥${item[1].toFixed(2)}万<br/>销售额: ¥${item[3].toLocaleString('zh-CN')}<br/>费比: ${feeRatioPercent}%<br/>ROI: ${item[5].toFixed(2)}<br/>订单达成率: ${item[6].toFixed(2)}%`;
},
},
xAxis: {
type: 'value',
min: minRoi - roiPadding,
max: Math.max(maxRoi + roiPadding, 3.5),
name: '投资回报率 (ROI)',
min: Math.min(minAchieve - achievePadding, 0),
max: Math.max(maxAchieve + achievePadding, 120),
name: '销售额达成率 (%)',
nameLocation: 'middle',
nameGap: 35,
axisLabel: { fontWeight: 'bold' },
axisLabel: {
fontWeight: 'bold',
formatter: '{value}%',
},
splitLine: {
lineStyle: { type: 'dashed' },
},
},
yAxis: {
type: 'value',
min: Math.min(minProfit - profitPadding, 0),
max: Math.max(maxProfit + profitPadding, 10),
name: '税后净利润 (单位: 万元)',
min: minProfit - profitPadding,
max: maxProfit + profitPadding,
name: '税后盈亏 (单位: 万元)',
nameLocation: 'middle',
nameGap: 50,
axisLabel: { fontWeight: 'bold' },
@@ -171,7 +225,7 @@ const renderShopScatterChart = () => {
series: [
{
type: 'scatter',
symbolSize: 45,
symbolSize: (val: any) => calcBubbleSize(Number(val[3] ?? 0)),
data,
label: {
show: true,
@@ -184,26 +238,38 @@ const renderShopScatterChart = () => {
formatter: (params: any) => params.data?.[2] ?? '',
},
itemStyle: {
color: (params: any) => {
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';
},
color: (params: any) => getFeeRatioColor(Number(params.data?.[4] ?? 0)),
shadowBlur: 15,
shadowColor: 'rgba(0,0,0,0.1)',
},
markLine: {
silent: true,
lineStyle: { type: 'dashed', color: '#94a3b8' },
data: [{ xAxis: 3.5 }, { yAxis: 10 }],
data: [{ xAxis: 100 }, { yAxis: 0 }],
},
},
],
});
};
const rerenderCharts = async () => {
await nextTick();
renderShopBarChart();
renderShopScatterChart();
};
watch(
() => [props.shopSalesRatios, props.shopComparisons],
() => {
rerenderCharts();
},
{ deep: true, immediate: true },
);
onMounted(() => {
rerenderCharts();
});
// 暴露方法给父组件调用
defineExpose({ renderShopBarChart, renderShopScatterChart });
</script>
@@ -226,7 +292,7 @@ defineExpose({ renderShopBarChart, renderShopScatterChart });
class="rounded-2xl border border-slate-200 dark:border-[hsl(240deg_3.7%_22%)] bg-white/90 dark:bg-[hsl(222.34deg_10.43%_12.27%)] p-8 shadow-[0_4px_12px_rgba(0,0,0,0.03)] dark:shadow-none backdrop-blur-[10px]"
>
<h3 class="mb-6 text-lg font-black text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
店铺经营效率象限 (ROI vs 净利)
店铺经营健康度分层 (达成率 vs 税后盈亏)
</h3>
<div class="h-[380px] w-full">
<EchartsUI ref="scatterChartRef" />
@@ -236,19 +302,19 @@ defineExpose({ renderShopBarChart, renderShopScatterChart });
class="flex items-center gap-2 text-xs font-bold text-slate-600 dark:text-[hsl(240deg_5%_64.9%)]"
>
<span class="h-3 w-3 rounded-full bg-emerald-500"></span>
高产区
低费比(健康)
</div>
<div
class="flex items-center gap-2 text-xs font-bold text-slate-600 dark:text-[hsl(240deg_5%_64.9%)]"
>
<span class="h-3 w-3 rounded-full bg-blue-500"></span>
潜力区
中费比(可控)
</div>
<div
class="flex items-center gap-2 text-xs font-bold text-slate-600 dark:text-[hsl(240deg_5%_64.9%)]"
>
<span class="h-3 w-3 rounded-full bg-rose-500"></span>
风险区
高费比(风险)
</div>
</div>
</div>
@@ -19,7 +19,7 @@ import { Page } from '@vben/common-ui';
import { Button, DatePicker, InputNumber, Spin } from 'ant-design-vue';
import dayjs from 'dayjs';
import { getCompanyOverviewApi, exportCompanyOverviewApi, exportCategoryOverviewApi } from '#/api/ecommerce/company-overview/apis';
import { getCompanyOverviewApi } from '#/api/ecommerce/company-overview/apis';
import { getShopPageApi } from '#/api/ecommerce/shop';
import { exportByPost } from '#/utils/export';
@@ -536,6 +536,7 @@ const navigateToShopOverview = (shopId: number) => {
ref="shopChartsRef"
:shop-sales-ratios="overviewData?.shopSalesRatios"
:shop-comparisons="overviewData?.shopComparisons"
:shop-efficiency-quadrants="overviewData?.shopEfficiencyQuadrants"
/>
<!-- 第三层:横向对比大表 -->
@@ -2,6 +2,7 @@ import type {
// 系列类型的定义后缀都为 SeriesOption
BarSeriesOption,
LineSeriesOption,
ScatterSeriesOption,
} from 'echarts/charts';
import type {
DatasetComponentOption,
@@ -12,7 +13,13 @@ import type {
} from 'echarts/components';
import type { ComposeOption } from 'echarts/core';
import { BarChart, LineChart, PieChart, RadarChart } from 'echarts/charts';
import {
BarChart,
LineChart,
PieChart,
RadarChart,
ScatterChart,
} from 'echarts/charts';
import {
// 数据集组件
DatasetComponent,
@@ -34,6 +41,7 @@ export type ECOption = ComposeOption<
| DatasetComponentOption
| GridComponentOption
| LineSeriesOption
| ScatterSeriesOption
| TitleComponentOption
| TooltipComponentOption
>;
@@ -49,6 +57,7 @@ echarts.use([
TransformComponent,
BarChart,
LineChart,
ScatterChart,
LabelLayout,
UniversalTransition,
CanvasRenderer,