-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
286 lines (231 loc) · 7.96 KB
/
main.js
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import * as dagreD3 from 'dagre-d3';
import * as graphlibDot from 'graphlib-dot';
import * as FuzzySet from 'fuzzyset.js';
import {
ancestryGraph,
closestAncestor,
commonAncestryGraph,
descendantsCountExceeds,
shortestPath,
} from './graph_search';
var data = null; // Contains the raw graph data
var nameToId = new Object();
var fuzzyNames = FuzzySet.default();
let width = 1000;
let height = 800;
let svg = d3.select("#content").append("svg")
.attr("width", width)
.attr("height", height)
.style("cursor", "move");
let initialScale = '0.2';
let inner = svg.append("g").attr("transform", `scale(${initialScale},${initialScale})`);
// Set up zoom support
let zoom = d3.behavior.zoom().scale(initialScale).on("zoom", function() {
inner.attr("transform", "translate(" + d3.event.translate + ")" +
"scale(" + d3.event.scale + ")");
});
svg.call(zoom);
function convertData(d) {
// decompress the input
let nodes = d.nodes;
let edges = d.edges;
let allKeys = Object.keys(nodes).map(x => parseInt(x));
let maxKey = 0;
for (let i = 0, n = allKeys.length; i < n; i++) {
if (allKeys[i] > maxKey) {
maxKey = allKeys[i];
}
}
let graph = new Array(1 + maxKey);
// graph is a list whose index is the id, and the entries are objects of the form
//
// {
// name: str,
// in: [int], <-- list of ids of incoming edges
// out: [int], <-- list of ids of outgoing edges
// }
for (let id in nodes) {
let mgp_id = parseInt(id);
let name = nodes[id];
if (name) {
graph[mgp_id] = {
'name': name,
'in': [],
'out': [],
};
nameToId[name] = mgp_id;
fuzzyNames.add(name);
}
}
for (let i = 0, n = edges.length; i < n; i++) {
let source = edges[i][0];
let target = edges[i][1];
// sometimes the dataset can be missing entries, then the graph entry
// will be null, and we filter them out
if (graph[source] != null && graph[target] != null) {
graph[source]['out'].push(target);
graph[target]['in'].push(source);
}
}
return graph;
}
function setSearchBar(text, textInputId, resultsId) {
d3.select(textInputId).property('value', text);
d3.select(resultsId).style('display', 'none');
}
function suggest(textInputId, resultsId) {
let searchString = d3.select(textInputId).property('value');
let autocomplete = d3.select(resultsId);
console.log('Autocompleting ' + searchString);
if (d3.event.keyCode == 13) {
autocomplete.style('display', 'none');
renderFromSearch(textInputId);
} else {
var results = null;
if (searchString.length >= 3) {
results = fuzzyNames.get(searchString, '', 0.3);
}
let dataList = autocomplete.selectAll('li')
.data([]).exit().remove();
if (results) {
let dataList = autocomplete.selectAll('option').data(results);
dataList.enter()
.append('li')
.attr('class', 'autocomplete_option')
.attr('value', function (d) { return d[1]; })
.text(function (d) { return d[1]; });
}
autocomplete.selectAll('.autocomplete_option').on('click', function() {
setSearchBar(this.innerText, textInputId, resultsId);
});
autocomplete.style('display', 'block');
}
}
d3.json("genealogy_graph.json", function(error, d) {
if (error) throw error;
console.log('Done loading data');
data = convertData(d);
window.data = data;
console.log('Done converting data');
d3.select('#hide_while_loading').style('display', 'block');
d3.select('#loading').style('display', 'none');
let gauss = 18231;
createAncestryGraphFor(gauss);
});
function edgeListToStrings(edges) {
return edges.map(function(e) {
let sourceName = data[e[0]].name;
let targetName = data[e[1]].name;
return '"' + sourceName + '" -> "' + targetName + '";';
});
}
// Create and configure the renderer
var render = dagreD3.render();
function renderGraph(graphString) {
let graph;
try {
graph = graphlibDot.read(graphString);
} catch (e) {
console.log('Failed to parse graph...')
throw e;
}
// Render the graph into svg g
d3.select("svg g").call(render, graph);
// Zoom and translate to center
let graphWidth = graph.graph().width;
let graphHeight = graph.graph().height;
let zoomScale = Math.min(width / graphWidth, height / graphHeight);
let translate = [(width/2) - ((graphWidth * zoomScale)/2), 0];
zoom.translate(translate);
zoom.scale(zoomScale);
zoom.event(inner);
return graph;
}
function createAncestryGraphStringFor(id) {
let parentsOnly = false;
if (descendantsCountExceeds(data, id, 1000)) {
console.log("the graph is too big!");
parentsOnly = true;
}
let edgeStrings = edgeListToStrings(ancestryGraph(data, id, parentsOnly));
let graphString = "digraph { ";
graphString = graphString + "\n \"" + data[id].name + "\" [style=\"fill: #66ff66; font-weight: bold\"];\n";
for (let edge of edgeStrings) {
graphString = graphString + " " + edge + "\n";
}
graphString = graphString + "}\n";
return graphString;
}
function createAncestryGraphFor(id) {
return renderGraph(createAncestryGraphStringFor(id));
}
function createCommonAncestryGraphFor(id1, id2) {
let graph = commonAncestryGraph(data, id1, id2);
setOrClearErrorField("No common ancestors!", graph);
if (!graph) {
return;
}
let edgeStrings = edgeListToStrings(graph);
let graphString = "digraph { ";
graphString = graphString + " \"" + data[id1].name + "\" [style=\"fill: #66ff66; font-weight: bold\"];";
graphString = graphString + " \"" + data[id2].name + "\" [style=\"fill: #6666ff; font-weight: bold\"];";
for (let edge of edgeStrings) {
graphString = graphString + " " + edge + " ";
}
graphString = graphString + "}";
return renderGraph(graphString);
}
function setOrClearErrorField(text, condition) {
if (condition) {
d3.select("#name_not_found").style('display', 'none');
} else {
d3.select("#name_not_found").style('display', 'block').text(text);
}
}
function getIdFromSearch(textInputId) {
let name = d3.select(textInputId).property("value").trim();
let id = nameToId[name];
setOrClearErrorField("Name '" + name + "' not found.", id);
return id;
}
function renderAncestryGraphFromSearch() {
let id = getIdFromSearch('#single_name_input');
if (id) {
createAncestryGraphFor(id);
}
}
function renderCommonAncestryGraphFromSearch() {
let id1 = getIdFromSearch('#common_ancestor_input1');
let id2 = getIdFromSearch('#common_ancestor_input2');
if (id1 && id2) {
createCommonAncestryGraphFor(id1, id2);
}
}
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
pom.dispatchEvent(event);
}
else {
pom.click();
}
}
function downloadDotFile() {
let id = getIdFromSearch('#single_name_input');
if (id) {
download('ancestry_graph.dot', createAncestryGraphStringFor(id));
}
}
d3.select("#single_name_input").on("keyup", () => suggest("#single_name_input", "#single_name_autocomplete_results"));
d3.select('#ancestry_button').on('click', renderAncestryGraphFromSearch);
d3.select("#common_ancestor_input1").on("keyup", () => suggest("#common_ancestor_input1", "#common_ancestor_autocomplete_results"));
d3.select("#common_ancestor_input2").on("keyup", () => suggest("#common_ancestor_input2", "#common_ancestor_autocomplete_results"));
d3.select('#common_ancestry_button').on('click', renderCommonAncestryGraphFromSearch);
d3.select('#autocomplete_results').style('display', 'none');
d3.select('#loading').style('display', 'block');
d3.select('#hide_while_loading').style('display', 'none');
d3.select('#download').on('click', downloadDotFile);