-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
98 lines (80 loc) · 2.59 KB
/
graph.js
File metadata and controls
98 lines (80 loc) · 2.59 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
async function fetchDBLP() {
const url = 'https://dblp.org/pid/68/7553-1.html';
const res = await fetch(url);
const text = await res.text();
const parser = new DOMParser();
const doc = parser.parseFromString(text, 'text/html');
const publications = [];
doc.querySelectorAll('.data').forEach(div => {
const authors = Array.from(div.querySelectorAll('.author')).map(a => a.textContent.trim());
if(authors.length > 0) publications.push(authors);
});
return publications;
}
function buildGraph(publications, centerName="Daniel de Oliveira") {
const nodesMap = new Map();
const links = [];
publications.forEach(authors => {
if (!authors.includes(centerName)) return;
authors.forEach(name => {
if (!nodesMap.has(name)) nodesMap.set(name, { id: name, isCenter: name===centerName });
});
authors.forEach(a1 => {
authors.forEach(a2 => {
if(a1!==a2) links.push({ source: a1, target: a2 });
});
});
});
return { nodes: Array.from(nodesMap.values()), links };
}
async function renderGraph() {
const publications = await fetchDBLP();
const { nodes, links } = buildGraph(publications);
const width = 900, height = 600;
const svg = d3.select("#graph")
.append("svg")
.attr("width", width)
.attr("height", height);
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d=>d.id).distance(120))
.force("charge", d3.forceManyBody().strength(-300))
.force("center", d3.forceCenter(width/2, height/2));
const link = svg.append("g")
.selectAll("line")
.data(links)
.enter().append("line")
.attr("stroke","#999").attr("stroke-opacity",0.6);
const node = svg.append("g")
.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("r", d=>d.isCenter?10:6)
.attr("fill", d=>d.isCenter?"orange":"steelblue")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.append("title").text(d=>d.id);
simulation.on("tick", ()=>{
link
.attr("x1", d=>d.source.x)
.attr("y1", d=>d.source.y)
.attr("x2", d=>d.target.x)
.attr("y2", d=>d.target.y);
node
.attr("cx", d=>d.x)
.attr("cy", d=>d.y);
});
function dragstarted(event,d){
if(!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x; d.fy = d.y;
}
function dragged(event,d){
d.fx = event.x; d.fy = event.y;
}
function dragended(event,d){
if(!event.active) simulation.alphaTarget(0);
d.fx = null; d.fy = null;
}
}
renderGraph();