|
| 1 | +import React, { useCallback, useMemo, useState } from 'react'; |
| 2 | +import { Box, HStack, useTheme } from '@chakra-ui/react'; |
| 3 | +import { |
| 4 | + ResponsiveContainer, |
| 5 | + AreaChart, |
| 6 | + Area, |
| 7 | + XAxis, |
| 8 | + YAxis, |
| 9 | + CartesianGrid, |
| 10 | + Tooltip, |
| 11 | + type TooltipProps |
| 12 | +} from 'recharts'; |
| 13 | +import { type NameType, type ValueType } from 'recharts/types/component/DefaultTooltipContent'; |
| 14 | +import { formatNumber } from '@fastgpt/global/common/math/tools'; |
| 15 | + |
| 16 | +type XAxisConfig = { |
| 17 | + dataKey: string; |
| 18 | +}; |
| 19 | + |
| 20 | +type LineConfig = { |
| 21 | + dataKey: string; |
| 22 | + name: string; |
| 23 | + color: string; |
| 24 | + gradient?: boolean; |
| 25 | +}; |
| 26 | + |
| 27 | +type TooltipItem = { |
| 28 | + label: string; |
| 29 | + dataKey: string; |
| 30 | + color: string; |
| 31 | + formatter?: (value: number) => string; |
| 32 | + customValue?: (data: any) => number; |
| 33 | +}; |
| 34 | + |
| 35 | +type LineChartComponentProps = { |
| 36 | + data: Record<string, any>[]; |
| 37 | + title: string; |
| 38 | + HeaderRightChildren?: React.ReactNode; |
| 39 | + lines: LineConfig[]; |
| 40 | + tooltipItems?: TooltipItem[]; |
| 41 | +}; |
| 42 | + |
| 43 | +const CustomTooltip = ({ |
| 44 | + active, |
| 45 | + payload, |
| 46 | + tooltipItems |
| 47 | +}: TooltipProps<ValueType, NameType> & { tooltipItems?: TooltipItem[] }) => { |
| 48 | + const data = payload?.[0]?.payload; |
| 49 | + |
| 50 | + if (active && data && tooltipItems) { |
| 51 | + return ( |
| 52 | + <Box bg={'white'} p={3} borderRadius={'md'} border={'base'} boxShadow={'sm'}> |
| 53 | + <Box fontSize={'sm'} color={'myGray.900'} mb={2}> |
| 54 | + {data.x} |
| 55 | + </Box> |
| 56 | + {tooltipItems.map((item, index) => { |
| 57 | + const value = (() => { |
| 58 | + if (item.customValue) { |
| 59 | + return item.customValue(data); |
| 60 | + } else { |
| 61 | + return data[item.dataKey]; |
| 62 | + } |
| 63 | + })(); |
| 64 | + |
| 65 | + const displayValue = item.formatter ? item.formatter(value) : formatNumber(value); |
| 66 | + |
| 67 | + return ( |
| 68 | + <HStack key={index} fontSize={'sm'} _notLast={{ mb: 1 }}> |
| 69 | + <Box w={2} h={2} borderRadius={'full'} bg={item.color} /> |
| 70 | + <Box>{item.label}</Box> |
| 71 | + <Box>{displayValue}</Box> |
| 72 | + </HStack> |
| 73 | + ); |
| 74 | + })} |
| 75 | + </Box> |
| 76 | + ); |
| 77 | + } |
| 78 | + return null; |
| 79 | +}; |
| 80 | + |
| 81 | +const LineChartComponent = ({ |
| 82 | + data, |
| 83 | + title, |
| 84 | + HeaderRightChildren, |
| 85 | + lines, |
| 86 | + tooltipItems |
| 87 | +}: LineChartComponentProps) => { |
| 88 | + const theme = useTheme(); |
| 89 | + |
| 90 | + // Y-axis number formatter function |
| 91 | + const formatYAxisNumber = useCallback((value: number): string => { |
| 92 | + if (value >= 1000000) { |
| 93 | + return value / 1000000 + 'M'; |
| 94 | + } else if (value >= 1000) { |
| 95 | + return value / 1000 + 'K'; |
| 96 | + } |
| 97 | + return value.toString(); |
| 98 | + }, []); |
| 99 | + |
| 100 | + // Generate gradient definitions |
| 101 | + const gradientDefs = useMemo(() => { |
| 102 | + return ( |
| 103 | + <defs> |
| 104 | + {lines.map((line, index) => ( |
| 105 | + <linearGradient |
| 106 | + key={`gradient-${line.color}`} |
| 107 | + id={`gradient-${line.color}`} |
| 108 | + x1="0" |
| 109 | + y1="0" |
| 110 | + x2="0" |
| 111 | + y2="1" |
| 112 | + > |
| 113 | + <stop offset="0%" stopColor={line.color} stopOpacity={0.25} /> |
| 114 | + <stop offset="100%" stopColor={line.color} stopOpacity={0.01} /> |
| 115 | + </linearGradient> |
| 116 | + ))} |
| 117 | + </defs> |
| 118 | + ); |
| 119 | + }, [lines]); |
| 120 | + |
| 121 | + return ( |
| 122 | + <> |
| 123 | + <HStack mb={4} justifyContent={'space-between'} alignItems={'flex-start'}> |
| 124 | + <Box fontSize={'sm'} color={'myGray.900'} fontWeight={'medium'}> |
| 125 | + {title} |
| 126 | + </Box> |
| 127 | + {HeaderRightChildren && HeaderRightChildren} |
| 128 | + </HStack> |
| 129 | + <ResponsiveContainer width="100%" height={'100%'}> |
| 130 | + <AreaChart |
| 131 | + data={data} |
| 132 | + margin={{ top: 5, right: 30, left: 0, bottom: HeaderRightChildren ? 30 : 15 }} |
| 133 | + > |
| 134 | + {gradientDefs} |
| 135 | + <XAxis |
| 136 | + dataKey={'x'} |
| 137 | + tickMargin={10} |
| 138 | + tick={{ fontSize: '12px', color: theme.colors.myGray['500'], fontWeight: '500' }} |
| 139 | + interval={'preserveStartEnd'} |
| 140 | + /> |
| 141 | + <YAxis |
| 142 | + axisLine={false} |
| 143 | + tickSize={0} |
| 144 | + tickMargin={10} |
| 145 | + tick={{ fontSize: '12px', color: theme.colors.myGray['500'], fontWeight: '500' }} |
| 146 | + interval={'preserveStartEnd'} |
| 147 | + tickFormatter={formatYAxisNumber} |
| 148 | + /> |
| 149 | + <CartesianGrid strokeDasharray="3 3" horizontal={true} vertical={false} /> |
| 150 | + {tooltipItems && <Tooltip content={<CustomTooltip tooltipItems={tooltipItems} />} />} |
| 151 | + {lines.map((line, index) => ( |
| 152 | + <Area |
| 153 | + key={index} |
| 154 | + type="monotone" |
| 155 | + name={line.name} |
| 156 | + dataKey={line.dataKey} |
| 157 | + stroke={line.color} |
| 158 | + strokeWidth={2} |
| 159 | + fill={`url(#gradient-${line.color})`} |
| 160 | + dot={false} |
| 161 | + /> |
| 162 | + ))} |
| 163 | + </AreaChart> |
| 164 | + </ResponsiveContainer> |
| 165 | + </> |
| 166 | + ); |
| 167 | +}; |
| 168 | + |
| 169 | +export default LineChartComponent; |
0 commit comments