-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgraph.html
245 lines (208 loc) · 8.26 KB
/
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
<html>
<head>
<meta charset="UTF-8">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.0.min.js" crossorigin="anonymous"></script>
<script src="dist/uduvudu.js" type="text/javascript"></script>
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<div class="container">
<!-- location where to render to -->
<div class="row">
<div id="main">
</div>
</div>
</div>
<!-- where the templates are injected -->
<div id="templates">
<!-- container for literals -->
<script id="literals" type="text/uduvudu-template">
<style type="text/css">
path.link {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
marker#type {
fill: red;
}
path.link.type {
stroke: red;
}
path.link.schema {
stroke: green;
}
circle {
fill: #ccc;
stroke: #333;
stroke-width: 1.5px;
}
circle.literal {
fill: white;
stroke: none;
}
text {
font: 14px sans-serif;
pointer-events: none;
x: 8px;
y: 0.31em;
}
text.shadow {
stroke: #fff;
stroke-width: 3px;
stroke-opacity: .8;
}
text.literal {
text-anchor: middle;
background-color: #ccc;
}
</style>
<% window.types = {"uri": 200, "type": 300, "schema": 200, "literal": 100}; %>
<% window.links = []; %>
<% _.each(literals, function(e) {print(template(e.literal))}); %>
</script>
<!-- container for literals -->
<script id="unknowns" type="text/uduvudu-template">
<% _.each(unknowns, function(e) {print(template(e.unknown))}); %>
</script>
<script id="unknowns_js" type="text/uduvudu-template">
var nodes = {};
// Compute the distinct nodes from the links.
links.forEach(function(link) {
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source, type: link.type});
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, type: link.type});
});
var w = $(window).width(),
h = $(window).height();
var force = d3.layout.force()
.size([w, h])
.linkDistance(function (d) {return types[d.type]})
.charge(-700)
.on("tick", tick)
force
.nodes(d3.values(nodes))
.links(links)
.start();
var drag = force.drag()
.on("dragstart", dragstart);
var svg = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h);
// Per-type markers, as they don't inherit styles.
svg.append("svg:defs").selectAll("marker")
.data(["type", "schema", "uri", "literal"])
.enter().append("svg:marker")
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 22)
.attr("refY", -.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
var path = svg.append("svg:g").selectAll("path")
.data(force.links())
.enter().append("svg:path")
.attr("id", function(d) { return d.source.index + "_" + d.target.index; })
.attr("class", function(d) { return "link " + d.type; })
.attr("marker-end", function(d) { return "url(#" + d.type + ")"; });
var circle = svg.append("svg:g").selectAll("circle")
.data(force.nodes())
.enter().append("svg:circle")
.attr("r", 10)
.on("dblclick", dblclick)
.attr("class", function(d) { return "link " + d.type; })
.classed("literal", function (d) {return d.type === "literal";})
.call(force.drag);
var text = svg.append("svg:g").selectAll("g")
.data(force.nodes())
.enter().append("svg:g");
// A copy of the text with a thick white stroke for legibility.
text.append("svg:text")
.attr("x", function (d) { return (d.type === "literal")?0:12;})
.attr("y", ".31em")
.classed("literal", function (d) {return d.type === "literal";})
.classed("shadow", true)
.text(function(d) { return d.name; });
text.append("svg:text")
.attr("x", function (d) {return (d.type === "literal")?0:12;})
.attr("y", ".31em")
.classed("literal", function (d) {return d.type === "literal";})
.text(function(d) { return d.name; });
var path_label = svg.append("svg:g").selectAll(".path_label")
.data(force.links() )
.enter().append("svg:text")
.attr("class", "path_label")
.append("svg:textPath")
.attr("startOffset", "50%")
.attr("baseline-shift", "3px")
.attr("text-anchor", "middle")
.attr("xlink:href", function(d) { return "#" + d.source.index + "_" + d.target.index; })
.style("fill", "#000")
.style("font-family", "Arial")
.text(function(d) { return d.edge; });
function dragstart(d) {
d3.select(this).classed("fixed", d.fixed = true);
}
function dblclick(d) {
d3.select(this).classed("fixed", d.fixed = false);
}
// Use elliptical arc path segments to doubly-encode directionality.
function tick() {
path.attr("d", function(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy);
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
});
circle.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
text.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
</script>
<!-- literal -->
<script id="literal" type="text/uduvudu-template">
<% window.links.push({source: literal.s.u, edge: literal.p.u, target: literal.o.u, type: "literal"}); %>
</script>
<!-- unknown -->
<script id="unknown" type="text/uduvudu-template">
<% window.links.push({source: unknown.s.u, edge: unknown.p.u, target: unknown.o.u, type: "uri"}); %>
</script>
</div>
<script type="text/javascript">
// instantiate rdf-ext object
var store = new rdf.LdpStore();
var source = 'http://www.w3.org/People/Berners-Lee/card';
var source_query = 'http://dbpedia.org/sparql?query=CONSTRUCT%20%7B%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FBern%3E%20%3Fp%20%3Fo.%20%3Fo%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23label%3E%20%3Fl%7D%20WHERE%20%7B%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FBern%3E%20%3Fp%20%3Fo.%20OPTIONAL%20%7B%3Fo%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23label%3E%20%3Fl.%7D%7D&format=text%2Fturtle'
// something about what is going on
document.getElementById('main').innerHTML = ''
+ '<div class="alert alert-info">'
+ ' <button type="button" class="close" data-dismiss="alert">×</button>'
+ ' <strong>Loading</strong> '+source+' is being loaded ...'
+ '</div>';
// load triples
store.graph(source, function (error, graph) {
if (error == null) {
console.debug("successfully loaded "+graph.toArray().length+" triples");
// resource (entry for template search) is same as source in this example
uduvudu.process(graph, {'resource': source} , function (out) {
// write the result of the processing in the main div
$('#main').html(out);
});
} else {
console.log(error)
document.getElementById('main').innerHTML = ''
+ '<div class="alert alert-danger">'
+ ' <button type="button" class="close" data-dismiss="alert">×</button>'
+ ' <strong>Error:</strong> '+ error +'.'
+ '</div>';
};
});
</script>
</body>
</html>