Files
monkey-ai-web/src/components/analytics/ModelDistribution.jsx
T
zhanjunhao 8a48765a46 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

125 lines
3.2 KiB
React

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 (
<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' } }}
>
{pieSpec ? (
<div>
<VChart
spec={pieSpec}
option={{ mode: 'desktop-browser' }}
style={{ height: 200 }}
/>
<div className='mt-4 space-y-2'>
{topModels.map((item, index) => (
<div key={index} className='flex items-center gap-2'>
<span className='text-xs text-gray-500 w-4'>{index + 1}</span>
<span
className='text-sm flex-1 truncate'
title={item.model_name || t('未知')}
>
{item.model_name || t('未知')}
</span>
<span className='text-xs text-gray-500'>
{formatPercent(item.percent, 1)}
</span>
</div>
))}
</div>
</div>
) : (
<div className='h-[300px] flex items-center justify-center text-gray-400'>
{t('暂无数据')}
</div>
)}
</Card>
);
};
export default ModelDistribution;