import React, { useCallback, useMemo } from 'react'; import { Box, Flex, HStack, useTheme } from '@chakra-ui/react'; import { ResponsiveContainer, XAxis, YAxis, CartesianGrid, Tooltip, type TooltipProps, BarChart, Bar } from 'recharts'; import { type NameType, type ValueType } from 'recharts/types/component/DefaultTooltipContent'; import { formatNumber } from '@fastgpt/global/common/math/tools'; import { useTranslation } from 'next-i18next'; import QuestionTip from '../MyTooltip/QuestionTip'; type BarConfig = { dataKey: string; name: string; color: string; stackId?: string; }; type TooltipItem = { label: string; dataKey: string; color: string; formatter?: (value: number) => string; customValue?: (data: Record) => number; }; type BarChartComponentProps = { data: Record[]; title: string; description?: string; HeaderRightChildren?: React.ReactNode; bars: BarConfig[]; tooltipItems?: TooltipItem[]; blur?: boolean; }; const CustomTooltip = ({ active, payload, tooltipItems }: TooltipProps & { tooltipItems?: TooltipItem[] }) => { const data = payload?.[0]?.payload; if (!active || !data || !tooltipItems) { return null; } return ( {data.xLabel || data.x} {tooltipItems.map((item, index) => { const value = item.customValue ? item.customValue(data) : data[item.dataKey]; const displayValue = item.formatter ? item.formatter(value) : formatNumber(value); return ( {item.label} {displayValue.toLocaleString()} ); })} ); }; const BarChartComponent = ({ data, title, description, HeaderRightChildren, bars, tooltipItems, blur = false }: BarChartComponentProps) => { const theme = useTheme(); const { t } = useTranslation(); // Y-axis number formatter function const formatYAxisNumber = useCallback((value: number): string => { if (value >= 1000000) { return value / 1000000 + 'M'; } else if (value >= 1000) { return value / 1000 + 'K'; } return value.toString(); }, []); return ( <> {title} {HeaderRightChildren} {tooltipItems && } />} {bars.map((bar) => ( ))} ); }; export default BarChartComponent;