/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ 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: item.amount || 0, count: 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) => `¥${(datum?.value || 0).toFixed(2)}`, }, { 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 + (item.amount || 0), 0, ); return ( {icon} {title} } styles={{ body: { padding: '16px' } }} > {pieSpec ? (
{channelList.map((item, index) => (
{item.payment_method ? t(item.payment_method) : t('未知')}
¥{(item.amount || 0).toFixed(2)} ({item.percent?.toFixed(1)}%)
))}
{t('合计')} ¥{totalAmount.toFixed(2)}
) : (
{t('暂无数据')}
)}
); }; export default TopupChannelChart;