2aa9d73325
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
239 lines
6.1 KiB
React
239 lines
6.1 KiB
React
import React, { useMemo } from 'react';
|
|
import { Card } from '@/components/ui';
|
|
import { VChart } from '@visactor/react-vchart';
|
|
import { formatCurrency } from '@/helpers/safeDisplay';
|
|
|
|
const TrendCharts = ({ title, icon, data, type, t }) => {
|
|
const spec = useMemo(() => {
|
|
if (!data || data.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
if (type === 'user') {
|
|
// 用户趋势图
|
|
const chartData = data.flatMap((item) => [
|
|
{ date: item.date, type: t('新增用户'), value: item.new_users || 0 },
|
|
{ date: item.date, type: t('活跃用户'), value: item.active_users || 0 },
|
|
{ date: item.date, type: t('付费用户'), value: item.paid_users || 0 },
|
|
]);
|
|
|
|
return {
|
|
type: 'line',
|
|
data: [{ id: 'userTrend', values: chartData }],
|
|
xField: 'date',
|
|
yField: 'value',
|
|
seriesField: 'type',
|
|
point: {
|
|
visible: true,
|
|
size: 4,
|
|
},
|
|
line: {
|
|
style: {
|
|
lineWidth: 2,
|
|
},
|
|
},
|
|
legends: {
|
|
visible: true,
|
|
orient: 'top',
|
|
position: 'middle',
|
|
padding: { top: 0, bottom: 16, left: 0, right: 0 },
|
|
layoutType: 'normal',
|
|
},
|
|
tooltip: {
|
|
visible: true,
|
|
mark: {
|
|
visible: true,
|
|
content: [
|
|
{
|
|
key: (datum) => datum?.type,
|
|
value: (datum) =>
|
|
t('{{n}} 人', {
|
|
n: (datum?.value || 0).toLocaleString(),
|
|
count: datum?.value || 0,
|
|
}),
|
|
},
|
|
],
|
|
},
|
|
dimension: {
|
|
visible: true,
|
|
content: [
|
|
{
|
|
key: (datum) => datum?.type,
|
|
value: (datum) =>
|
|
t('{{n}} 人', {
|
|
n: (datum?.value || 0).toLocaleString(),
|
|
count: datum?.value || 0,
|
|
}),
|
|
},
|
|
],
|
|
},
|
|
},
|
|
color: ['#3b82f6', '#10b981', '#f59e0b'],
|
|
crosshair: {
|
|
xField: { visible: true, line: { visible: true } },
|
|
yField: { visible: false },
|
|
},
|
|
axes: [
|
|
{
|
|
orient: 'bottom',
|
|
label: {
|
|
style: {
|
|
angle: -45,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
orient: 'left',
|
|
label: {
|
|
formatMethod: (v) => Math.round(v),
|
|
},
|
|
},
|
|
],
|
|
};
|
|
} else {
|
|
// 收支趋势图 - 双轴
|
|
const topupKey = t('充值金额');
|
|
const consumeKey = t('消耗额度');
|
|
const chartData = data.map((item) => ({
|
|
date: item.date,
|
|
[topupKey]: item.topup_amount || 0,
|
|
[consumeKey]: (item.consume_quota || 0) / 500000, // 转换为美元单位
|
|
rawConsume: item.consume_quota || 0, // 保留原始值用于 tooltip
|
|
}));
|
|
|
|
return {
|
|
type: 'common',
|
|
data: [{ id: 'financeTrend', values: chartData }],
|
|
series: [
|
|
{
|
|
type: 'bar',
|
|
id: 'topup',
|
|
name: topupKey,
|
|
dataIndex: 0,
|
|
xField: 'date',
|
|
yField: topupKey,
|
|
bar: {
|
|
style: {
|
|
fill: '#3b82f6',
|
|
cornerRadius: [4, 4, 0, 0],
|
|
},
|
|
},
|
|
tooltip: {
|
|
visible: true,
|
|
mark: {
|
|
content: [
|
|
{
|
|
key: () => topupKey,
|
|
value: (datum) => formatCurrency(datum?.[topupKey]),
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
type: 'line',
|
|
id: 'consume',
|
|
name: consumeKey,
|
|
dataIndex: 0,
|
|
xField: 'date',
|
|
yField: consumeKey,
|
|
point: {
|
|
visible: true,
|
|
size: 4,
|
|
style: {
|
|
fill: '#f59e0b',
|
|
},
|
|
},
|
|
line: {
|
|
style: {
|
|
stroke: '#f59e0b',
|
|
lineWidth: 2,
|
|
},
|
|
},
|
|
tooltip: {
|
|
visible: true,
|
|
mark: {
|
|
content: [
|
|
{
|
|
key: () => consumeKey,
|
|
value: (datum) => formatCurrency(datum?.[consumeKey]),
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
legends: {
|
|
visible: true,
|
|
orient: 'top',
|
|
position: 'middle',
|
|
padding: { top: 0, bottom: 16, left: 0, right: 0 },
|
|
layoutType: 'normal',
|
|
},
|
|
tooltip: {
|
|
visible: true,
|
|
dimension: {
|
|
visible: true,
|
|
},
|
|
},
|
|
crosshair: {
|
|
xField: { visible: true, line: { visible: true } },
|
|
},
|
|
axes: [
|
|
{
|
|
orient: 'bottom',
|
|
label: {
|
|
style: {
|
|
angle: -45,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
orient: 'left',
|
|
title: {
|
|
visible: true,
|
|
text: t('充值金额 (¥)'),
|
|
},
|
|
seriesIndex: [0],
|
|
},
|
|
{
|
|
orient: 'right',
|
|
title: {
|
|
visible: true,
|
|
text: t('消耗额度 ($)'),
|
|
},
|
|
seriesIndex: [1],
|
|
},
|
|
],
|
|
};
|
|
}
|
|
}, [data, type, t]);
|
|
|
|
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>
|
|
}
|
|
styles={{ body: { padding: '16px' } }}
|
|
>
|
|
{spec ? (
|
|
<VChart
|
|
spec={spec}
|
|
option={{ mode: 'desktop-browser' }}
|
|
style={{ height: 300 }}
|
|
/>
|
|
) : (
|
|
<div className='h-[300px] flex items-center justify-center text-gray-400'>
|
|
{t('暂无数据')}
|
|
</div>
|
|
)}
|
|
</Card>
|
|
);
|
|
};
|
|
|
|
export default TrendCharts;
|