diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/components/FinanceTrendChart.vue b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/components/FinanceTrendChart.vue index a907627f..7653c062 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/components/FinanceTrendChart.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/components/FinanceTrendChart.vue @@ -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(); const { renderEcharts: renderFinanceTrend } = useEcharts(financeChartRef); +let darkObserver: MutationObserver | null = null; +let renderTimer: ReturnType | 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}
${lines.join('
')}`; + 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[] = [ + `
${title}
`, + `● 实际销售额:${formatWanWithYuanDetail(sales)}`, + `● 毛利:${formatWanWithYuanDetail(gross)} (毛利率 ${pctOfSales(gross, sales)})`, + `● 净利:${formatWanWithYuanDetail(net)} (净利率 ${pctOfSales(net, sales)})`, + ]; + if (refund > 0) { + lines.push( + `● 退款:${formatWanWithYuanDetail(refund)}`, + ); + } + return lines.join('
'); }, }, 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 });