Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 6x 6x 15x 15x 5x 10x 10x 8x 10x 10x 6x 6x 2x 4x 4x | import type { ReactNode } from 'react';
import type { TooltipProps } from 'recharts';
import { cn } from '@/lib/utils';
import { Text } from '@/components/text';
// Types for tooltip data
export interface TooltipData {
averageScore?: {
current: number;
total: number;
};
[key: string]: any;
}
export interface ChartTooltipProps extends TooltipProps<any, any> {
title?: string;
formatValue?: (data: TooltipData) => string;
className?: string;
contentClassName?: string;
titleClassName?: string;
valueClassName?: string;
wrapperClassName?: string;
}
// Default tooltip styles
const defaultTooltipStyles = {
wrapper: ['relative'],
content: [
// Appearance
'rounded-lg border bg-white shadow-lg text-center',
// Spacing
'p-3',
],
title: ['text-xs text-default mb-1'],
value: [
'text-lg font-bold bg-gradient-horizontal bg-clip-text text-transparent text-center',
],
};
// Reusable Chart Tooltip Component
export const ChartTooltip = ({
active,
payload,
coordinate,
title = 'Average Score',
formatValue,
className,
contentClassName,
titleClassName,
valueClassName,
wrapperClassName,
}: ChartTooltipProps) => {
const tooltipWrapperStyles = {
position: 'absolute' as const,
left: coordinate?.x,
top: 0,
transform: 'translateX(-50%)',
zIndex: 9999,
pointerEvents: 'none' as const,
};
if (
!active ||
!payload ||
!payload.length ||
!coordinate ||
coordinate.x === undefined ||
coordinate.y === undefined
) {
return null;
}
const data = payload[0]?.payload as TooltipData;
// Default format function
const defaultFormatValue = (data: TooltipData): string => {
return data.averageScore
? `${data.averageScore.current}/${data.averageScore.total}`
: 'N/A';
};
const value = formatValue ? formatValue(data) : defaultFormatValue(data);
return (
<div
className={cn(
defaultTooltipStyles.wrapper.join(' '),
wrapperClassName,
className
)}
style={tooltipWrapperStyles}
>
<div
className={cn(defaultTooltipStyles.content.join(' '), contentClassName)}
style={{ minWidth: '200px' }}
>
<Text
size="xs"
className={cn(defaultTooltipStyles.title.join(' '), titleClassName)}
>
{title}
</Text>
<Text
size="lg"
weight="bold"
className={cn(defaultTooltipStyles.value.join(' '), valueClassName)}
>
{value}
</Text>
</div>
</div>
);
};
// Alternative simple tooltip for different use cases
export interface SimpleTooltipProps extends TooltipProps<any, any> {
formatValue?: (data: any) => ReactNode;
className?: string;
}
export const SimpleTooltip = ({
active,
payload,
label,
formatValue,
className,
}: SimpleTooltipProps) => {
if (!active || !payload || !payload.length) {
return null;
}
const data = payload[0]?.payload;
return (
<div
className={cn(
'bg-background/95 border rounded-lg shadow-lg p-2 text-sm',
className
)}
>
{label && (
<Text weight="medium" className="mb-1">
{label}
</Text>
)}
{formatValue ? (
formatValue(data)
) : (
<Text variant="muted">{payload[0]?.value}</Text>
)}
</div>
);
};
export default ChartTooltip;
|