This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
viz.tmpl
148 lines (139 loc) · 4.17 KB
/
viz.tmpl
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
<html lang="en">
<head>
<title>ent schema network</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/tonsky/[email protected]/distr/fira_code.css">
<script src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/randomcolor/0.6.1/randomColor.min.js" integrity="sha512-vPeZ7JCboHcfpqSx5ZD+/jpEhS4JpXxfz9orSvAPPj0EKUVShU2tgy7XkU+oujBJKnWmu4hU7r9MMQNWPfXsYw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<style type="text/css">
html * {
font-family: 'Fira Code', monospace !important;
font-size: 14px;
}
#schema {
width: 100%;
height: 100%;
border: 1px solid lightgray;
}
.var-type {
color: #4EC9B0;
}
table {
padding: 2px 3px;
}
.vis-tooltip,
.table-container {
background-color: #1e1e1e !important;
color: white;
}
tr {
color: white;
}
</style>
</head>
<body>
<div id="schema"></div>
<br />
<script type="text/javascript">
// see https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces
const fieldsToTable = fields => {
const container = document.createElement("div");
container.setAttribute("class", "table-container")
if (!fields) {
container.innerText = "no fields";
return container;
}
const tbl = document.createElement("table");
const tblBody = document.createElement("tbody");
for (const field of fields) {
const row = document.createElement("tr");
for (const key of ["name", "type"]) {
const cell = document.createElement("td");
const cellText = document.createTextNode(field[key]);
if (key === "type") {
cell.setAttribute("class", "var-type")
}
cell.appendChild(cellText);
row.appendChild(cell);
}
tblBody.appendChild(row);
}
tbl.appendChild(tblBody);
container.appendChild(tbl);
return container;
}
// get the graph representation from go (template)
const entGraph = JSON.parse({{.}});
const nodes = new vis.DataSet((entGraph.nodes || []).map(n =>
({
id: n.id,
label: n.id,
color: randomColor({
luminosity: 'light',
hue: 'random',
}),
title: fieldsToTable(n.fields),
})
));
edgesCounter = {};
// go through edges and magnify nodes with multiple self references
// and node with multiple edges to the same node
const edgeKey = e => `${e.to}::${e.from}`
const edges = new vis.DataSet((entGraph.edges || []).map(e => {
const counter = (edgesCounter[edgeKey(e)] || 0) + 1;
edgesCounter[edgeKey(e)] = counter;
if (e.from === e.to) {
return {
...e,
physics: false,
arrows: "to",
type: 'curvedCW',
selfReference: {
size: (counter + 1) * 10,
angle: (counter * 0.8) * Math.PI / 4,
renderBehindTheNode: false
}
}
}
return { ...e, type: 'curvedCW', physics: false, arrows: "to", smooth: { type: 'curvedCW', roundness: Math.pow(-1, counter) * 0.2 * counter } }
}));
const options = {
manipulation: false,
edges: {
physics: false,
smooth: { type: 'curvedCW', roundness: 0.2 },
arrows: "to",
},
nodes: {
widthConstraint: 60,
heightConstraint: 60,
shape: "box",
font: { align: "center" },
},
layout: {
improvedLayout: true,
hierarchical: {
enabled: true,
levelSeparation: 250,
},
},
physics: {
enabled: true,
barnesHut: {
springConstant: 0,
avoidOverlap: 1,
springConstant: 0
},
solver: "barnesHut",
repulsion: {
nodeDistance: 150,
springConstant: 0,
damping: 0,
springLength: 0
}
}
};
const container = document.getElementById("schema");
const gph = new vis.Network(container, { nodes, edges }, options);
</script>
</body>
</html>