mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 01:49:19 +08:00
fix(company-overview): 修复店铺经营健康度分层散点图不显示的问题
- 散点图改用 shopComparisons 数据,x轴=达成率,y轴=税后盈亏,气泡大小=销售额,颜色=费比 - 添加 watch + onMounted 确保图表正确渲染 - echarts 插件补充 ScatterChart 支持
This commit is contained in:
+108
-42
@@ -3,7 +3,7 @@ import type { EchartsUIType } from '@vben/plugins/echarts';
|
|||||||
|
|
||||||
import type { CompanyOverviewVO } from '#/api/ecommerce/company-overview';
|
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';
|
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
|
||||||
|
|
||||||
defineOptions({ name: 'ShopCharts' });
|
defineOptions({ name: 'ShopCharts' });
|
||||||
@@ -11,6 +11,7 @@ defineOptions({ name: 'ShopCharts' });
|
|||||||
interface Props {
|
interface Props {
|
||||||
shopSalesRatios?: CompanyOverviewVO.ShopSalesRatio[];
|
shopSalesRatios?: CompanyOverviewVO.ShopSalesRatio[];
|
||||||
shopComparisons?: CompanyOverviewVO.ShopComparison[];
|
shopComparisons?: CompanyOverviewVO.ShopComparison[];
|
||||||
|
shopEfficiencyQuadrants?: CompanyOverviewVO.ShopEfficiencyQuadrant[];
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = defineProps<Props>();
|
const props = defineProps<Props>();
|
||||||
@@ -24,6 +25,11 @@ const formatAmount = (amount: number) => {
|
|||||||
return `¥${amount.toLocaleString('zh-CN')}`;
|
return `¥${amount.toLocaleString('zh-CN')}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const safeNumber = (value: number | string | undefined) => {
|
||||||
|
const num = Number(value);
|
||||||
|
return Number.isFinite(num) ? num : 0;
|
||||||
|
};
|
||||||
|
|
||||||
// 渲染店铺销售占比排行图表
|
// 渲染店铺销售占比排行图表
|
||||||
const renderShopBarChart = () => {
|
const renderShopBarChart = () => {
|
||||||
if (!barChartRef.value || !props.shopSalesRatios) return;
|
if (!barChartRef.value || !props.shopSalesRatios) return;
|
||||||
@@ -101,33 +107,77 @@ const renderShopBarChart = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// 渲染店铺效率象限图
|
// 渲染店铺经营健康度分层图(效率 × 结果 × 规模)
|
||||||
const renderShopScatterChart = () => {
|
const renderShopScatterChart = () => {
|
||||||
if (!scatterChartRef.value || !props.shopComparisons) return;
|
if (!scatterChartRef.value || !props.shopComparisons) return;
|
||||||
|
|
||||||
const scatterSource = props.shopComparisons.map((shop) => ({
|
const source = props.shopComparisons;
|
||||||
shopId: shop.shopId,
|
|
||||||
shopName: shop.shopName,
|
|
||||||
roi: Number(shop.roi),
|
|
||||||
netProfit: Number(shop.profitAfterTax),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const data: Array<[number, number, string]> = scatterSource.map((q) => {
|
const data: Array<[number, number, string, number, number, number, number]> =
|
||||||
const roi = Number.isFinite(q.roi) ? q.roi : 0;
|
source.map((shop) => [
|
||||||
const netProfit = Number.isFinite(q.netProfit) ? q.netProfit : 0;
|
// x: 销售额达成率(%)
|
||||||
return [roi, netProfit / 10_000, q.shopName];
|
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 profitValues = data.map((item) => item[1]);
|
||||||
const minRoi = roiValues.length ? Math.min(...roiValues) : 0;
|
const salesValues = data.map((item) => item[3]);
|
||||||
const maxRoi = roiValues.length ? Math.max(...roiValues) : 1;
|
const minAchieve = Math.min(...achieveValues);
|
||||||
const minProfit = profitValues.length ? Math.min(...profitValues) : 0;
|
const maxAchieve = Math.max(...achieveValues);
|
||||||
const maxProfit = profitValues.length ? Math.max(...profitValues) : 1;
|
const minProfit = Math.min(...profitValues);
|
||||||
const roiPadding = minRoi === maxRoi ? 1 : (maxRoi - minRoi) * 0.2;
|
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 =
|
const profitPadding =
|
||||||
minProfit === maxProfit ? 1 : (maxProfit - minProfit) * 0.2;
|
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({
|
renderScatterChart({
|
||||||
grid: {
|
grid: {
|
||||||
left: '6%',
|
left: '6%',
|
||||||
@@ -139,28 +189,32 @@ const renderShopScatterChart = () => {
|
|||||||
tooltip: {
|
tooltip: {
|
||||||
trigger: 'item',
|
trigger: 'item',
|
||||||
formatter: (params: any) => {
|
formatter: (params: any) => {
|
||||||
const shop = scatterSource[params.dataIndex];
|
const item = params.data;
|
||||||
if (!shop) return '';
|
if (!item) 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 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: {
|
xAxis: {
|
||||||
type: 'value',
|
type: 'value',
|
||||||
min: minRoi - roiPadding,
|
min: Math.min(minAchieve - achievePadding, 0),
|
||||||
max: Math.max(maxRoi + roiPadding, 3.5),
|
max: Math.max(maxAchieve + achievePadding, 120),
|
||||||
name: '投资回报率 (ROI)',
|
name: '销售额达成率 (%)',
|
||||||
nameLocation: 'middle',
|
nameLocation: 'middle',
|
||||||
nameGap: 35,
|
nameGap: 35,
|
||||||
axisLabel: { fontWeight: 'bold' },
|
axisLabel: {
|
||||||
|
fontWeight: 'bold',
|
||||||
|
formatter: '{value}%',
|
||||||
|
},
|
||||||
splitLine: {
|
splitLine: {
|
||||||
lineStyle: { type: 'dashed' },
|
lineStyle: { type: 'dashed' },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
yAxis: {
|
yAxis: {
|
||||||
type: 'value',
|
type: 'value',
|
||||||
min: Math.min(minProfit - profitPadding, 0),
|
min: minProfit - profitPadding,
|
||||||
max: Math.max(maxProfit + profitPadding, 10),
|
max: maxProfit + profitPadding,
|
||||||
name: '税后净利润 (单位: 万元)',
|
name: '税后盈亏 (单位: 万元)',
|
||||||
nameLocation: 'middle',
|
nameLocation: 'middle',
|
||||||
nameGap: 50,
|
nameGap: 50,
|
||||||
axisLabel: { fontWeight: 'bold' },
|
axisLabel: { fontWeight: 'bold' },
|
||||||
@@ -171,7 +225,7 @@ const renderShopScatterChart = () => {
|
|||||||
series: [
|
series: [
|
||||||
{
|
{
|
||||||
type: 'scatter',
|
type: 'scatter',
|
||||||
symbolSize: 45,
|
symbolSize: (val: any) => calcBubbleSize(Number(val[3] ?? 0)),
|
||||||
data,
|
data,
|
||||||
label: {
|
label: {
|
||||||
show: true,
|
show: true,
|
||||||
@@ -184,26 +238,38 @@ const renderShopScatterChart = () => {
|
|||||||
formatter: (params: any) => params.data?.[2] ?? '',
|
formatter: (params: any) => params.data?.[2] ?? '',
|
||||||
},
|
},
|
||||||
itemStyle: {
|
itemStyle: {
|
||||||
color: (params: any) => {
|
color: (params: any) => getFeeRatioColor(Number(params.data?.[4] ?? 0)),
|
||||||
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';
|
|
||||||
},
|
|
||||||
shadowBlur: 15,
|
shadowBlur: 15,
|
||||||
shadowColor: 'rgba(0,0,0,0.1)',
|
shadowColor: 'rgba(0,0,0,0.1)',
|
||||||
},
|
},
|
||||||
markLine: {
|
markLine: {
|
||||||
silent: true,
|
silent: true,
|
||||||
lineStyle: { type: 'dashed', color: '#94a3b8' },
|
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 });
|
defineExpose({ renderShopBarChart, renderShopScatterChart });
|
||||||
</script>
|
</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]"
|
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%)]">
|
<h3 class="mb-6 text-lg font-black text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
|
||||||
店铺经营效率象限 (ROI vs 净利)
|
店铺经营健康度分层 (达成率 vs 税后盈亏)
|
||||||
</h3>
|
</h3>
|
||||||
<div class="h-[380px] w-full">
|
<div class="h-[380px] w-full">
|
||||||
<EchartsUI ref="scatterChartRef" />
|
<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%)]"
|
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>
|
<span class="h-3 w-3 rounded-full bg-emerald-500"></span>
|
||||||
高产区
|
低费比(健康)
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
class="flex items-center gap-2 text-xs font-bold text-slate-600 dark:text-[hsl(240deg_5%_64.9%)]"
|
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>
|
<span class="h-3 w-3 rounded-full bg-blue-500"></span>
|
||||||
潜力区
|
中费比(可控)
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
class="flex items-center gap-2 text-xs font-bold text-slate-600 dark:text-[hsl(240deg_5%_64.9%)]"
|
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>
|
<span class="h-3 w-3 rounded-full bg-rose-500"></span>
|
||||||
风险区
|
高费比(风险)
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import { Page } from '@vben/common-ui';
|
|||||||
import { Button, DatePicker, InputNumber, Spin } from 'ant-design-vue';
|
import { Button, DatePicker, InputNumber, Spin } from 'ant-design-vue';
|
||||||
import dayjs from 'dayjs';
|
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 { getShopPageApi } from '#/api/ecommerce/shop';
|
||||||
import { exportByPost } from '#/utils/export';
|
import { exportByPost } from '#/utils/export';
|
||||||
|
|
||||||
@@ -536,6 +536,7 @@ const navigateToShopOverview = (shopId: number) => {
|
|||||||
ref="shopChartsRef"
|
ref="shopChartsRef"
|
||||||
:shop-sales-ratios="overviewData?.shopSalesRatios"
|
:shop-sales-ratios="overviewData?.shopSalesRatios"
|
||||||
:shop-comparisons="overviewData?.shopComparisons"
|
:shop-comparisons="overviewData?.shopComparisons"
|
||||||
|
:shop-efficiency-quadrants="overviewData?.shopEfficiencyQuadrants"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<!-- 第三层:横向对比大表 -->
|
<!-- 第三层:横向对比大表 -->
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import type {
|
|||||||
// 系列类型的定义后缀都为 SeriesOption
|
// 系列类型的定义后缀都为 SeriesOption
|
||||||
BarSeriesOption,
|
BarSeriesOption,
|
||||||
LineSeriesOption,
|
LineSeriesOption,
|
||||||
|
ScatterSeriesOption,
|
||||||
} from 'echarts/charts';
|
} from 'echarts/charts';
|
||||||
import type {
|
import type {
|
||||||
DatasetComponentOption,
|
DatasetComponentOption,
|
||||||
@@ -12,7 +13,13 @@ import type {
|
|||||||
} from 'echarts/components';
|
} from 'echarts/components';
|
||||||
import type { ComposeOption } from 'echarts/core';
|
import type { ComposeOption } from 'echarts/core';
|
||||||
|
|
||||||
import { BarChart, LineChart, PieChart, RadarChart } from 'echarts/charts';
|
import {
|
||||||
|
BarChart,
|
||||||
|
LineChart,
|
||||||
|
PieChart,
|
||||||
|
RadarChart,
|
||||||
|
ScatterChart,
|
||||||
|
} from 'echarts/charts';
|
||||||
import {
|
import {
|
||||||
// 数据集组件
|
// 数据集组件
|
||||||
DatasetComponent,
|
DatasetComponent,
|
||||||
@@ -34,6 +41,7 @@ export type ECOption = ComposeOption<
|
|||||||
| DatasetComponentOption
|
| DatasetComponentOption
|
||||||
| GridComponentOption
|
| GridComponentOption
|
||||||
| LineSeriesOption
|
| LineSeriesOption
|
||||||
|
| ScatterSeriesOption
|
||||||
| TitleComponentOption
|
| TitleComponentOption
|
||||||
| TooltipComponentOption
|
| TooltipComponentOption
|
||||||
>;
|
>;
|
||||||
@@ -49,6 +57,7 @@ echarts.use([
|
|||||||
TransformComponent,
|
TransformComponent,
|
||||||
BarChart,
|
BarChart,
|
||||||
LineChart,
|
LineChart,
|
||||||
|
ScatterChart,
|
||||||
LabelLayout,
|
LabelLayout,
|
||||||
UniversalTransition,
|
UniversalTransition,
|
||||||
CanvasRenderer,
|
CanvasRenderer,
|
||||||
|
|||||||
Reference in New Issue
Block a user