Repository navigation
Expand file tree
/
Copy pathgraph.js
More file actions
140 lines (103 loc) · 4.02 KB
/
Copy pathgraph.js
File metadata and controls
140 lines (103 loc) · 4.02 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
function genGraph() {
//Begin generation of graph itself
var svg = d3.select("#chart"),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }).strength(function(d) {return (d.value / 5000)})) // Strength is common tags / 100
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
var g = svg.append("g")
.attr("class", "everything");
var link = g.append("g")
.attr("class", "links")
.selectAll("line")
.data(window.artistData.links)
.enter().append("line")
.attr("stroke-width", 1);
var node = g.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(window.artistData.nodes)
.enter().append("g");
svg.on("mousedown", function() {
node.selectAll("circle").classed("selected", false);
d3.selectAll(".links>line").classed("selected", false);
});
node.on("click", function(d) { // SET ONCLICK FUNCTIONALITY For artist spotlight
node.selectAll("circle").classed("selected", false);
d3.selectAll(".links>line").classed("selected", false);
d3.selectAll(".links>line").filter(function(link_d) {
return link_d.source.id == d.id || link_d.target.id == d.id;
}).classed("selected", true);
node
.select("circle").classed("selected", function(link_n) {
return d.linked.includes(link_n.id) ? true : false;
});
d3.select(this).select("circle").attr("class", "selected");
$("#spotlight").text(d.id); // Create spotlight list
$("#taglist").text(d.genres.join("\n"));
$("#linklist").text(d.linked.sort(function(a,b) { // Sort by # of common tags
var f = parseInt(a.split(",")[1]);
var s = parseInt(b.split(",")[1]);
return s - f;
}).join("\n"));
});
var circles = node.append("circle")
.attr("r", 5)
.attr("fill", function(d) { return color(d.group); })
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
var lables = node.append("text")
.text(function(d) {
return d.id;
})
.attr('x', 6)
.attr('y', 3);
simulation
.nodes(window.artistData.nodes)
.on("tick", ticked);
simulation.force("link")
.links(window.artistData.links);
var radius = 15;
node.attr("r", radius)
var zoom_handler = d3.zoom()
.on("zoom", zoomaction);
zoom_handler(svg);
function ticked() {
link
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
/*
node
.attr("transform", function(d) { return "translate(" + (d.x = Math.max(radius, Math.min(width - radius, d.x))) + "," + (d.y = Math.max(radius, Math.min(height - radius, d.y))) + ")";
})
*/
node
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
})
}
function zoomaction() { //zoom functionality
g.attr("transform", d3.event.transform);
}
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
}