-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
252 lines (216 loc) · 8.15 KB
/
index.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
text {
font-family: sans-serif;
font-size: 10px;
}
</style>
<body>
<!-- <div id="controls">
<input name="mode" type="radio" value="force" id="mode-force" checked>
<label for="mode-force"> Force-Directed</label>
<input name="mode" type="radio" value="circle" id="mode-circle">
<label for="mode-circle"> Circle</label>
</div> -->
<div id="graph"></div>
<svg width="960" height="800"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory10);
// color = {
// const scale = d3.scaleOrdinal(d3.schemeCategory10);
// return d => scale(d.group);
// }
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function (d) { return d.id; }))
.force("charge", d3.forceManyBody().strength(-15))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("collide", d3.forceCollide().radius(27))
// .force("r", d3.forceRadial(function (d) { return d.type === "a" ? 100 : 200; }))
// .force("r", d3.forceRadial(500, width / 2, height / 2))
.force("x", d3.forceX(function (d) {
if (d.group % 10 == 1) {
return 3.5 * width / 5
} else if (d.group % 10 == 0) {
return 1 * width / 5
} else {
return width / 2
};
}).strength(function (d) {
if (d.group === 15) {
return 1
} else if (d.group > 15) {
return 0.6
} else {
return 0.6
};
}))
// .strength(500)
// https://github.com/xzhang0123/Visualizing-Mitochondria-Reactions-using-d3/blob/main/mitochondria.json
// https://raw.githubusercontent.com/xzhang0123/Visualizing-Mitochondria-Reactions-using-d3/main/mitochondrial_reactions.json
// d3.json("C:\Users\msi_vz\Google drive\coursera\CS_519_Scientific_visualization\Project\Visualizing-Mitochondria-Reactions-using-d3", function (error, graph) {
// d3.json("https://raw.githubusercontent.com/xzhang0123/Visualizing-Mitochondria-Reactions-using-d3/main/mitochondria.json", function (error, graph) {
d3.json("https://raw.githubusercontent.com/xzhang0123/Visualizing-Mitochondria-Reactions-using-d3/main/mitochondrial_reactions1.json", function (error, graph) {
if (error) throw error;
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("stroke-width", function (d) { return Math.sqrt(d.value) + 2; });
console.log(graph)
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(graph.nodes)
.enter().append("g")
var circles = node.append("circle")
// .attr("r", 5)
.attr("r", function (d) { return 0.7 * (d.group) + 11; })
.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', 0)
.attr('y', 0)
// .style("alignment-baseline", "middle");
.style("text-anchor", "middle");
node.append("title")
.text(function (d) { return d.id; });
// simulation
// .nodes(graph.nodes)
// .on("tick", ticked);
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
// simulation.on("tick", () => {
// link.attr("d", linkArc);
// });
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 + "," + d.y + ")";
})
}
});
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;
}
function linkArc(d) {
const r = Math.hypot(d.target.x - d.source.x, d.target.y - d.source.y);
return `
M${d.source.x},${d.source.y}
A${r},${r} 0 0,1 ${d.target.x},${d.target.y}
`;
}
// legend
// var keys = ["Mister A", "Brigitte", "Eleonore", "Another friend", "Batman"]
var keys = [
{ "id": "Mitochondrial enzymes", "group": 20 },
{ "id": "Membrane-bound enzymes", "group": 15 },
{ "id": "Cytosolic enzymes", "group": 21 },
{ "id": "Mitochondrial substrates", "group": 10 },
{ "id": "Cytosolic substrates", "group": 11 }
]
console.log(keys)
svg.selectAll("mydots")
.data(keys)
.enter()
.append("circle")
.attr("cx", 600)
.attr("cy", function (d, i) { return 10 + i * 25 }) // 100 is where the first dot appears. 25 is the distance between dots
.attr("r", 9)
.style("fill", function (d) { return color(d.group) })
svg.selectAll("mylabels")
.data(keys)
.enter()
.append("text")
.attr("x", 620)
.attr("y", function (d, i) { return 10 + i * 25 }) // 100 is where the first dot appears. 25 is the distance between dots
.style("fill", function (d) { return color(d.group) })
.text(function (d) { return d.id })
.attr("text-anchor", "left")
.style("alignment-baseline", "middle")
///change to circle
// force.on("tick", function () {
// 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("cx", function (d) { return d.x; })
// .attr("cy", function (d) { return d.y; });
// });
// var force = d3.layout.force()
// .charge(-120)
// .linkDistance(30)
// .size([width, height]);
d3.selectAll("#controls input[name=mode]").on("change", function () {
if (this.value == 'circle') {
force.stop();
var circles = svg.selectAll('circle')[0];
svg.selectAll('circle').data().forEach(function (d, i) {
d.x_resume = circles[i].cx.animVal.value;
d.y_resume = circles[i].cy.animVal.value
});
svg.selectAll('line')
.transition().duration(1000)
.attr('x1', function (d) { return x_scale(Math.sin(index_to_rad(d.source.index))); })
.attr('x2', function (d) { return x_scale(Math.sin(index_to_rad(d.target.index))); })
.attr('y1', function (d) { return y_scale(Math.cos(index_to_rad(d.source.index))); })
.attr('y2', function (d) { return y_scale(Math.cos(index_to_rad(d.target.index))); });
svg.selectAll('circle')
.transition().duration(1000)
.attr('cx', function (d, i) { return x_scale(Math.sin(index_to_rad(i))); })
.attr('cy', function (d, i) { return y_scale(Math.cos(index_to_rad(i))); });
} else {
svg.selectAll('line')
.transition().duration(1000)
.attr('x1', function (d) { return d.source.x_resume; })
.attr('y1', function (d) { return d.source.y_resume; })
.attr('x2', function (d) { return d.target.x_resume; })
.attr('y2', function (d) { return d.target.y_resume; });
svg.selectAll('circle')
.transition().duration(1000)
.attr('cx', function (d) { return d.x_resume; })
.attr('cy', function (d) { return d.y_resume; });
setTimeout(function () {
// force.resume();
}, 1000);
}
});
</script>