-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmbostock-950642-2.html
More file actions
211 lines (181 loc) · 5.51 KB
/
Copy pathmbostock-950642-2.html
File metadata and controls
211 lines (181 loc) · 5.51 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
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
<!DOCTYPE html>
<html lang="zh" class="">
<head>
<meta charset="utf-8" />
<style>
.link {
stroke: #ccc;
}
.node text {
pointer-events: none;
font: 10px sans-serif;
}
</style>
</head>
<body>
<div>
<span>文件名:</span>
<input id="file" name="file" type="text"></input>
<button onclick="process();">PROCESS</button>
</div>
<!-- <script src="./lib/collections.js"></script> -->
<script src="./lib/linq.js"></script>
<script src="./lib/d3/d3.v3.js"></script>
<script src="./lib/d3/d3-tip.js"></script>
<script>
var width = 960,
height = 500;
// 将JSON中的数据解析出来,用于检索
var dataNodes = [];
var dataEdges = [];
var dataRelations = [];
document.getElementById("file").value = "graph1.json";
function processNormalNodes(node){
var tmp = node.filter( function(d) { return d.type == null || d.type == "" } );
tmp.append("image")
.attr("xlink:href", function(d) { return d.icon })
.attr("x", -8)
.attr("y", -8)
.attr("width", 16)
.attr("height", 16);
tmp.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.value });
}
function processNumberNodes(node){
var tmp = node.filter( function(d) { return d.type == "number" } );
tmp.append("image")
.attr("xlink:href", function(d) { return d.icon })
.attr("x", -8)
.attr("y", -8)
.attr("width", 16)
.attr("height", 16);
tmp.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.value });
}
function processStringNodes(node){
var tmp = node.filter( function(d) { return d.type == "string" } );
tmp.append("image")
.attr("xlink:href", function(d) { return d.icon })
.attr("x", -8)
.attr("y", -8)
.attr("width", 16)
.attr("height", 16);
tmp.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.value });
}
function processUrlNodes(node){
var tmp = node.filter( function(d) { return d.type == "url" } );
tmp.append("a")
.attr("xlink:href", function(d) { return d.value })
.attr("target", "_new")
.append("image")
.attr("xlink:href", function(d) { return d.icon })
.attr("x", -8)
.attr("y", -8)
.attr("width", 16)
.attr("height", 16);
}
function processImageNodes(node){
var tmp = node.filter( function(d) { return d.type == "image-url" } );
tmp.append("image")
.attr("xlink:href", function(d) { return d.icon })
.attr("x", -8)
.attr("y", -8)
.attr("width", 16)
.attr("height", 16);
}
function processObjectNodes(node){
var tmp = node.filter( function(d) { return d.type == "object-url" } );
tmp.append("image")
.attr("xlink:href", function(d) { return d.icon })
.attr("x", -8)
.attr("y", -8)
.attr("width", 16)
.attr("height", 16);
tmp.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.value });
}
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) { // 这里应该根据link/edge的relation进行检索,而不是直接使用某个属性
// 1
//return "<span style='color:gray'>" + d.relation + "</span>";
// 2
//var tmp = dataRelations.where( function(i){ return i.url == d.relation } ).first();
var tmp = dataRelations.first( function(i){ return i.url == d.relation } );
return "<span style='color:gray'>" + ( tmp == null ? "predicate(" + d.relation + ")" : tmp.title ) + "</span>";
})
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.call(tip);
var force = d3.layout.force()
.gravity(.05)
.distance(100)
.charge(-100)
.size([width, height]);
// test 0: orginal source json data
// test 1:
function process(){
var filename = document.getElementById("file").value;
d3.json("http://www.chuci.info/demo/lore-graph/example-data/" + filename, function(error, json) {
force
.nodes(json.nodes)
.links(json.edges)
.start();
dataNodes = json.nodes;
dataEdges = json.edges;
dataRelations = json.relations;
var link = svg.selectAll(".link")
.data(json.edges)
.enter()
.append("line")
.attr("class", "link")
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
var node = svg.selectAll(".node")
.data(json.nodes)
.enter()
.append("g")
.attr("class", "node")
.call(force.drag);
processNormalNodes(node);
processNumberNodes(node);
processStringNodes(node);
processImageNodes(node);
processUrlNodes(node);
processObjectNodes(node);
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; });
// add from visual-rdf.js
//link.attr("x", function(d) { return (d.source.x+d.target.x)/2; })
// .attr("y", function(d) { return (d.source.y+d.target.y)/2; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
// visual-rdf.js
//linkArrowhead.attr("points", function(d) {
// return [[d.target.x,d.target.y+10].join(","),
// [d.target.x-3,d.target.y+16].join(","),
// [d.target.x+3,d.target.y+16].join(",")].join(" ");
// })
// .attr("transform",function(d) {
// angle = Math.atan2(d.target.y-d.source.y, d.target.x-d.source.x)*180/Math.PI + 90;
// return "rotate("+angle+", "+d.target.x+", "+d.target.y+")";
// });
}); // force.on(tick)
});
}
</script>
</body>
</html>