|
| 1 | +function toSegments(data = []) { |
| 2 | + const normalized = data |
| 3 | + .map((entry, index) => ({ |
| 4 | + label: entry?.label || `Segment ${index + 1}`, |
| 5 | + value: Math.max(0, Number(entry?.value || 0)), |
| 6 | + colorClass: |
| 7 | + typeof entry?.colorClass === 'string' && entry.colorClass.trim() |
| 8 | + ? entry.colorClass.trim() |
| 9 | + : null, |
| 10 | + })) |
| 11 | + .filter((entry) => Number.isFinite(entry.value)); |
| 12 | + |
| 13 | + const total = normalized.reduce((sum, entry) => sum + entry.value, 0); |
| 14 | + |
| 15 | + return { |
| 16 | + segments: normalized, |
| 17 | + total: total > 0 ? total : 1, |
| 18 | + hasValues: total > 0, |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +const FALLBACK_DATA = [ |
| 23 | + { label: 'Docs', value: 58 }, |
| 24 | + { label: 'API', value: 24 }, |
| 25 | + { label: 'Ops', value: 18 }, |
| 26 | +]; |
| 27 | + |
| 28 | +function normalizeInputData(data) { |
| 29 | + if (Array.isArray(data) && data.length > 0) { |
| 30 | + return data; |
| 31 | + } |
| 32 | + |
| 33 | + return FALLBACK_DATA; |
| 34 | +} |
| 35 | + |
| 36 | +const DEFAULT_COLORS = [ |
| 37 | + 'text-blue-500 dark:text-blue-300', |
| 38 | + 'text-emerald-500 dark:text-emerald-300', |
| 39 | + 'text-amber-500 dark:text-amber-300', |
| 40 | + 'text-purple-500 dark:text-purple-300', |
| 41 | + 'text-rose-500 dark:text-rose-300', |
| 42 | + 'text-cyan-500 dark:text-cyan-300', |
| 43 | +]; |
| 44 | + |
| 45 | +export default function DonutChart({ |
| 46 | + data = [], |
| 47 | + size = 180, |
| 48 | + strokeWidth = 22, |
| 49 | + showLegend = true, |
| 50 | + className = '', |
| 51 | +}) { |
| 52 | + const normalizedData = normalizeInputData(data); |
| 53 | + const { segments, total, hasValues } = toSegments(normalizedData); |
| 54 | + const radius = (size - strokeWidth) / 2; |
| 55 | + const circumference = 2 * Math.PI * radius; |
| 56 | + |
| 57 | + let offset = 0; |
| 58 | + |
| 59 | + return ( |
| 60 | + <div className={`my-4 rounded-lg border border-slate-200 bg-white p-4 dark:border-slate-800 dark:bg-slate-950/40 ${className}`}> |
| 61 | + <div className="flex flex-col items-center gap-4 sm:flex-row sm:items-start"> |
| 62 | + <svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} role="img" aria-label="Donut chart"> |
| 63 | + <circle |
| 64 | + cx={size / 2} |
| 65 | + cy={size / 2} |
| 66 | + r={radius} |
| 67 | + fill="none" |
| 68 | + stroke="currentColor" |
| 69 | + className="text-slate-200 dark:text-slate-800" |
| 70 | + strokeWidth={strokeWidth} |
| 71 | + /> |
| 72 | + |
| 73 | + {hasValues |
| 74 | + ? segments.map((segment, index) => { |
| 75 | + const segmentLength = (segment.value / total) * circumference; |
| 76 | + const strokeDasharray = `${segmentLength} ${circumference - segmentLength}`; |
| 77 | + const strokeDashoffset = -offset; |
| 78 | + offset += segmentLength; |
| 79 | + |
| 80 | + return ( |
| 81 | + <circle |
| 82 | + key={`${segment.label}-${index}`} |
| 83 | + cx={size / 2} |
| 84 | + cy={size / 2} |
| 85 | + r={radius} |
| 86 | + fill="none" |
| 87 | + stroke="currentColor" |
| 88 | + className={segment.colorClass || DEFAULT_COLORS[index % DEFAULT_COLORS.length]} |
| 89 | + strokeWidth={strokeWidth} |
| 90 | + strokeDasharray={strokeDasharray} |
| 91 | + strokeDashoffset={strokeDashoffset} |
| 92 | + transform={`rotate(-90 ${size / 2} ${size / 2})`} |
| 93 | + strokeLinecap="butt" |
| 94 | + /> |
| 95 | + ); |
| 96 | + }) |
| 97 | + : null} |
| 98 | + </svg> |
| 99 | + |
| 100 | + {showLegend ? ( |
| 101 | + <div className="min-w-44 space-y-2 text-sm"> |
| 102 | + {segments.map((segment, index) => { |
| 103 | + const colorClass = segment.colorClass || DEFAULT_COLORS[index % DEFAULT_COLORS.length]; |
| 104 | + const pct = hasValues ? Math.round((segment.value / total) * 100) : 0; |
| 105 | + |
| 106 | + return ( |
| 107 | + <div key={`${segment.label}-legend-${index}`} className="flex items-center justify-between gap-3"> |
| 108 | + <span className="inline-flex min-w-0 items-center gap-2"> |
| 109 | + <span className={`h-2.5 w-2.5 rounded-full bg-current ${colorClass}`} /> |
| 110 | + <span className="truncate text-slate-700 dark:text-slate-200">{segment.label}</span> |
| 111 | + </span> |
| 112 | + <span className="text-slate-500 dark:text-slate-400">{pct}%</span> |
| 113 | + </div> |
| 114 | + ); |
| 115 | + })} |
| 116 | + </div> |
| 117 | + ) : null} |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + ); |
| 121 | +} |
0 commit comments