/* 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'; 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 ( {icon} {title} } styles={{ body: { padding: '16px' } }} > {spec ? ( ) : (
{t('暂无数据')}
)}
); }; export default TrendCharts;