Repository navigation
Expand file tree
/
Copy pathgraphs.html
More file actions
169 lines (159 loc) · 7.24 KB
/
Copy pathgraphs.html
File metadata and controls
169 lines (159 loc) · 7.24 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>VivaGraph: real graphs</title>
<link rel="icon" href="data:,">
<script>
// Light or dark: what the address says (?theme=light), or else what the visitor's system is
// set to. Known before the page draws, so it does not flash in the other one.
const BACKGROUND = {light: '#ffffff', dark: '#0e1624'};
const systemLight = matchMedia('(prefers-color-scheme: light)');
function themeNow() {
const asked = new URLSearchParams(location.search).get('theme');
return Object.hasOwn(BACKGROUND, asked) ? asked : systemLight.matches ? 'light' : 'dark';
}
document.documentElement.dataset.theme = themeNow();
</script>
<style>
/* The canvas color, so the page does not flash before the first frame */
html {background: #0e1624; --text: #e6ebf2; --quiet: #a9b4c4; --control: #151e2d; --edge: rgba(255, 255, 255, 0.2)}
html[data-theme=light] {background: #ffffff; --text: #1b2433; --quiet: #556070; --control: #ffffff; --edge: rgba(27, 36, 51, 0.25)}
#about {position: fixed; left: 12px; top: 12px; max-width: min(26em, calc(100vw - 24px)); font: 13px/1.45 system-ui, sans-serif; color: var(--text)}
#about select, #about button {font: inherit; color: inherit; background: var(--control); border: 1px solid var(--edge); border-radius: 6px; padding: 4px 6px; max-width: 100%}
#about button {cursor: pointer; padding: 4px 10px}
#controls {display: flex; gap: 6px}
#controls select {min-width: 0}
#about p {margin: 8px 0 0; color: var(--quiet)}
#about a {color: inherit}
</style>
</head>
<body>
<div id="about">
<div id="controls">
<select id="pick" aria-label="Graph"></select>
<button id="theme"></button>
</div>
<p id="text">Loading…</p>
<p id="credit"></p>
</div>
<script src="../dist/vivagraph.min.js"></script>
<script>
// Each graph is a file in data/. The page loads them with fetch, so it has to come from a web
// server (`npx serve`, for one) and not straight from the disk.
const GRAPHS = {
'london-underground': 'London Underground',
'influence': 'Who influenced whom (6,000 people)',
'royal-houses': 'Royal houses of Europe',
'greek-mythology': 'Greek mythology',
'programming-languages': 'Programming languages',
'nobel-advisors': 'Nobel laureates and their teachers',
'air-routes': 'Air routes',
'constellations': 'Constellations',
'muscles-and-bones': 'Muscles and bones',
'burgess-shale': 'Burgess Shale food web',
'miserables': 'Les Misérables',
'word-ladders': 'Word ladders',
'countries': 'Countries and their borders',
'us-counties': 'US counties',
'wordnet-mammals': 'Mammals (WordNet)',
'chess-openings': 'Chess openings',
'languages': 'Languages of the world (13,000 nodes)',
'world-metros': 'Metros of the world (16,000 nodes)',
'wordnet-nouns': 'English nouns (74,000 nodes, best on WebGPU)'
};
const pick = document.getElementById('pick');
for (const key in GRAPHS) pick.add(new Option(GRAPHS[key], key));
// Picking a graph changes the address, so every graph has a link and Back goes to the last one
pick.onchange = () => { history.pushState(null, '', address({graph: pick.value})); show(); };
window.onpopstate = () => { show(); paint(); };
// The button names the look it switches to. A look picked here stays in the address, and with
// it in links to the page; until then the page follows the system.
const button = document.getElementById('theme');
button.onclick = () => {
history.replaceState(null, '', address({theme: themeNow() === 'light' ? 'dark' : 'light'}));
paint();
};
systemLight.addEventListener('change', paint);
let viva = null, turn = 0;
show();
paint();
// The address of now, with one part changed
function address(change) {
const params = new URLSearchParams(location.search);
for (const key in change) params.set(key, change[key]);
return '?' + params;
}
// The page and the graph in the look of now
function paint() {
const theme = themeNow();
document.documentElement.dataset.theme = theme;
button.textContent = theme === 'light' ? 'Dark' : 'Light';
if (viva) viva.setBackground(BACKGROUND[theme]);
}
// Draws the graph that the address names, in place of the one on screen
function show() {
const asked = new URLSearchParams(location.search).get('graph');
const name = Object.hasOwn(GRAPHS, asked) ? asked : 'london-underground';
const mine = ++turn;
pick.value = name;
document.getElementById('text').textContent = 'Loading…';
document.getElementById('credit').textContent = '';
fetch(`data/${name}.json`).then(response => response.json()).then(file => {
if (mine !== turn) return; // another graph was picked while this one was on its way
const graph = toGraph(file);
// The file says which field colors the nodes and which one puts the important names first
const style = {};
if (file.colorBy) style.colorBy = file.colorBy;
if (file.palette) style.palette = file.palette;
if (file.linkData && file.linkColors) style.linkColors = file.linkColors;
if (viva) viva.dispose();
viva = Viva.render(graph, {
background: BACKGROUND[themeNow()],
style,
labels: file.importance ? {importance: file.importance} : true
});
const count = n => n.toLocaleString('en-US');
document.getElementById('text').textContent =
`${file.about} ${count(graph.getNodesCount())} nodes, ${count(graph.getLinksCount())} links.`;
credit(document.getElementById('credit'), `Data: ${file.source}. ${file.license}.`);
}).catch(error => {
if (mine === turn) document.getElementById('text').textContent = `Could not load the graph (${error.message}).`;
});
}
// A file is {fields, nodes: [[...fields]], links: [from, to, from, to, ...], linkData?} and a few
// words about itself. A node's row holds `fields` in order: "id" is its id (the row number when
// there is none), and the rest becomes node.data, which labels, tooltips and search read.
function toGraph(file) {
const graph = Viva.createGraph();
const idAt = file.fields.indexOf('id');
const ids = file.nodes.map((row, i) => {
const data = {};
file.fields.forEach((field, k) => {
if (k === idAt || row[k] === null || row[k] === undefined) return;
// Fields that start with "_" color the nodes, but tooltips don't list them
if (field[0] === '_') Object.defineProperty(data, field, {value: row[k]});
else data[field] = row[k];
});
const id = idAt < 0 ? i : row[idAt];
graph.addNode(id, data);
return id;
});
for (let i = 0; i + 1 < file.links.length; i += 2) {
graph.addLink(ids[file.links[i]], ids[file.links[i + 1]], file.linkData ? file.linkData[i / 2] : undefined);
}
return graph;
}
// Text with its web addresses as links
function credit(element, text) {
text.split(/(https?:\/\/[^\s),]*[^\s),.])/).forEach((part, i) => {
if (i % 2 === 0) { element.append(part); return; }
const link = element.appendChild(document.createElement('a'));
link.href = part;
link.textContent = part.replace(/^https?:\/\/(www\.)?/, '').replace(/\/$/, '');
});
}
</script>
</body>
</html>