-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforce_directed_graph.html
281 lines (246 loc) · 8.37 KB
/
force_directed_graph.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src='https://d3js.org/d3.v4.min.js'></script>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<style>
body {
font: Arial 12px;
text-align: center;
}
.link {
stroke: #ccc;
}
.node text {
pointer-events: none;
font: sans-serif;
}
</style>
<title>Title</title>
</head>
<body>
<select id="graphSelect" onchange="ShowGraph(parseInt(this.value))">
<option selected="selected" value="1">English</option>
<option value="2">简体中文</option>
</select>
<svg id="d3canvas" style="display: block"></svg>
<svg id="d3canvas_zh" style="display: none"></svg>
<script>
// var nodes = [ { name: "efficient visual representations", group:1 }, { name: "Neuromorphic vision sensors", group:1 },
// { name: "unique advantages", group:1 }, { name: "traditional image and video processing methods", group:1 },
// { name: "their frame based counterparts", group:1 } ];
var nodes_en = []
var nodes_zh = []
var edges_en = []
var edges_zh = []
function ShowGraph(page){
//console.log(page)
if (page == '1'){
$("#d3canvas").css("display","block")
$("#d3canvas_zh").css("display","none")
}else{
$("#d3canvas").css("display","none")
$("#d3canvas_zh").css("display","block")
}
}
function readJson() {
var url = "nodes1.json"
var request = new XMLHttpRequest
request.open("get", url)
request.send(null)
request.onload = function () {
if (request.status == 200) {
var json = JSON.parse(request.responseText)
for (var i = 0; i < json.length; i++) {
console.log(json[i].name)
}
console.log(json)
nodes = json
}
}
}
// 读取Json文件数据
function getedgesJson(data) {
// console.log(data)
edges_en = data
}
function getedgesJson_zh(data){
edges_zh = data
}
function sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
function getnodesJson(data) {
// data 就是要取的json数据
nodes_en = data
// getedgesJson()
//console.log(edges)
sleep(10).then(() => {
// 这里写sleep之后需要去做的事情
drift('#d3canvas',nodes_en, edges_en)
})
}
function getnodesJson_zh(data) {
// data 就是要取的json数据
nodes_zh = data
// getedgesJson()
//console.log(edges)
sleep(10).then(() => {
// 这里写sleep之后需要去做的事情
drift('#d3canvas_zh',nodes_zh, edges_zh)
})
}
function drift(tagName, nodes, edges) {
//console.log(tagName)
var marge = {top: 60, bottom: 60, left: 60, right: 60}
var svg = d3.select(tagName)
//console.log($(window).width(), $(window).height())
svg.attr("width", $(window).width())
svg.attr("height", $(window).height())
var width = svg.attr("width")
var height = svg.attr("height")
var g = svg.append("g").attr("transform", "translate(" + marge.top + "," + marge.left + ")");
var g = svg.append("g").attr("transform", "scale(" + 0.25 + "," + 0.25 + ")");
// 准备数据
//document.documentElement.scrollTop = height/2
// var edges = [ { source : 0 , target: 1,value:1 } , { source : 0 , target: 2,value:1.3 } ,
// { source : 0 , target: 3,value:1 } , { source : 0 , target: 4,value:5 },{ source : 0 , target: 5,value:3 }];
console.log(nodes)
console.log(edges)
//新建一个力导向图
var forceSimulation = d3.forceSimulation(nodes)
.force("charge", d3.forceManyBody())
.force("link", d3.forceLink(edges))
.force("center", d3.forceCenter());
//设置一个color的颜色比例尺,为了让不同的扇形呈现不同的颜色
var colorScale = d3.scaleOrdinal()
.domain(d3.range(nodes.length))
.range(d3.schemeCategory10);
//生成节点数据
forceSimulation.nodes(nodes)
.on("tick", ticked);//这个函数很重要,后面给出具体实现和说明
//生成边数据
forceSimulation.force("link")
.links(edges)
.distance(function (d) {//每一边的长度
return d.value * 500;
})
//设置图形的中心位置
forceSimulation.force("center")
.x(width * 2)
.y(height * 2);
//绘制边
var links = g.append("g")
.selectAll("line")
.data(edges)
.enter()
.append("line")
.attr("stroke", function (d, i) {
//return colorScale(i);
return "#888888";
})
.attr("stroke-width", 1);
var linksText = g.append("g")
.selectAll("text")
.data(edges)
.enter()
.append("text")
.text(function (d) {
return d.relation;
})
var gs = g.selectAll(".circleText")
.data(nodes)
.enter()
.append("g")
.attr("transform", function (d, i) {
var cirX = d.x;
var cirY = d.y;
return "translate(" + cirX + "," + cirY + ")";
})
.call(d3.drag()
.on("start", started)
.on("drag", dragged)
.on("end", ended)
);
//绘制节点
gs.append("circle")
.attr("r", 10)
.attr("fill", function (d, i) {
if (d.title == 1) {
return "#86a7a7";
}
//return colorScale(i);
return "#c9d8d8"
})
//文字
gs.append("text")
.attr("x", -10)
.attr("y", -20)
.attr("dy", 10)
.text(function (d) {
return d.name;
})
.style("fill", function (d) {
if (d.count) {
return "#555555"
}
})
.style("font-size", function (d) {
var fontSize = 16 + 5 * d.count;
return fontSize + "px"
})
function ticked() {
links
.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;
});
linksText
.attr("x", function (d) {
return (d.source.x + d.target.x) / 2;
})
.attr("y", function (d) {
return (d.source.y + d.target.y) / 2;
});
gs
.attr("transform", function (d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
function started(d) {
if (!d3.event.active) {
forceSimulation.alphaTarget(0.7).restart();//设置衰减系数,对节点位置移动过程的模拟,数值越高移动越快,数值范围[0,1]
}
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function ended(d) {
if (!d3.event.active) {
forceSimulation.alphaTarget(0);
}
d.fx = null;
d.fy = null;
}
}
// document.documentElement.scrollTop = 1600
// document.documentElement.
</script>
<script type="text/javascript" src="./data/json/nodes1_zh.json"></script>
<script type="text/javascript" src="./data/json/nodes1.json"></script>
<script type="text/javascript" src="./data/json/edges1_zh.json"></script>
<script type="text/javascript" src="./data/json/edges1.json"></script>
</body>
</html>