-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtree-view.tsx
More file actions
202 lines (188 loc) · 4.77 KB
/
tree-view.tsx
File metadata and controls
202 lines (188 loc) · 4.77 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React, { useMemo } from "react";
import { Group } from "@visx/group";
import { Tree, hierarchy } from "@visx/hierarchy";
import { HierarchyPointNode } from "@visx/hierarchy/lib/types";
import { LinkHorizontal } from "@visx/shape";
import { LinearGradient } from "@visx/gradient";
const peach = "#fd9b93";
const pink = "#fe6e9e";
const blue = "#03c0dc";
const green = "#26deb0";
const plum = "#71248e";
const lightpurple = "#374469";
const white = "#ffffff";
export const background = "#272b4d";
/**
* Tree data input for representing component-instance referencing data.
* @deprecated not implemented
*/
export interface TreeNodeWithComponents {
type: "tree-node-with-components";
entry: TreeNode;
components: TreeNode[];
}
export interface TreeNode {
name: string;
id?: string;
type?: string;
children?: TreeNode[];
}
type HierarchyNode = HierarchyPointNode<TreeNode>;
/** Handles rendering Root, Parent, and other Nodes. */
function Node({ node }: { node: HierarchyNode }) {
const width = 40;
const height = 20;
const centerX = -width / 2;
const centerY = -height / 2;
const isRoot = node.depth === 0;
const isParent = !!node.children;
if (isRoot) return <RootNode node={node} />;
if (isParent) return <ParentNode node={node} />;
return (
<Group top={node.x} left={node.y}>
<rect
height={height}
width={width}
y={centerY}
x={centerX}
fill={background}
stroke={green}
strokeWidth={1}
strokeDasharray="2,2"
strokeOpacity={0.6}
rx={10}
onClick={() => {
alert(`Name: "${node.data.name}"\n${node.data.id}`);
}}
/>
<text
dy=".33em"
fontSize={9}
fontFamily="Arial"
textAnchor="middle"
fill={green}
style={{ pointerEvents: "none" }}
>
{node.data.name}
</text>
<text
dy="-2em"
fontSize={4}
fontFamily="Arial"
textAnchor="middle"
style={{ pointerEvents: "none" }}
fill={white}
>
{node.data.id}
</text>
<text
dy="-1.5em"
fontSize={9}
fontFamily="Arial"
textAnchor="middle"
style={{ pointerEvents: "none" }}
fill={white}
>
{node.data.type}
</text>
</Group>
);
}
function RootNode({ node }: { node: HierarchyNode }) {
return (
<Group top={node.x} left={node.y}>
<circle r={12} fill="url('#lg')" />
<text
dy=".33em"
fontSize={9}
fontFamily="Arial"
textAnchor="middle"
style={{ pointerEvents: "none" }}
fill={plum}
>
{node.data.name}
</text>
</Group>
);
}
function ParentNode({ node }: { node: HierarchyNode }) {
const width = 40;
const height = 20;
const centerX = -width / 2;
const centerY = -height / 2;
return (
<Group top={node.x} left={node.y}>
<rect
height={height}
width={width}
y={centerY}
x={centerX}
fill={background}
stroke={blue}
strokeWidth={1}
onClick={() => {}}
/>
<text
dy="-3.3em"
fontSize={4}
fontFamily="Arial"
textAnchor="middle"
style={{ pointerEvents: "none" }}
fill={white}
>
{node.data.id}
</text>
<text
dy=".33em"
fontSize={9}
fontFamily="Arial"
textAnchor="middle"
style={{ pointerEvents: "none" }}
fill={white}
>
{node.data.name}
</text>
</Group>
);
}
const defaultMargin = { top: 10, left: 80, right: 80, bottom: 10 };
export type TreeProps = {
width: number;
height: number;
margin?: { top: number; right: number; bottom: number; left: number };
tree: TreeNode;
};
export function HorizontalHierarchyTreeVisualization({
width,
height,
tree,
margin = defaultMargin,
}: TreeProps) {
const data = useMemo(() => hierarchy(tree), []);
const yMax = height - margin.top - margin.bottom;
const xMax = width - margin.left - margin.right;
return width < 10 ? null : (
<svg width={width} height={height}>
<LinearGradient id="lg" from={peach} to={pink} />
<rect width={width} height={height} rx={14} fill={background} />
<Tree<TreeNode> root={data} size={[yMax, xMax]}>
{(tree) => (
<Group top={margin.top} left={margin.left}>
{tree.links().map((link, i) => (
<LinkHorizontal
key={`link-${i}`}
data={link}
stroke={lightpurple}
strokeWidth="1"
fill="none"
/>
))}
{tree.descendants().map((node, i) => (
<Node key={`node-${i}`} node={node} />
))}
</Group>
)}
</Tree>
</svg>
);
}