Files
monkey-ai-web/src/components/analytics/TopupChannelChart.jsx
T
2026-10-15 16:33:46 +08:00

162 lines
4.5 KiB
React

/*
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 <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useMemo } from 'react';
import { Card } from '@douyinfe/semi-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('未知'),
value: item.amount || 0,
count: item.count || 0,
}));
// 根据支付渠道名称生成颜色数组
const colorList = chartData.map((item, index) => {
return (
CHANNEL_COLORS[item.name] ||
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) => `${datum?.count || 0} ${t('笔')}`,
},
],
},
},
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 (
<Card
className='shadow-sm hover:shadow-md transition-shadow'
title={
<div className='flex items-center gap-2'>
<span className='text-brand'>{icon}</span>
<span className='font-semibold'>{title}</span>
</div>
}
bodyStyle={{ padding: '16px' }}
>
{pieSpec ? (
<div>
<VChart
spec={pieSpec}
option={{ mode: 'desktop-browser' }}
style={{ height: 200 }}
/>
<div className='mt-4 space-y-2'>
{channelList.map((item, index) => (
<div
key={index}
className='flex items-center justify-between text-sm py-1 border-b border-gray-100 dark:border-gray-700 last:border-0'
>
<span className='text-gray-600 dark:text-gray-400'>
{item.payment_method}
</span>
<div className='text-right'>
<span className='font-medium'>
¥{(item.amount || 0).toFixed(2)}
</span>
<span className='text-gray-400 text-xs ml-2'>
({item.percent?.toFixed(1)}%)
</span>
</div>
</div>
))}
<div className='flex items-center justify-between text-sm pt-2 font-semibold'>
<span>{t('合计')}</span>
<span>¥{totalAmount.toFixed(2)}</span>
</div>
</div>
</div>
) : (
<div className='h-[300px] flex items-center justify-center text-gray-400'>
{t('暂无数据')}
</div>
)}
</Card>
);
};
export default TopupChannelChart;