-
Notifications
You must be signed in to change notification settings - Fork 76
/
d3sparql.js
2050 lines (1821 loc) · 59.4 KB
/
d3sparql.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// d3sparql.js - utilities for visualizing SPARQL results with the D3 library
//
// Web site: http://github.com/ktym/d3sparql/
// Copyright: 2013-2015 (C) Toshiaki Katayama (ktym@dbcls.jp)
// License: BSD license (same as D3.js)
// Initial version: 2013-01-28
//
var d3sparql = {
version: "d3sparql.js version 2018-05-04",
debug: false // set to true for showing debug information
}
/*
Execute a SPARQL query and pass the result to a given callback function
Synopsis:
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="d3sparql.js"></script>
<script>
function exec() {
var endpoint = d3.select("#endpoint").property("value")
var sparql = d3.select("#sparql").property("value")
d3sparql.query(endpoint, sparql, render)
}
function render(json) {
// set options and call the d3sparql.xxxxx visualization methods in this library ...
var config = {
"margin": {"top": 10, "right": 10, "bottom": 10, "left": 10},
"selector": "#result",
...
}
d3sparql.xxxxx(json, config)
}
</script>
<style>
<!-- customize CSS -->
</style>
</head>
<body onload="exec()">
<form style="display:none">
<input id="endpoint" value="http://dbpedia.org/sparql" type="text">
<textarea id="sparql">
PREFIX ...
SELECT ...
WHERE { ... }
</textarea>
</form>
<div id="result"></div>
</body>
</html>
*/
d3sparql.fetch = function(url, callback) {
if (d3sparql.debug) { console.log(url) }
var mime = "application/sparql-results+json"
d3.xhr(url, mime, function(request) {
var json = request.responseText
if (d3sparql.debug) { console.log(json) }
callback(JSON.parse(json))
})
/*
// d3.json sometimes fails to retrieve "application/sparql-results+json" as it is designed for "application/json"
d3.json(url, function(error, json) {
if (d3sparql.debug) { console.log(error) }
if (d3sparql.debug) { console.log(json) }
callback(json)
})
*/
}
d3sparql.query = function(endpoint, sparql, callback) {
var url = endpoint + "?query=" + encodeURIComponent(sparql)
if (d3sparql.debug) { console.log(endpoint) }
d3sparql.fetch(url, callback)
}
/*
Convert sparql-results+json object into a JSON graph in the {"nodes": [], "links": []} form.
Suitable for d3.layout.force(), d3.layout.sankey() etc.
Options:
config = {
"key1": "node1", // SPARQL variable name for node1 (optional; default is the 1st variable)
"key2": "node2", // SPARQL variable name for node2 (optional; default is the 2nd varibale)
"label1": "node1label", // SPARQL variable name for the label of node1 (optional; default is the 3rd variable)
"label2": "node2label", // SPARQL variable name for the label of node2 (optional; default is the 4th variable)
"value1": "node1value", // SPARQL variable name for the value of node1 (optional; default is the 5th variable)
"value2": "node2value" // SPARQL variable name for the value of node2 (optional; default is the 6th variable)
}
Synopsis:
d3sparql.query(endpoint, sparql, render)
function render(json) {
var config = { ... }
d3sparql.forcegraph(json, config)
d3sparql.sankey(json, config)
}
TODO:
Should follow the convention in the miserables.json https://gist.github.com/mbostock/4062045 to contain group for nodes and value for edges.
*/
d3sparql.graph = function(json, config) {
config = config || {}
var head = json.head.vars
var data = json.results.bindings
var opts = {
"key1": config.key1 || head[0] || "key1",
"key2": config.key2 || head[1] || "key2",
"label1": config.label1 || head[2] || false,
"label2": config.label2 || head[3] || false,
"value1": config.value1 || head[4] || false,
"value2": config.value2 || head[5] || false,
}
var graph = {
"nodes": [],
"links": []
}
var check = d3.map()
var index = 0
for (var i = 0; i < data.length; i++) {
var key1 = data[i][opts.key1].value
var key2 = data[i][opts.key2].value
var label1 = opts.label1 ? data[i][opts.label1].value : key1
var label2 = opts.label2 ? data[i][opts.label2].value : key2
var value1 = opts.value1 ? data[i][opts.value1].value : false
var value2 = opts.value2 ? data[i][opts.value2].value : false
if (!check.has(key1)) {
graph.nodes.push({"key": key1, "label": label1, "value": value1})
check.set(key1, index)
index++
}
if (!check.has(key2)) {
graph.nodes.push({"key": key2, "label": label2, "value": value2})
check.set(key2, index)
index++
}
graph.links.push({"source": check.get(key1), "target": check.get(key2)})
}
if (d3sparql.debug) { console.log(JSON.stringify(graph)) }
return graph
}
/*
Convert sparql-results+json object into a JSON tree of {"name": name, "value": size, "children": []} format like in the flare.json file.
Suitable for d3.layout.hierarchy() family
* cluster: d3sparql.dendrogram()
* pack: d3sparql.circlepack()
* partition: d3sparql.sunburst()
* tree: d3sparql.roundtree()
* treemap: d3sparql.treemap(), d3sparql.treemapzoom()
Options:
config = {
"root": "root_name", // SPARQL variable name for root node (optional; default is the 1st variable)
"parent": "parent_name", // SPARQL variable name for parent node (optional; default is the 2nd variable)
"child": "child_name", // SPARQL variable name for child node (ptional; default is the 3rd variable)
"value": "value_name" // SPARQL variable name for numerical value of the child node (optional; default is the 4th variable or "value")
}
Synopsis:
d3sparql.sparql(endpoint, sparql, render)
function render(json) {
var config = { ... }
d3sparql.roundtree(json, config)
d3sparql.dendrogram(json, config)
d3sparql.sunburst(json, config)
d3sparql.treemap(json, config)
d3sparql.treemapzoom(json, config)
}
*/
d3sparql.tree = function(json, config) {
config = config || {}
var head = json.head.vars
var data = json.results.bindings
var opts = {
"root": config.root || head[0],
"parent": config.parent || head[1],
"child": config.child || head[2],
"value": config.value || head[3] || "value",
}
var pair = d3.map()
var size = d3.map()
var root = data[0][opts.root].value
var parent = child = children = true
for (var i = 0; i < data.length; i++) {
parent = data[i][opts.parent].value
child = data[i][opts.child].value
if (parent != child) {
if (pair.has(parent)) {
children = pair.get(parent)
children.push(child)
} else {
children = [child]
}
pair.set(parent, children)
if (data[i][opts.value]) {
size.set(child, data[i][opts.value].value)
}
}
}
function traverse(node) {
var list = pair.get(node)
if (list) {
var children = list.map(function(d) { return traverse(d) })
// sum of values of children
var subtotal = d3.sum(children, function(d) { return d.value })
// add a value of parent if exists
var total = d3.sum([subtotal, size.get(node)])
return {"name": node, "children": children, "value": total}
} else {
return {"name": node, "value": size.get(node) || 1}
}
}
var tree = traverse(root)
if (d3sparql.debug) { console.log(JSON.stringify(tree)) }
return tree
}
/*
Rendering sparql-results+json object containing multiple rows into a HTML table
Options:
config = {
"selector": "#result"
}
Synopsis:
d3sparql.query(endpoint, sparql, render)
function render(json) {
var config = { ... }
d3sparql.htmltable(json, config)
}
CSS:
<style>
table {
margin: 10px;
}
th {
background: #eeeeee;
}
th:first-letter {
text-transform: capitalize;
}
</style>
*/
d3sparql.htmltable = function(json, config) {
config = config || {}
var head = json.head.vars
var data = json.results.bindings
var opts = {
"selector": config.selector || null
}
var table = d3sparql.select(opts.selector, "htmltable").append("table").attr("class", "table table-bordered")
var thead = table.append("thead")
var tbody = table.append("tbody")
thead.append("tr")
.selectAll("th")
.data(head)
.enter()
.append("th")
.text(function(col) { return col })
var rows = tbody.selectAll("tr")
.data(data)
.enter()
.append("tr")
var cells = rows.selectAll("td")
.data(function(row) {
return head.map(function(col) {
return row[col] ? row[col].value : ""
})
})
.enter()
.append("td")
.text(function(val) { return val })
// default CSS
table.style({
"margin": "10px"
})
table.selectAll("th").style({
"background": "#eeeeee",
"text-transform": "capitalize",
})
}
/*
Rendering sparql-results+json object containing one row into a HTML table
Options:
config = {
"selector": "#result"
}
Synopsis:
d3sparql.query(endpoint, sparql, render)
function render(json) {
var config = { ... }
d3sparql.htmlhash(json, config)
}
CSS:
<style>
table {
margin: 10px;
}
th {
background: #eeeeee;
}
th:first-letter {
text-transform: capitalize;
}
</style>
*/
d3sparql.htmlhash = function(json, config) {
config = config || {}
var head = json.head.vars
var data = json.results.bindings[0]
var opts = {
"selector": config.selector || null
}
var table = d3sparql.select(opts.selector, "htmlhash").append("table").attr("class", "table table-bordered")
var tbody = table.append("tbody")
var row = tbody.selectAll("tr")
.data(function() {
return head.map(function(col) {
return {"head": col, "data": data[col] ? data[col].value : ""}
})
})
.enter()
.append("tr")
row.append("th")
.text(function(d) { return d.head })
row.append("td")
.text(function(d) { return d.data })
// default CSS
table.style({
"margin": "10px"
})
table.selectAll("th").style({
"background": "#eeeeee",
"text-transform": "capitalize",
})
}
/*
Rendering sparql-results+json object into a bar chart
References:
http://bl.ocks.org/mbostock/3885304
http://bl.ocks.org/mbostock/4403522
Options:
config = {
"label_x": "Prefecture", // label for x-axis (optional; default is same as var_x)
"label_y": "Area", // label for y-axis (optional; default is same as var_y)
"var_x": "pref", // SPARQL variable name for x-axis (optional; default is the 1st variable)
"var_y": "area", // SPARQL variable name for y-axis (optional; default is the 2nd variable)
"width": 850, // canvas width (optional)
"height": 300, // canvas height (optional)
"margin": 40, // canvas margin (optional)
"selector": "#result"
}
Synopsis:
d3sparql.query(endpoint, sparql, render)
function render(json) {
var config = { ... }
d3sparql.barchart(json, config)
}
CSS/SVG:
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
</style>
*/
d3sparql.barchart = function(json, config) {
config = config || {}
var head = json.head.vars
var data = json.results.bindings
var opts = {
"label_x": config.label_x || head[0],
"label_y": config.label_y || head[1],
"var_x": config.var_x || head[0],
"var_y": config.var_y || head[1],
"width": config.width || 750,
"height": config.height || 300,
"margin": config.margin || 80, // TODO: to make use of {top: 10, right: 10, bottom: 80, left: 80}
"selector": config.selector || null
}
var scale_x = d3.scale.ordinal().rangeRoundBands([0, opts.width - opts.margin], 0.1)
var scale_y = d3.scale.linear().range([opts.height - opts.margin, 0])
var axis_x = d3.svg.axis().scale(scale_x).orient("bottom")
var axis_y = d3.svg.axis().scale(scale_y).orient("left") // .ticks(10, "%")
scale_x.domain(data.map(function(d) { return d[opts.var_x].value }))
scale_y.domain(d3.extent(data, function(d) { return parseInt(d[opts.var_y].value) }))
var svg = d3sparql.select(opts.selector, "barchart").append("svg")
.attr("width", opts.width)
.attr("height", opts.height)
// .append("g")
// .attr("transform", "translate(" + opts.margin + "," + 0 + ")")
var ax = svg.append("g")
.attr("class", "axis x")
.attr("transform", "translate(" + opts.margin + "," + (opts.height - opts.margin) + ")")
.call(axis_x)
var ay = svg.append("g")
.attr("class", "axis y")
.attr("transform", "translate(" + opts.margin + ",0)")
.call(axis_y)
var bar = svg.selectAll(".bar")
.data(data)
.enter()
.append("rect")
.attr("transform", "translate(" + opts.margin + "," + 0 + ")")
.attr("class", "bar")
.attr("x", function(d) { return scale_x(d[opts.var_x].value) })
.attr("width", scale_x.rangeBand())
.attr("y", function(d) { return scale_y(d[opts.var_y].value) })
.attr("height", function(d) { return opts.height - scale_y(parseInt(d[opts.var_y].value)) - opts.margin })
/*
.call(function(e) {
e.each(function(d) {
console.log(parseInt(d[opts.var_y].value))
})
})
*/
ax.selectAll("text")
.attr("dy", ".35em")
.attr("x", 10)
.attr("y", 0)
.attr("transform", "rotate(90)")
.style("text-anchor", "start")
ax.append("text")
.attr("class", "label")
.text(opts.label_x)
.style("text-anchor", "middle")
.attr("transform", "translate(" + ((opts.width - opts.margin) / 2) + "," + (opts.margin - 5) + ")")
ay.append("text")
.attr("class", "label")
.text(opts.label_y)
.style("text-anchor", "middle")
.attr("transform", "rotate(-90)")
.attr("x", 0 - (opts.height / 2))
.attr("y", 0 - (opts.margin - 20))
// default CSS/SVG
bar.attr({
"fill": "steelblue",
})
svg.selectAll(".axis").attr({
"stroke": "black",
"fill": "none",
"shape-rendering": "crispEdges",
})
svg.selectAll("text").attr({
"stroke": "none",
"fill": "black",
"font-size": "8pt",
"font-family": "sans-serif",
})
}
/*
Rendering sparql-results+json object into a pie chart
References:
http://bl.ocks.org/mbostock/3887235 Pie chart
http://bl.ocks.org/mbostock/3887193 Donut chart
Options:
config = {
"label": "pref", // SPARQL variable name for slice label (optional; default is the 1st variable)
"size": "area", // SPARQL variable name for slice value (optional; default is the 2nd variable)
"width": 700, // canvas width (optional)
"height": 600, // canvas height (optional)
"margin": 10, // canvas margin (optional)
"hole": 50, // radius size of a center hole (optional; 0 for pie, r > 0 for doughnut)
"selector": "#result"
}
Synopsis:
d3sparql.query(endpoint, sparql, render)
function render(json) {
var config = { ... }
d3sparql.piechart(json, config)
}
CSS/SVG:
<style>
.label {
font: 10px sans-serif;
}
.arc path {
stroke: #ffffff;
}
</style>
*/
d3sparql.piechart = function(json, config) {
config = config || {}
var head = json.head.vars
var data = json.results.bindings
var opts = {
"label": config.label || head[0],
"size": config.size || head[1],
"width": config.width || 700,
"height": config.height || 700,
"margin": config.margin || 10,
"hole": config.hole || 100,
"selector": config.selector || null
}
var radius = Math.min(opts.width, opts.height) / 2 - opts.margin
var hole = Math.max(Math.min(radius - 50, opts.hole), 0)
var color = d3.scale.category20()
var arc = d3.svg.arc()
.outerRadius(radius)
.innerRadius(hole)
var pie = d3.layout.pie()
//.sort(null)
.value(function(d) { return d[opts.size].value })
var svg = d3sparql.select(opts.selector, "piechart").append("svg")
.attr("width", opts.width)
.attr("height", opts.height)
.append("g")
.attr("transform", "translate(" + opts.width / 2 + "," + opts.height / 2 + ")")
var g = svg.selectAll(".arc")
.data(pie(data))
.enter()
.append("g")
.attr("class", "arc")
var slice = g.append("path")
.attr("d", arc)
.attr("fill", function(d, i) { return color(i) })
var text = g.append("text")
.attr("class", "label")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")" })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) { return d.data[opts.label].value })
// default CSS/SVG
slice.attr({
"stroke": "#ffffff",
})
// TODO: not working?
svg.selectAll("text").attr({
"stroke": "none",
"fill": "black",
"font-size": "20px",
"font-family": "sans-serif",
})
}
/*
Rendering sparql-results+json object into a scatter plot
References:
http://bl.ocks.org/mbostock/3244058
Options:
config = {
"label_x": "Size", // label for x-axis (optional; default is same as var_x)
"label_y": "Count", // label for y-axis (optional; default is same as var_y)
"var_x": "size", // SPARQL variable name for x-axis values (optional; default is the 1st variable)
"var_y": "count", // SPARQL variable name for y-axis values (optional; default is the 2nd variable)
"var_r": "volume", // SPARQL variable name for radius (optional; default is the 3rd variable)
"min_r": 1, // minimum radius size (optional; default is 1)
"max_r": 20, // maximum radius size (optional; default is 20)
"width": 850, // canvas width (optional)
"height": 300, // canvas height (optional)
"margin_x": 80, // canvas margin x (optional)
"margin_y": 40, // canvas margin y (optional)
"selector": "#result"
}
Synopsis:
d3sparql.query(endpoint, sparql, render)
function render(json) {
var config = { ... }
d3sparql.scatterplot(json, config)
}
CSS/SVG:
<style>
.label {
font-size: 10pt;
}
.node circle {
stroke: black;
stroke-width: 1px;
fill: pink;
opacity: 0.5;
}
</style>
*/
d3sparql.scatterplot = function(json, config) {
config = config || {}
var head = json.head.vars
var data = json.results.bindings
var opts = {
"label_x": config.label_x || head[0] || "x",
"label_y": config.label_y || head[1] || "y",
"label_r": config.label_r || head[2] || "r",
"var_x": config.var_x || head[0],
"var_y": config.var_y || head[1],
"var_r": config.var_r || head[2] || 5,
"min_r": config.min_r || 1,
"max_r": config.max_r || 20,
"width": config.width || 850,
"height": config.height || 300,
"margin_x": config.margin_x || 80,
"margin_y": config.margin_y || 40,
"selector": config.selector || null
}
var extent_x = d3.extent(data, function(d) { return parseInt(d[opts.var_x].value) })
var extent_y = d3.extent(data, function(d) { return parseInt(d[opts.var_y].value) })
var extent_r = d3.extent(data, function(d) { return parseInt(d[opts.var_r] ? d[opts.var_r].value : opts.var_r) })
var scale_x = d3.scale.linear().range([opts.margin_x, opts.width - opts.margin_x]).domain(extent_x)
var scale_y = d3.scale.linear().range([opts.height - opts.margin_y, opts.margin_y]).domain(extent_y)
var scale_r = d3.scale.linear().range([opts.min_r, opts.max_r]).domain(extent_r)
var axis_x = d3.svg.axis().scale(scale_x)
var axis_y = d3.svg.axis().scale(scale_y).orient("left")
var svg = d3sparql.select(opts.selector, "scatterplot").append("svg")
.attr("width", opts.width)
.attr("height", opts.height)
var circle = svg.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("class", "node")
.attr("cx", function(d) { return scale_x(d[opts.var_x].value) })
.attr("cy", function(d) { return scale_y(d[opts.var_y].value) })
.attr("r", function(d) { return scale_r(d[opts.var_r] ? d[opts.var_r].value : opts.var_r) })
.attr("opacity", 0.5)
.append("title")
.text(function(d) { return d[opts.label_r] ? d[opts.label_r].value : opts.label_r })
var ax = svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (opts.height - opts.margin_y) + ")")
.call(axis_x)
var ay = svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + opts.margin_x + ",0)")
.call(axis_y)
ax.append("text")
.attr("class", "label")
.text(opts.label_x)
.style("text-anchor", "middle")
.attr("transform", "translate(" + ((opts.width - opts.margin_x) / 2) + "," + (opts.margin_y - 5) + ")")
ay.append("text")
.attr("class", "label")
.text(opts.label_y)
.style("text-anchor", "middle")
.attr("transform", "rotate(-90)")
.attr("x", 0 - (opts.height / 2))
.attr("y", 0 - (opts.margin_x - 20))
// default CSS/SVG
ax.attr({
"stroke": "black",
"fill": "none",
})
ay.attr({
"stroke": "black",
"fill": "none",
})
// This doesn't work with .append("circle") with .append("title") for tooltip
circle.attr({
"stroke": "gray",
"stroke-width": "1px",
"fill": "lightblue",
"opacity": 0.5,
})
//svg.selectAll(".label").attr({
svg.selectAll("text").attr({
"stroke": "none",
"fill": "black",
"font-size": "8pt",
"font-family": "sans-serif",
})
}
/*
Rendering sparql-results+json object into a force graph
References:
http://bl.ocks.org/mbostock/4062045
Options:
config = {
"radius": 12, // static value or a function to calculate radius of nodes (optional)
"charge": -250, // force between nodes (optional; negative: repulsion, positive: attraction)
"distance": 30, // target distance between linked nodes (optional)
"width": 1000, // canvas width (optional)
"height": 500, // canvas height (optional)
"label": "name", // SPARQL variable name for node labels (optional)
"selector": "#result"
// options for d3sparql.graph() can be added here ...
}
Synopsis:
d3sparql.query(endpoint, sparql, render)
function render(json) {
var config = { ... }
d3sparql.forcegraph(json, config)
}
CSS/SVG:
<style>
.link {
stroke: #999999;
}
.node {
stroke: black;
opacity: 0.5;
}
circle.node {
stroke-width: 1px;
fill: lightblue;
}
text.node {
font-family: "sans-serif";
font-size: 8px;
}
</style>
TODO:
Try other d3.layout.force options.
*/
d3sparql.forcegraph = function(json, config) {
config = config || {}
var graph = (json.head && json.results) ? d3sparql.graph(json, config) : json
var scale = d3.scale.linear()
.domain(d3.extent(graph.nodes, function(d) { return parseFloat(d.value) }))
.range([1, 20])
var opts = {
"radius": config.radius || function(d) { return d.value ? scale(d.value) : 1 + d.label.length },
"charge": config.charge || -500,
"distance": config.distance || 50,
"width": config.width || 1000,
"height": config.height || 750,
"label": config.label || false,
"selector": config.selector || null
}
var svg = d3sparql.select(opts.selector, "forcegraph").append("svg")
.attr("width", opts.width)
.attr("height", opts.height)
var link = svg.selectAll(".link")
.data(graph.links)
.enter()
.append("line")
.attr("class", "link")
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter()
.append("g")
var circle = node.append("circle")
.attr("class", "node")
.attr("r", opts.radius)
var text = node.append("text")
.text(function(d) { return d[opts.label || "label"] })
.attr("class", "node")
var force = d3.layout.force()
.charge(opts.charge)
.linkDistance(opts.distance)
.size([opts.width, opts.height])
.nodes(graph.nodes)
.links(graph.links)
.start()
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 })
text.attr("x", function(d) { return d.x })
.attr("y", function(d) { return d.y })
circle.attr("cx", function(d) { return d.x })
.attr("cy", function(d) { return d.y })
})
node.call(force.drag)
// default CSS/SVG
link.attr({
"stroke": "#999999",
})
circle.attr({
"stroke": "black",
"stroke-width": "1px",
"fill": "lightblue",
"opacity": 1,
})
text.attr({
"font-size": "8px",
"font-family": "sans-serif",
})
}
/*
Rendering sparql-results+json object into a sanky graph
References:
https://github.com/d3/d3-plugins/tree/master/sankey
http://bost.ocks.org/mike/sankey/
Options:
config = {
"width": 1000, // canvas width (optional)
"height": 900, // canvas height (optional)
"margin": 50, // canvas margin (optional)
"selector": "#result"
// options for d3sparql.graph() can be added here ...
}
Synopsis:
d3sparql.query(endpoint, sparql, render)
function render(json) {
var config = { ... }
d3sparql.sankey(json, config)
}
CSS/SVG:
<style>
.node rect {
cursor: move;
fill-opacity: .9;
shape-rendering: crispEdges;
}
.node text {
pointer-events: none;
text-shadow: 0 1px 0 #ffffff;
}
.link {
fill: none;
stroke: #000000;
stroke-opacity: .2;
}
.link:hover {
stroke-opacity: .5;
}
</style>
Dependencies:
* sankey.js
* Download from https://github.com/d3/d3-plugins/tree/master/sankey
* Put <script src="sankey.js"></script> in the HTML <head> section
*/
d3sparql.sankey = function(json, config) {
config = config || {}
var graph = (json.head && json.results) ? d3sparql.graph(json, config) : json
var opts = {
"width": config.width || 750,
"height": config.height || 1200,
"margin": config.margin || 10,
"selector": config.selector || null
}
var nodes = graph.nodes
var links = graph.links
for (var i = 0; i < links.length; i++) {
links[i].value = 2 // TODO: fix to use values on links
}
var sankey = d3.sankey()
.size([opts.width, opts.height])
.nodeWidth(15)
.nodePadding(10)
.nodes(nodes)
.links(links)
.layout(32)
var path = sankey.link()
var color = d3.scale.category20()
var svg = d3sparql.select(opts.selector, "sankey").append("svg")
.attr("width", opts.width + opts.margin * 2)
.attr("height", opts.height + opts.margin * 2)
.append("g")
.attr("transform", "translate(" + opts.margin + "," + opts.margin + ")")
var link = svg.selectAll(".link")
.data(links)
.enter()
.append("path")
.attr("class", "link")
.attr("d", path)
.attr("stroke-width", function(d) { return Math.max(1, d.dy) })
.sort(function(a, b) { return b.dy - a.dy })
var node = svg.selectAll(".node")
.data(nodes)
.enter()
.append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")" })
.call(d3.behavior.drag()
.origin(function(d) { return d })
.on("dragstart", function() { this.parentNode.appendChild(this) })
.on("drag", dragmove)
)
node.append("rect")
.attr("width", function(d) { return d.dx })
.attr("height", function(d) { return d.dy })
.attr("fill", function(d) { return color(d.label) })
.attr("opacity", 0.5)
node.append("text")
.attr("x", -6)
.attr("y", function(d) { return d.dy/2 })
.attr("dy", ".35em")
.attr("text-anchor", "end")
.attr("transform", null)
.text(function(d) { return d.label })
.filter(function(d) { return d.x < opts.width / 2 })
.attr("x", 6 + sankey.nodeWidth())
.attr("text-anchor", "start")
// default CSS/SVG
link.attr({
"fill": "none",
"stroke": "grey",
"opacity": 0.5,
})
function dragmove(d) {
d3.select(this).attr("transform", "translate(" + d.x + "," + (d.y = Math.max(0, Math.min(opts.height - d.dy, d3.event.y))) + ")")
sankey.relayout()
link.attr("d", path)
}
}
/*
Rendering sparql-results+json object into a round tree
References:
http://bl.ocks.org/4063550 Reingold-Tilford Tree
Options:
config = {