-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcytoscape.js
142 lines (127 loc) · 4.02 KB
/
cytoscape.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
// generated color pallete: https://gka.github.io/palettes/#/9|s|00429d,96ffea,ffffe0|ffffe0,ff005e,93003a|1|1
// full pallete: ["#ffffe0", "#c5eddf", "#a5d5d8", "#8abccf", "#73a2c6", "#5d8abd", "#4771b2", "#2e59a8", "#00429d"]
const nodeColors = ["#8abccf", "#73a2c6", "#5d8abd", "#4771b2", "#00429d"]
const edgeColor = '#ccc';
const selectedEdgeColor = '#777';
function createCytoScape(graph) {
removeDisconnectedNodes(graph);
const elements = [];
for (const node of graph.nodes) {
if (node) {
const { label: id, group, ...rest } = node;
const color = nodeColors[group - 1];
elements.push({ data: { id, color, ...rest } });
}
}
for (const edge of graph.edges) {
const from = graph.nodes[edge.from].label;
const to = graph.nodes[edge.to].label;
elements.push({ data: { id: `${from} -> ${to}`, source: from, target: to } });
}
const cy = cytoscape({
container: document.getElementById('container'),
elements,
style: [ // the stylesheet for the graph
{
selector: 'node',
style: {
'background-color': 'data(color)',
'label': 'data(id)',
}
},
{
selector: 'edge',
style: {
'width': 2,
'line-color': edgeColor,
'target-arrow-color': edgeColor,
'target-arrow-shape': 'triangle',
'curve-style': 'bezier'
}
}
],
layout: {
name: 'klay',
klay: {
spacing: 30,
thoroughness: 7,
direction: 'DOWN',
}
}
});
const tippies = {}
graph.nodes.forEach(node => {
if (node) {
tippies[node.label] = makeTippy(cy.getElementById(node.label), node);
}
})
cy.on('mouseover', 'node', e => {
const id = e.target.id();
tippies[id].show();
cy.style().selector(`edge[source="${id}"]`).style({
'width': 4,
'arrow-scale': 1.1,
'line-color': selectedEdgeColor,
'target-arrow-color': selectedEdgeColor,
}).update();
document.body.style.cursor = 'pointer';
});
cy.on('mouseout', 'node', e => {
const id = e.target.id();
tippies[id].hide();
cy.style().selector(`edge[source="${id}"]`).style({
'width': 2,
'arrow-scale': 1,
'line-color': edgeColor,
'target-arrow-color': edgeColor,
}).update();
document.body.style.cursor = 'default';
});
cy.on('tap', 'node', e => {
const href = e.target.data('href');
if (e.originalEvent.ctrlKey) { // open in new tab if ctrl-click
window.open(href, '_blank');
} else {
location.href = href;
}
})
}
function makeTippy(node, { title, description, hours }) {
const ref = node.popperRef();
// unfortunately, a dummy element must be passed
// as tippy only accepts a dom element as the target
// https://github.com/atomiks/tippyjs/issues/661
const dummyDomEle = document.createElement('div');
const tip = tippy(dummyDomEle, {
theme: 'translucent', // maybe use 'light' instead?
onCreate: instance => { // mandatory
// patch the tippy's popper reference so positioning works
// https://atomiks.github.io/tippyjs/misc/#custom-position
instance.popperInstance.reference = ref;
},
lazy: false, // mandatory
trigger: 'manual', // mandatory
// dom element inside the tippy:
content: () => { // function can be better for performance
const div = document.createElement('div');
div.innerHTML = `
<h3 style="display: flex; justify-content: space-between; align-items: center; margin: 10px; margin-bottom: 5px">
${title}
<span style="letter-spacing: 5px; font-size: 2em">${'•'.repeat(hours)}</span>
</h3>
<p style="margin: 10px; margin-top: 0">${description}</p>
`;
return div;
},
// your own preferences:
arrow: true,
placement: 'top',
hideOnClick: false,
multiple: true,
sticky: true,
// if interactive:
interactive: true,
appendTo: document.getElementById('container') // or append dummyDomEle to document.body
});
return tip;
};