import React, { useEffect, useRef } from "react";
import { TreeGraph } from "@antv/g6";
const data = {
id: "root",
label: "root",
children: [
{
id: "child1",
label: "child1",
children: [
{
id: "grandchild1",
label: "grandchild1",
},
{
id: "grandchild2",
label: "grandchild2",
},
],
},
{
id: "child2",
label: "child2",
},
],
};
const Tree: React.FC = () => {
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!containerRef.current) return;
const graph = new TreeGraph({
container: containerRef.current,
width: 800,
height: 600,
modes: {
default: ["collapse-expand"],
},
layout: {
type: "compactBox",
direction: "LR",
getId: (node) => node.id,
getHeight: () => 16,
getWidth: () => 16,
getVGap: () => 30,
getHGap: () => 100,
},
defaultNode: {
size: 26,
style: {
fill: "#C6E5FF",
stroke: "#5B8FF9",
},
},
defaultEdge: {
type: "cubic-horizontal",
style: {
stroke: "#A3B1BF",
},
},
});
graph.data(data);
graph.render();
graph.fitView();
return () => graph.destroy();
}, []);
return <div ref={containerRef} />;
};
export default Tree;