mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
feat: 优化经营趋势图表
This commit is contained in:
+296
-63
@@ -2,7 +2,7 @@
|
||||
import type { EchartsUIType } from '@vben/plugins/echarts';
|
||||
import type { WorkbenchVO } from '#/api/ecommerce/workbench/types';
|
||||
|
||||
import { nextTick, onMounted, ref, watch } from 'vue';
|
||||
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
|
||||
|
||||
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
|
||||
|
||||
@@ -20,132 +20,365 @@ const chartOverviewShell =
|
||||
const financeChartRef = ref<EchartsUIType>();
|
||||
const { renderEcharts: renderFinanceTrend } = useEcharts(financeChartRef);
|
||||
|
||||
let darkObserver: MutationObserver | null = null;
|
||||
let renderTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let isUnmounted = false;
|
||||
|
||||
const hasData = computed(
|
||||
() =>
|
||||
props.trend.length > 0 &&
|
||||
props.trend.some(
|
||||
(d) =>
|
||||
Number(d.salesYuan ?? 0) !== 0 ||
|
||||
Number(d.profitYuan ?? 0) !== 0 ||
|
||||
Number(d.netProfitYuan ?? 0) !== 0,
|
||||
),
|
||||
);
|
||||
|
||||
const periodSummary = computed(() => {
|
||||
let sales = 0;
|
||||
let gross = 0;
|
||||
let net = 0;
|
||||
for (const d of props.trend) {
|
||||
sales += Number(d.salesYuan ?? 0);
|
||||
gross += Number(d.profitYuan ?? 0);
|
||||
net += Number(d.netProfitYuan ?? 0);
|
||||
}
|
||||
const netMarginPct =
|
||||
sales > 0 ? Number(((net / sales) * 100).toFixed(1)) : null;
|
||||
return { sales, gross, net, netMarginPct };
|
||||
});
|
||||
|
||||
function pctOfSales(part: number, sales: number): string {
|
||||
if (!Number.isFinite(sales) || sales <= 0) return '—';
|
||||
return `${((part / sales) * 100).toFixed(1)}%`;
|
||||
}
|
||||
|
||||
function barGradient(isDark: boolean, color: string, colorEnd: string) {
|
||||
return {
|
||||
type: 'linear' as const,
|
||||
x: 0,
|
||||
y: 0,
|
||||
x2: 0,
|
||||
y2: 1,
|
||||
colorStops: [
|
||||
{ offset: 0, color },
|
||||
{ offset: 1, color: isDark ? colorEnd : colorEnd },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
function drawFinanceTrend() {
|
||||
if (!financeChartRef.value) return;
|
||||
if (!financeChartRef.value || !hasData.value) return;
|
||||
try {
|
||||
const isDark = document.documentElement.classList.contains('dark');
|
||||
const axisLabel = isDark ? 'hsl(240deg 5% 64.9%)' : '#64748b';
|
||||
const split = isDark ? 'hsl(240deg 3.7% 22%)' : '#f1f5f9';
|
||||
const dates = props.trend.map((d) => d.date);
|
||||
|
||||
const salesWan = props.trend.map((d) => yuanToWanNum(Number(d.salesYuan ?? 0)));
|
||||
const grossWan = props.trend.map((d) => yuanToWanNum(Number(d.profitYuan ?? 0)));
|
||||
const netWan = props.trend.map((d) => yuanToWanNum(Number(d.netProfitYuan ?? 0)));
|
||||
const refundWan = props.trend.map((d) => yuanToWanNum(Number(d.refundYuan ?? 0)));
|
||||
const netMarginPct = props.trend.map((d) => {
|
||||
const sales = Number(d.salesYuan ?? 0);
|
||||
const net = Number(d.netProfitYuan ?? 0);
|
||||
if (sales <= 0) return null;
|
||||
return Number(((net / sales) * 100).toFixed(2));
|
||||
});
|
||||
|
||||
const showDataZoom = props.trend.length > 14;
|
||||
|
||||
renderFinanceTrend({
|
||||
tooltip: {
|
||||
trigger: 'axis',
|
||||
axisPointer: { type: 'cross', crossStyle: { color: isDark ? '#475569' : '#94a3b8' } },
|
||||
backgroundColor: isDark ? 'hsl(222.34deg 10.43% 12.27%)' : '#fff',
|
||||
textStyle: { color: isDark ? 'hsl(0deg 0% 95%)' : '#0f172a' },
|
||||
formatter: (params: any) => {
|
||||
borderColor: isDark ? 'hsl(240deg 3.7% 22%)' : '#e2e8f0',
|
||||
textStyle: { color: isDark ? 'hsl(0deg 0% 95%)' : '#0f172a', fontSize: 12 },
|
||||
formatter: (params: unknown) => {
|
||||
if (!Array.isArray(params) || params.length === 0) return '';
|
||||
const idx = params[0]?.dataIndex ?? 0;
|
||||
const idx = Number((params[0] as { dataIndex?: number })?.dataIndex ?? 0);
|
||||
const row = props.trend[idx];
|
||||
if (!row) return '';
|
||||
const lines = params.map((p: any) => {
|
||||
const name = String(p.seriesName ?? '');
|
||||
let yuan = Number(p.value) * 10_000;
|
||||
if (name.includes('实际销售') || name.startsWith('GMV'))
|
||||
yuan = Number(row.salesYuan ?? 0);
|
||||
else if (name.startsWith('毛利')) yuan = Number(row.profitYuan ?? 0);
|
||||
else if (name.startsWith('净利')) yuan = Number(row.netProfitYuan ?? 0);
|
||||
else if (name.startsWith('退款')) yuan = Number(row.refundYuan ?? 0);
|
||||
return `${p.marker ?? ''}${p.seriesName}: ${formatWanWithYuanDetail(yuan)}`;
|
||||
});
|
||||
return `${params[0]?.axisValueLabel ?? row.date}<br/>${lines.join('<br/>')}`;
|
||||
const sales = Number(row.salesYuan ?? 0);
|
||||
const gross = Number(row.profitYuan ?? 0);
|
||||
const net = Number(row.netProfitYuan ?? 0);
|
||||
const refund = Number(row.refundYuan ?? 0);
|
||||
const title = String(
|
||||
(params[0] as { axisValueLabel?: string })?.axisValueLabel ?? row.date,
|
||||
);
|
||||
const lines: string[] = [
|
||||
`<div style="font-weight:800;margin-bottom:6px">${title}</div>`,
|
||||
`<span style="color:${isDark ? '#60a5fa' : '#1d4ed8'}">●</span> 实际销售额:${formatWanWithYuanDetail(sales)}`,
|
||||
`<span style="color:${isDark ? '#34d399' : '#059669'}">●</span> 毛利:${formatWanWithYuanDetail(gross)} <span style="opacity:0.75">(毛利率 ${pctOfSales(gross, sales)})</span>`,
|
||||
`<span style="color:${isDark ? '#c4b5fd' : '#6d28d9'}">●</span> 净利:${formatWanWithYuanDetail(net)} <span style="opacity:0.75">(净利率 ${pctOfSales(net, sales)})</span>`,
|
||||
];
|
||||
if (refund > 0) {
|
||||
lines.push(
|
||||
`<span style="color:${isDark ? '#fb7185' : '#e11d48'}">●</span> 退款:${formatWanWithYuanDetail(refund)}`,
|
||||
);
|
||||
}
|
||||
return lines.join('<br/>');
|
||||
},
|
||||
},
|
||||
legend: {
|
||||
top: 0,
|
||||
textStyle: { color: axisLabel, fontWeight: 700 },
|
||||
data: ['实际销售额(万)', '毛利(万)', '净利(万)', '退款(万)'],
|
||||
itemWidth: 12,
|
||||
itemHeight: 8,
|
||||
textStyle: { color: axisLabel, fontWeight: 700, fontSize: 11 },
|
||||
data: [
|
||||
'实际销售额',
|
||||
'毛利',
|
||||
'净利',
|
||||
'退款',
|
||||
'净利率',
|
||||
],
|
||||
selected: {
|
||||
退款: props.trend.some((d) => Number(d.refundYuan ?? 0) > 0),
|
||||
},
|
||||
},
|
||||
grid: {
|
||||
left: '3%',
|
||||
right: '3%',
|
||||
bottom: '3%',
|
||||
top: 64,
|
||||
left: '2%',
|
||||
right: '4%',
|
||||
bottom: showDataZoom ? 48 : '3%',
|
||||
top: 72,
|
||||
containLabel: true,
|
||||
},
|
||||
dataZoom: showDataZoom
|
||||
? [
|
||||
{
|
||||
type: 'inside',
|
||||
start: Math.max(0, 100 - (14 / props.trend.length) * 100),
|
||||
end: 100,
|
||||
},
|
||||
{
|
||||
type: 'slider',
|
||||
height: 18,
|
||||
bottom: 8,
|
||||
borderColor: 'transparent',
|
||||
backgroundColor: isDark ? 'hsl(240deg 3.7% 15.9%)' : '#f8fafc',
|
||||
fillerColor: isDark ? 'hsl(217.2deg 32.6% 25%)' : '#e2e8f0',
|
||||
handleStyle: { color: isDark ? '#60a5fa' : '#3b82f6' },
|
||||
textStyle: { color: axisLabel, fontSize: 10 },
|
||||
},
|
||||
]
|
||||
: undefined,
|
||||
xAxis: {
|
||||
type: 'category',
|
||||
data: props.trend.map((d) => d.date),
|
||||
data: dates,
|
||||
axisLine: { lineStyle: { color: split } },
|
||||
axisLabel: { color: axisLabel, fontSize: 11 },
|
||||
axisPointer: { type: 'shadow' },
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value',
|
||||
splitLine: { lineStyle: { type: 'dashed', color: split } },
|
||||
axisLabel: { color: axisLabel },
|
||||
},
|
||||
yAxis: [
|
||||
{
|
||||
type: 'value',
|
||||
name: '金额(万)',
|
||||
nameTextStyle: { color: axisLabel, fontSize: 11, fontWeight: 700 },
|
||||
splitLine: { lineStyle: { type: 'dashed', color: split } },
|
||||
axisLabel: {
|
||||
color: axisLabel,
|
||||
formatter: (v: number) => (Number.isFinite(v) ? String(v) : ''),
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'value',
|
||||
name: '净利率(%)',
|
||||
nameTextStyle: { color: axisLabel, fontSize: 11, fontWeight: 700 },
|
||||
splitLine: { show: false },
|
||||
axisLabel: {
|
||||
color: axisLabel,
|
||||
formatter: (v: number) => `${v}%`,
|
||||
},
|
||||
},
|
||||
],
|
||||
series: [
|
||||
{
|
||||
name: '实际销售额(万)',
|
||||
type: 'line',
|
||||
smooth: true,
|
||||
symbolSize: 6,
|
||||
data: props.trend.map((d) => yuanToWanNum(Number(d.salesYuan ?? 0))),
|
||||
lineStyle: { width: 2.5, color: isDark ? '#60a5fa' : '#1d4ed8' },
|
||||
itemStyle: { color: isDark ? '#60a5fa' : '#1d4ed8' },
|
||||
name: '实际销售额',
|
||||
type: 'bar',
|
||||
barMaxWidth: 28,
|
||||
itemStyle: {
|
||||
borderRadius: [6, 6, 0, 0],
|
||||
color: barGradient(
|
||||
isDark,
|
||||
isDark ? 'rgba(96, 165, 250, 0.85)' : 'rgba(37, 99, 235, 0.88)',
|
||||
isDark ? 'rgba(37, 99, 235, 0.35)' : 'rgba(147, 197, 253, 0.55)',
|
||||
),
|
||||
},
|
||||
emphasis: {
|
||||
itemStyle: {
|
||||
color: isDark ? '#93c5fd' : '#2563eb',
|
||||
},
|
||||
},
|
||||
data: salesWan,
|
||||
z: 1,
|
||||
},
|
||||
{
|
||||
name: '毛利(万)',
|
||||
name: '毛利',
|
||||
type: 'line',
|
||||
smooth: true,
|
||||
symbolSize: 6,
|
||||
data: props.trend.map((d) => yuanToWanNum(Number(d.profitYuan ?? 0))),
|
||||
smooth: 0.35,
|
||||
symbol: 'circle',
|
||||
symbolSize: 7,
|
||||
showSymbol: props.trend.length <= 31,
|
||||
lineStyle: { width: 2.5, color: isDark ? '#34d399' : '#059669' },
|
||||
itemStyle: { color: isDark ? '#34d399' : '#059669' },
|
||||
itemStyle: { color: isDark ? '#34d399' : '#059669', borderWidth: 2, borderColor: isDark ? '#0f172a' : '#fff' },
|
||||
data: grossWan,
|
||||
z: 3,
|
||||
},
|
||||
{
|
||||
name: '净利(万)',
|
||||
name: '净利',
|
||||
type: 'line',
|
||||
smooth: true,
|
||||
symbolSize: 6,
|
||||
data: props.trend.map((d) => yuanToWanNum(Number(d.netProfitYuan ?? 0))),
|
||||
lineStyle: { width: 2.5, color: isDark ? '#c4b5fd' : '#6d28d9' },
|
||||
itemStyle: { color: isDark ? '#c4b5fd' : '#6d28d9' },
|
||||
smooth: 0.35,
|
||||
symbol: 'circle',
|
||||
symbolSize: 7,
|
||||
showSymbol: props.trend.length <= 31,
|
||||
lineStyle: { width: 3, color: isDark ? '#c4b5fd' : '#6d28d9' },
|
||||
itemStyle: { color: isDark ? '#c4b5fd' : '#6d28d9', borderWidth: 2, borderColor: isDark ? '#0f172a' : '#fff' },
|
||||
areaStyle: {
|
||||
color: barGradient(
|
||||
isDark,
|
||||
isDark ? 'rgba(196, 181, 253, 0.22)' : 'rgba(109, 40, 217, 0.12)',
|
||||
'rgba(109, 40, 217, 0)',
|
||||
),
|
||||
},
|
||||
data: netWan,
|
||||
z: 4,
|
||||
},
|
||||
{
|
||||
name: '退款(万)',
|
||||
name: '退款',
|
||||
type: 'line',
|
||||
smooth: true,
|
||||
symbolSize: 6,
|
||||
data: props.trend.map((d) => yuanToWanNum(Number(d.refundYuan ?? 0))),
|
||||
lineStyle: { width: 2.5, color: isDark ? '#fb7185' : '#e11d48' },
|
||||
smooth: 0.35,
|
||||
symbol: 'none',
|
||||
lineStyle: {
|
||||
width: 2,
|
||||
type: 'dashed',
|
||||
color: isDark ? '#fb7185' : '#e11d48',
|
||||
},
|
||||
itemStyle: { color: isDark ? '#fb7185' : '#e11d48' },
|
||||
data: refundWan,
|
||||
z: 2,
|
||||
},
|
||||
{
|
||||
name: '净利率',
|
||||
type: 'line',
|
||||
yAxisIndex: 1,
|
||||
smooth: 0.35,
|
||||
symbol: 'none',
|
||||
lineStyle: {
|
||||
width: 2,
|
||||
type: 'dotted',
|
||||
color: isDark ? '#fbbf24' : '#d97706',
|
||||
},
|
||||
itemStyle: { color: isDark ? '#fbbf24' : '#d97706' },
|
||||
data: netMarginPct,
|
||||
z: 5,
|
||||
},
|
||||
],
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[WorkbenchFinanceTrendChart] render failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async function scheduleRender() {
|
||||
if (renderTimer) {
|
||||
clearTimeout(renderTimer);
|
||||
renderTimer = null;
|
||||
}
|
||||
await nextTick();
|
||||
if (isUnmounted || !hasData.value) return;
|
||||
renderTimer = setTimeout(() => {
|
||||
if (isUnmounted) return;
|
||||
drawFinanceTrend();
|
||||
}, 50);
|
||||
}
|
||||
|
||||
async function redraw() {
|
||||
await nextTick();
|
||||
drawFinanceTrend();
|
||||
await scheduleRender();
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
void redraw();
|
||||
isUnmounted = false;
|
||||
void scheduleRender();
|
||||
darkObserver = new MutationObserver(() => {
|
||||
if (hasData.value) void scheduleRender();
|
||||
});
|
||||
darkObserver.observe(document.documentElement, {
|
||||
attributes: true,
|
||||
attributeFilter: ['class'],
|
||||
});
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
isUnmounted = true;
|
||||
if (renderTimer) {
|
||||
clearTimeout(renderTimer);
|
||||
renderTimer = null;
|
||||
}
|
||||
darkObserver?.disconnect();
|
||||
darkObserver = null;
|
||||
});
|
||||
|
||||
watch(
|
||||
() => props.trend,
|
||||
() => {
|
||||
void redraw();
|
||||
void scheduleRender();
|
||||
},
|
||||
{ deep: true },
|
||||
{ deep: true, immediate: true },
|
||||
);
|
||||
|
||||
watch(hasData, (ok) => {
|
||||
if (ok) void scheduleRender();
|
||||
});
|
||||
|
||||
defineExpose({ redraw });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div :class="chartOverviewShell">
|
||||
<div
|
||||
class="border-b border-slate-100 px-6 pb-6 pt-6 md:px-8 md:pb-6 md:pt-8 dark:border-b-[hsl(240deg_3.7%_22%)]"
|
||||
class="border-b border-slate-100 px-6 pb-4 pt-6 md:px-8 md:pt-8 dark:border-b-[hsl(240deg_3.7%_22%)]"
|
||||
>
|
||||
<h3
|
||||
class="text-xl font-black text-slate-800 dark:text-[hsl(0deg_0%_95%)]"
|
||||
>
|
||||
实际销售额、毛利与净利趋势
|
||||
</h3>
|
||||
<div class="flex flex-col gap-3 sm:flex-row sm:items-end sm:justify-between">
|
||||
<div>
|
||||
<h3
|
||||
class="text-xl font-black text-slate-800 dark:text-[hsl(0deg_0%_95%)]"
|
||||
>
|
||||
经营趋势
|
||||
</h3>
|
||||
<p class="mt-1 text-xs font-bold text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">
|
||||
柱状为实际销售额,折线为毛利/净利;右轴为日净利率,便于看「赚了多少」与「赚得好不好」
|
||||
</p>
|
||||
</div>
|
||||
<div
|
||||
v-if="hasData"
|
||||
class="flex flex-wrap gap-2 text-xs font-bold"
|
||||
>
|
||||
<span
|
||||
class="rounded-lg bg-blue-50 px-2.5 py-1 text-blue-700 dark:bg-blue-950/50 dark:text-blue-300"
|
||||
>
|
||||
区间销售 {{ formatWanWithYuanDetail(periodSummary.sales) }}
|
||||
</span>
|
||||
<span
|
||||
class="rounded-lg bg-violet-50 px-2.5 py-1 text-violet-700 dark:bg-violet-950/50 dark:text-violet-300"
|
||||
>
|
||||
区间净利 {{ formatWanWithYuanDetail(periodSummary.net) }}
|
||||
<template v-if="periodSummary.netMarginPct != null">
|
||||
· {{ periodSummary.netMarginPct }}%
|
||||
</template>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="px-6 py-5 md:px-8 md:py-6">
|
||||
<div class="h-[280px] w-full">
|
||||
<div
|
||||
v-show="!hasData"
|
||||
class="flex h-[320px] items-center justify-center text-sm font-bold text-slate-400"
|
||||
>
|
||||
所选时段暂无趋势数据
|
||||
</div>
|
||||
<div v-show="hasData" class="h-[320px] w-full">
|
||||
<EchartsUI ref="financeChartRef" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -24,6 +24,7 @@ import {
|
||||
} from 'echarts/charts';
|
||||
import {
|
||||
// 数据集组件
|
||||
DataZoomComponent,
|
||||
DatasetComponent,
|
||||
GridComponent,
|
||||
LegendComponent,
|
||||
@@ -56,6 +57,7 @@ echarts.use([
|
||||
RadarChart,
|
||||
TooltipComponent,
|
||||
GridComponent,
|
||||
DataZoomComponent,
|
||||
DatasetComponent,
|
||||
TransformComponent,
|
||||
BarChart,
|
||||
|
||||
Reference in New Issue
Block a user