-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.html
More file actions
80 lines (71 loc) · 1.66 KB
/
Copy pathhtml.html
File metadata and controls
80 lines (71 loc) · 1.66 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SQL AST Visualizer</title>
<style>
.node circle {
fill: #88c0d0;
}
.node text {
font: 12px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
</style>
</head>
<body>
<svg width="960" height="800"></svg>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
const data = {
"name": "-",
"children": [
{
"name": "*",
"children": [
{
"name": "+",
"children": [
{ "name": "1" },
{ "name": "100" }
]
},
{ "name": "3" }
]
},
{ "name": "1" }
]
};
const svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
const treeLayout = d3.tree().size([height - 100, width - 200]);
const root = d3.hierarchy(data);
treeLayout(root);
// Draw links
svg.selectAll(".link")
.data(root.links())
.join("path")
.attr("class", "link")
.attr("d", d3.linkHorizontal()
.x(d => d.y + 100)
.y(d => d.x));
// Draw nodes
const node = svg.selectAll(".node")
.data(root.descendants())
.join("g")
.attr("class", "node")
.attr("transform", d => `translate(${d.y + 100},${d.x})`);
node.append("circle").attr("r", 20);
node.append("text")
.attr("dy", 4)
.attr("x", d => d.children ? -25 : 25)
.style("text-anchor", d => d.children ? "end" : "start")
.text(d => d.data.name);
</script>
</body>
</html>