generated from foambubble/foam-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.njk
172 lines (153 loc) · 4.58 KB
/
graph.njk
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
<style type="text/css">
#graph-wrapper {
width: 100%;
overflow-x: scroll;
}
svg {
margin: 2rem 1rem;
}
svg text {
font-family: system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,Segoe UI Emoji,Apple Color Emoji,Noto Color Emoji,sans-serif;
font-size: 1rem;
stroke: var(--background-body);
stroke-width: 8px;
}
.spinner-wrapper {
width: 100%;
display: flex;
justify-content: center;
margin-top: 100px;
}
.lds-ellipsis {
display: inline-block;
position: relative;
width: 80px;
height: 80px;
}
.lds-ellipsis div {
position: absolute;
top: 33px;
width: 13px;
height: 13px;
border-radius: 50%;
background: var(--text-main);
animation-timing-function: cubic-bezier(0, 1, 1, 0);
}
.lds-ellipsis div:nth-child(1) {
left: 8px;
animation: lds-ellipsis1 0.6s infinite;
}
.lds-ellipsis div:nth-child(2) {
left: 8px;
animation: lds-ellipsis2 0.6s infinite;
}
.lds-ellipsis div:nth-child(3) {
left: 32px;
animation: lds-ellipsis2 0.6s infinite;
}
.lds-ellipsis div:nth-child(4) {
left: 56px;
animation: lds-ellipsis3 0.6s infinite;
}
@keyframes lds-ellipsis1 {
0% {
transform: scale(0);
}
100% {
transform: scale(1);
}
}
@keyframes lds-ellipsis3 {
0% {
transform: scale(1);
}
100% {
transform: scale(0);
}
}
@keyframes lds-ellipsis2 {
0% {
transform: translate(0, 0);
}
100% {
transform: translate(24px, 0);
}
}
</style>
<div id="graph-wrapper"><div class="spinner-wrapper"><div class="lds-ellipsis"><div></div><div></div><div></div><div></div></div></div></div>
<script type="module">
import * as d3 from "https://cdn.skypack.dev/d3@7";
const data = {{ collections.all | tree(protected) | safe }};
// Modified under ISC from https://observablehq.com/@d3/tree
const getLabel = (d, n) => /* d?.label || */ d?.title || n.id.split('/').pop().replace(/-/g, ' ').replace(/./, c => c.toUpperCase());
const getLink = (d, n) => d?.url;
//const root = d3.hierarchy(data, children);
const root = d3.stratify().path(d => d.url)(data);
// Sort the nodes.
//sort, // how to sort nodes prior to layout (e.g., (a, b) => d3.descending(a.height, b.height))
//if (sort != null) root.sort(sort);
// Compute the layout.
// TODO: compute width based on sum of max title char length of each level
const dx = 30;
const dy = 300;
d3.tree().nodeSize([dx, dy])(root);
// Center the tree.
let minY = Infinity;
let maxY = -minY;
let minX = Infinity;
let maxX = -minY;
root.each(d => {
if (d.x > maxY) maxY = d.x;
if (d.x < minY) minY = d.x;
if (d.y > maxX) maxX = d.y;
if (d.y < minX) minX = d.y;
});
// Compute the default dims
const width = maxX - minX;
const height = maxY - minY;
const svg = d3.create("svg")
.attr("viewBox", [minX - 150, minY - dx, width + 2 * dy, height + 2 * dx])
.attr("width", width + 2 * dy)
.attr("height", height + 2 * dx)
.attr("font-family", "sans-serif")
.attr("font-size", 10);
svg.append("g")
.attr("fill", "none")
.attr("stroke", 'var(--text-main)')
.attr("stroke-opacity", 0.4)
//.attr("stroke-linecap", strokeLinecap)
//.attr("stroke-linejoin", strokeLinejoin)
.attr("stroke-width", 1.5)
.selectAll("path")
.data(root.links())
.join("path")
.attr("d", d3.linkHorizontal()
.x(d => d.y)
.y(d => d.x));
const node = svg.append("g")
.selectAll("a")
.data(root.descendants())
.join("a")
.attr("xlink:href", d => getLink(d.data, d))
.attr("target", "_blank")
.attr("transform", d => `translate(${d.y},${d.x})`);
node.append("circle")
.attr("fill", d => d.data ? 'var(--links)' : 'var(--text-main)')
//.attr("opacity", d => d.data ? 1 : 0);
.attr("r", 3)
//if (title != null) node.append("title")
// .text(d => title(d.data, d));
// Compute labels and titles.
const descendants = root.descendants();
const L = descendants.map(d => getLabel(d.data, d));
node.append("text")
.attr("dy", "0.32em")
.attr("x", d => d.children ? -6 : 6)
.attr("text-anchor", d => d.children ? "end" : "start")
.attr("paint-order", "stroke")
.attr("fill", d => d.data ? 'var(--links)' : 'var(--text-main)')
.text((d, i) => L[i]);
const graphWrapper = document.getElementById('graph-wrapper');
graphWrapper.removeChild(graphWrapper.firstChild);
graphWrapper.appendChild(svg.node());
</script>