import React, { useMemo } from 'react'; import { Card } from '@/components/ui'; import { VChart } from '@visactor/react-vchart'; import { renderQuota } from '../../helpers'; import { formatPercent } from '../../helpers/safeDisplay'; const ModelDistribution = ({ data, title, icon, t }) => { const pieSpec = useMemo(() => { if (!data || data.length === 0) return null; const chartData = data.slice(0, 8).map((item) => ({ name: item.model_name || t('未知'), value: item.quota || 0, })); // 如果有更多数据,合并为"其他" if (data.length > 8) { const otherQuota = data .slice(8) .reduce((sum, item) => sum + (item.quota || 0), 0); chartData.push({ name: t('其他'), value: otherQuota }); } return { type: 'pie', data: [{ id: 'modelDist', 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) => renderQuota(datum?.value || 0), }, ], }, }, legends: { visible: true, position: 'right', orient: 'vertical', item: { width: 100, label: { style: { fontSize: 11, }, }, }, }, label: { visible: false, }, color: [ '#3b82f6', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6', '#06b6d4', '#f97316', '#84cc16', '#94a3b8', ], }; }, [data, t]); // 排行列表 const topModels = data?.slice(0, 5) || []; return ( {icon} {title} } styles={{ body: { padding: '16px' } }} > {pieSpec ? (
{topModels.map((item, index) => (
{index + 1} {item.model_name || t('未知')} {formatPercent(item.percent, 1)}
))}
) : (
{t('暂无数据')}
)}
); }; export default ModelDistribution;