-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappGraphRendering.js
144 lines (118 loc) · 4.21 KB
/
appGraphRendering.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
const initData = {
nodes: [
{"id": "Bon Iver", "genre": "Alternative", "size": 1},
{"id": "Kanye West", "genre": "Rap", "size": 1},
{"id": "Rihanna", "genre": "", "size": 1},
{"id": "Coldplay", "genre": "", "size": 1},
{"id": "Swedish House Mafia", "genre": "", "size": 1},
{"id": "James Blake", "genre": "Electronic", "size": 1}
],
links: [
{"source": "Bon Iver", "target": "Kanye West"},
{"source": "Rihanna", "target": "Kanye West"},
{"source": "Rihanna", "target": "Coldplay"},
{"source": "Swedish House Mafia", "target": "Coldplay"},
{"source": "Bon Iver", "target": "James Blake"}
]
};
let hoverLink = null;
let hoverNode = null;
const highlightNodes = new Set();
const highlightLinks = new Set();
const dimmedNodes = new Set();
// cross-link node objects
const Graph = ForceGraph()
const elem = document.getElementById('graph');
const graph = Graph(elem)
.graphData(initData)
.nodeId('id')
.nodeAutoColorBy('id')
.height((window.innerHeight*0.65))
.width((window.innerWidth - 24))
// .backgroundColor('gray')
//Node Click
.onNodeClick(node => {
searchForArtist(node.id);
})
// Node hover
.onNodeHover(node => {
hoverNode = node;
elem.style.cursor = node ? 'pointer' : null;
highlightNodes.clear();
highlightLinks.clear();
dimmedNodes.clear();
if (node) {
console.log(hoverNode);
Graph.graphData().links.forEach(link => {
if (link.source == hoverNode){
highlightNodes.add(link.target);
}else{
dimmedNodes.add(link.target);
}
if (link.target == hoverNode){
highlightNodes.add(link.source);
}else{
dimmedNodes.add(link.source);
}
});
}
})
/* .onLinkHover(link => {
hoverLink = link;
highlightNodes.clear();
highlightLinks.clear();
dimmedNodes.clear();
if (link){
highlightNodes.add(link.target);
highlightNodes.add(link.source);
Graph.graphData().nodes.forEach(node => {
dimmedNodes.add(node);
});
dimmedNodes.delete(link.target);
dimmedNodes.delete(link.source);
}
}) */
.autoPauseRedraw(false)
.nodeCanvasObject((node, ctx, globalScale) => {
const hovered = node === hoverNode;
const highlight = highlightNodes.has(node);
const dimmed = dimmedNodes.has(node);
const label = node.id;
const fontSize = node.size*12/globalScale;
ctx.font = `${fontSize}px Sans-Serif`;
const textWidth = ctx.measureText(label).width;
const bckgDimensions = [textWidth, fontSize].map(n => n + fontSize * 0.2); // some padding
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
ctx.fillRect(node.x - bckgDimensions[0] / 2, node.y - bckgDimensions[1] / 2, ...bckgDimensions);
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillStyle = hovered ? 'crimson'
: highlight ? 'black'
: dimmed ? ('rgba(0, 0, 0, 0.2)')
: node.color;
ctx.fillText(label, node.x, node.y);
node.__bckgDimensions = bckgDimensions; // to re-use in nodePointerAreaPaint
})
.nodePointerAreaPaint((node, color, ctx) => {
ctx.fillStyle = color;
const bckgDimensions = node.__bckgDimensions;
bckgDimensions && ctx.fillRect(node.x - bckgDimensions[0] / 2, node.y - bckgDimensions[1] / 2, ...bckgDimensions);
})
;
// Window resize stuff
elementResizeDetectorMaker().listenTo(
document.getElementById('graph'),
el => Graph.width(el.offsetWidth)
);
document.getElementById('btn1').addEventListener('click', function() {
var search = document.getElementById("textInput").value;
console.log(search);
searchForArtist(search);
});
document.getElementById('textInput').addEventListener('keydown', function (event) {
if (event.key === 'Enter' || event.keyCode === 13) {
var search = document.getElementById("textInput").value;
console.log(search);
searchForArtist(search);
}
});