|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { lazy, Suspense, useEffect, useRef } from "react"; |
| 3 | + |
| 4 | +const ForceGraph2D = lazy(() => import("react-force-graph-2d")); |
| 5 | + |
| 6 | +interface NodeType { |
| 7 | + id: string; |
| 8 | + name: string; |
| 9 | + val: number; |
| 10 | + x?: number; |
| 11 | + y?: number; |
| 12 | + url?: string; |
| 13 | +} |
| 14 | + |
| 15 | +interface LinkType { |
| 16 | + source: string; |
| 17 | + target: string; |
| 18 | +} |
| 19 | + |
| 20 | +interface ForceGraphProps { |
| 21 | + graphData: { |
| 22 | + nodes: NodeType[]; |
| 23 | + links: LinkType[]; |
| 24 | + }; |
| 25 | + width?: number; |
| 26 | + height?: number; |
| 27 | +} |
| 28 | + |
| 29 | +const ForceGraph = ({ graphData, width = 500, height = 300 }: ForceGraphProps) => { |
| 30 | + const fgRef = useRef<any>(null); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + if (fgRef.current) { |
| 34 | + fgRef.current.d3Force("link").distance(120); |
| 35 | + fgRef.current.zoom(0.7); |
| 36 | + } |
| 37 | + }, [graphData]); |
| 38 | + |
| 39 | + const onNodeClick = (node: any) => { |
| 40 | + if (node.url) { |
| 41 | + window.open(node.url, "_blank"); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className="relative h-[300px] w-full overflow-hidden rounded-lg border border-gray3 bg-white"> |
| 47 | + <Suspense |
| 48 | + fallback={ |
| 49 | + <div className="flex h-full items-center justify-center">그래프를 불러오는 중...</div> |
| 50 | + } |
| 51 | + > |
| 52 | + <ForceGraph2D |
| 53 | + ref={fgRef} |
| 54 | + graphData={graphData} |
| 55 | + nodeLabel="name" |
| 56 | + nodeColor={() => "#1A1A1A"} |
| 57 | + linkColor={() => "#E5E5E5"} |
| 58 | + nodeRelSize={3} |
| 59 | + linkWidth={1} |
| 60 | + width={width} |
| 61 | + height={height} |
| 62 | + cooldownTicks={30} |
| 63 | + backgroundColor="white" |
| 64 | + nodeCanvasObject={(node, ctx, globalScale) => { |
| 65 | + if (typeof node.x === "undefined" || typeof node.y === "undefined") return; |
| 66 | + |
| 67 | + const maxLabelLength = 15; |
| 68 | + const label = |
| 69 | + node.name.length > maxLabelLength |
| 70 | + ? node.name.slice(0, maxLabelLength) + "..." |
| 71 | + : node.name; |
| 72 | + const fontSize = 12 / globalScale; |
| 73 | + const nodeSize = 5; |
| 74 | + |
| 75 | + // 동그라미 그리기 |
| 76 | + ctx.beginPath(); |
| 77 | + ctx.arc(node.x, node.y, nodeSize, 0, 2 * Math.PI); |
| 78 | + ctx.fillStyle = "#1A1A1A"; |
| 79 | + ctx.fill(); |
| 80 | + |
| 81 | + // 텍스트 배경 |
| 82 | + ctx.font = `${fontSize}px Sans-Serif`; |
| 83 | + const textWidth = ctx.measureText(label).width; |
| 84 | + const bckgDimensions = [textWidth, fontSize].map((n) => n + fontSize * 0.2); |
| 85 | + |
| 86 | + ctx.fillStyle = "rgba(255, 255, 255, 0.8)"; |
| 87 | + ctx.fillRect( |
| 88 | + node.x - bckgDimensions[0] / 2, |
| 89 | + node.y + nodeSize + 2, |
| 90 | + bckgDimensions[0], |
| 91 | + bckgDimensions[1] |
| 92 | + ); |
| 93 | + |
| 94 | + // 텍스트 그리기 |
| 95 | + ctx.textAlign = "center"; |
| 96 | + ctx.textBaseline = "middle"; |
| 97 | + ctx.fillStyle = "#1A1A1A"; |
| 98 | + ctx.fillText(label, node.x, node.y + nodeSize + 2 + bckgDimensions[1] / 2); |
| 99 | + }} |
| 100 | + onNodeClick={onNodeClick} |
| 101 | + d3AlphaDecay={0.02} |
| 102 | + d3VelocityDecay={0.3} |
| 103 | + minZoom={0.5} |
| 104 | + maxZoom={2} |
| 105 | + enablePointerInteraction={true} |
| 106 | + enableZoomInteraction={true} |
| 107 | + onEngineStop={() => { |
| 108 | + const canvas = document.querySelector("canvas"); |
| 109 | + if (canvas) { |
| 110 | + canvas.style.willChange = "auto"; |
| 111 | + } |
| 112 | + }} |
| 113 | + /> |
| 114 | + </Suspense> |
| 115 | + </div> |
| 116 | + ); |
| 117 | +}; |
| 118 | + |
| 119 | +export default ForceGraph; |
0 commit comments