import { I18N_TECHNICAL_TEXT } from '../../i18n/technicalText.js'; import { formatNumber, formatPercent, toFiniteNumber, } from '../../helpers/safeDisplay'; import React, { useMemo } from 'react'; import { Card } from '@/components/ui'; import { VChart } from '@visactor/react-vchart'; // 支付渠道颜色映射:支付宝蓝色、微信绿色 const CHANNEL_COLORS = { 支付宝: '#3b82f6', // 蓝色 微信支付: '#10b981', // 绿色 Stripe: '#8b5cf6', // 紫色 PayPal: '#0070ba', Creem: '#f59e0b', // 橙色 易支付: '#ef4444', // 红色 }; const DEFAULT_COLORS = [ '#3b82f6', '#10b981', '#8b5cf6', '#f59e0b', '#ef4444', '#06b6d4', '#f97316', ]; const TopupChannelChart = ({ data, title, icon, t }) => { const pieSpec = useMemo(() => { if (!data || data.length === 0) return null; const chartData = data.map((item) => ({ name: item.payment_method ? t(item.payment_method) : t('未知'), rawName: item.payment_method, value: toFiniteNumber(item.amount) ?? 0, count: toFiniteNumber(item.count) ?? 0, })); // 根据支付渠道名称生成颜色数组 const colorList = chartData.map((item, index) => { return ( CHANNEL_COLORS[item.rawName] || DEFAULT_COLORS[index % DEFAULT_COLORS.length] ); }); return { type: 'pie', data: [{ id: 'topupChannel', values: chartData }], valueField: 'value', categoryField: 'name', outerRadius: 0.8, innerRadius: 0.5, padAngle: 0.02, pie: { style: { cornerRadius: 4, }, }, tooltip: { mark: { content: [ { key: (datum) => datum?.name, value: (datum) => `¥${formatNumber(datum?.value, 2, '0.00')}`, }, { key: () => t('笔数'), value: (datum) => t('{{n}} 笔', { n: datum?.count || 0, count: datum?.count || 0, }), }, ], }, }, legends: { visible: true, position: 'bottom', orient: 'horizontal', }, label: { visible: true, position: 'outside', content: '{name}: {value:.1%}', }, color: colorList, }; }, [data, t]); // 统计列表 const channelList = data || []; const totalAmount = channelList.reduce( (sum, item) => sum + (toFiniteNumber(item.amount) ?? 0), 0, ); return ( {icon} {title} } styles={{ body: { padding: '16px' } }} > {pieSpec ? (
{channelList.map((item, index) => (
{item.payment_method ? t(item.payment_method) : t('未知')}
¥{formatNumber(item.amount, 2, '0.00')} ({formatPercent(item.percent, 1).replace('%', '')} {I18N_TECHNICAL_TEXT.PERCENT_CLOSE}
))}
{t('合计')} ¥{formatNumber(totalAmount, 2, '0.00')}
) : (
{t('暂无数据')}
)}
); }; export default TopupChannelChart;