10 changed files with 392 additions and 0 deletions
@ -0,0 +1,59 @@
|
||||
.links line { |
||||
stroke: #999; |
||||
stroke-opacity: 0.6; |
||||
} |
||||
|
||||
.nodes circle { |
||||
stroke: #fff; |
||||
stroke-width: 1.5px; |
||||
} |
||||
|
||||
#iho-graph { |
||||
/*overflow: scroll; |
||||
width: 100%; |
||||
height: 2200px;*/ |
||||
} |
||||
|
||||
text { |
||||
text-baseline: middle; |
||||
text-anchor: middle; |
||||
font: 10px sans-serif; |
||||
/*pointer-events: none;*/ |
||||
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff; |
||||
} |
||||
|
||||
text a { |
||||
fill: navy; |
||||
} |
||||
|
||||
text a:visited { |
||||
fill: darkpurple; |
||||
} |
||||
|
||||
text a:hover, |
||||
text a:active { |
||||
text-decoration: underline; |
||||
fill: darkred; |
||||
} |
||||
|
||||
uri { |
||||
text-baseline: middle; |
||||
text-anchor: middle; |
||||
font: 10px sans-serif; |
||||
/*pointer-events: none;*/ |
||||
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff; |
||||
} |
||||
|
||||
uri a { |
||||
fill: navy; |
||||
} |
||||
|
||||
uri a:visited { |
||||
fill: darkpurple; |
||||
} |
||||
|
||||
uri a:hover, |
||||
text a:active { |
||||
text-decoration: underline; |
||||
fill: darkred; |
||||
} |
||||
@ -0,0 +1,27 @@
|
||||
d3js: |
||||
remote: https://github.com/d3/d3 |
||||
header: true |
||||
version: 4.13.0 |
||||
license: |
||||
name: BSD-3 |
||||
url: https://github.com/d3/d3/blob/master/LICENSE |
||||
gpl-compatible: true |
||||
js: |
||||
/libraries/d3/d3.min.js: {} |
||||
fdg-graph: |
||||
version: 1.x |
||||
css: |
||||
base: |
||||
css/fdg-graph.css: {} |
||||
js: |
||||
js/fdg.js: {} |
||||
d3-selection: |
||||
remote: https://github.com/d3/d3-selection |
||||
header: true |
||||
version: 1.3.0 |
||||
license: |
||||
name: BSD-3 |
||||
url: https://github.com/d3/d3-selection/blob/master/LICENSE |
||||
gpl-compatible: true |
||||
js: |
||||
/libraries/d3-selection/d3-selection.min.js: {} |
||||
@ -0,0 +1,14 @@
|
||||
islandora_herbarium_object.graph: |
||||
path: '/islandora_herbarium_object/fdg' |
||||
defaults: |
||||
_controller: '\Drupal\islandora_herbarium_object\Controller\GraphController::printFdgGraph' |
||||
_title: 'Graph' |
||||
requirements: |
||||
_permission: 'access content' |
||||
islandora_herbarium_object.graph-json: |
||||
path: '/islandora_herbarium_object/fdg_json' |
||||
defaults: |
||||
_controller: '\Drupal\islandora_herbarium_object\Controller\GraphController::printFdgData' |
||||
_title: 'Graph Json' |
||||
requirements: |
||||
_permission: 'access content' |
||||
@ -0,0 +1,140 @@
|
||||
var svg = d3.select(".fdg"), |
||||
width = +svg.attr("width"), |
||||
height = +svg.attr("height"); |
||||
|
||||
var color = d3.scaleOrdinal(d3.schemeCategory20); |
||||
|
||||
var simulation = d3.forceSimulation() |
||||
.force("link", d3.forceLink().id(function (d) { |
||||
return d.id; |
||||
})) |
||||
.force("charge", d3.forceManyBody()) |
||||
.force("center", d3.forceCenter(width / 2, height / 2)); |
||||
|
||||
d3.json("/islandora_herbarium_object/fdg_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); |
||||
}); |
||||
|
||||
var node = svg.append("g") |
||||
.attr("class", "nodes") |
||||
.selectAll("circle") |
||||
.data(graph.nodes) |
||||
.enter().append("circle") |
||||
.attr("r", 12) |
||||
.attr("fill", function (d) { |
||||
return color(d.group); |
||||
}) |
||||
.call(d3.drag() |
||||
.on("start", dragstarted) |
||||
.on("drag", dragged) |
||||
.on("end", dragended)); |
||||
|
||||
node.append("title") |
||||
.text(function (d) { |
||||
return d.id; |
||||
}); |
||||
|
||||
/*var textElement = svg.append('g') |
||||
.attr('class', 'text') |
||||
.selectAll('text') |
||||
.data(graph.nodes) |
||||
.enter().append('text') |
||||
.text(function(d) {return d.name;}) |
||||
.attr('font-size', 10) |
||||
.attr('font-weight', 'bolder') |
||||
.attr('dx', 15) |
||||
.attr('dy', 4)*/ |
||||
|
||||
var uriElement = svg.append('g') |
||||
.attr('class', 'uri') |
||||
.attr('x', 8) |
||||
.attr('y', '.31em') |
||||
.selectAll('uri') |
||||
.data(graph.nodes) |
||||
.enter().append('a') |
||||
.attr("target", "_blank") |
||||
.attr('xlink:href', function (d) { |
||||
return d.label |
||||
}) |
||||
.append('text') |
||||
.text(function (d) { |
||||
return d.name; |
||||
}) |
||||
.attr('font-size', 10) |
||||
.attr('font-weight', 'bolder'); |
||||
//.attr('dx', 15)
|
||||
//.attr('dy', 28);
|
||||
//uriElement.select("a")
|
||||
// .attr("xlink:href", function(d) {
|
||||
// return d.label;
|
||||
// });
|
||||
|
||||
simulation |
||||
.nodes(graph.nodes) |
||||
.on("tick", ticked); |
||||
|
||||
simulation.force("link") |
||||
.links(graph.links); |
||||
|
||||
|
||||
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("cx", function (d) { |
||||
return d.x; |
||||
}) |
||||
.attr("cy", function (d) { |
||||
return d.y; |
||||
}); |
||||
|
||||
//textElement
|
||||
// .attr("dx", function(d) {return d.x})
|
||||
//.attr("dy", function(d) { return d.y; });
|
||||
|
||||
uriElement |
||||
.attr("dx", function (d) { |
||||
return d.x |
||||
}) |
||||
.attr("dy", function (d) { |
||||
return 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; |
||||
} |
||||
@ -0,0 +1,41 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\islandora_herbarium_object\Controller; |
||||
|
||||
use Drupal\Core\Controller\ControllerBase; |
||||
|
||||
/** |
||||
* Controller for d3 graphs. |
||||
*/ |
||||
class GraphController extends ControllerBase { |
||||
|
||||
/** |
||||
* Display the markup. |
||||
* |
||||
* @return array |
||||
* A renderable array. |
||||
*/ |
||||
public function printFdgGraph() { |
||||
return [ |
||||
'#theme' => 'fdg_graph', |
||||
'#attached' => [ |
||||
'library' => [ |
||||
'islandora_herbarium_object/d3js', |
||||
'islandora_herbarium_object/d3-selection', |
||||
'islandora_herbarium_object/fdg-graph', |
||||
], |
||||
], |
||||
]; |
||||
} |
||||
|
||||
/** |
||||
* Directly outputs the json for calls from javascript. |
||||
*/ |
||||
public function printFdgData() { |
||||
$fdgGraph = \Drupal::service('islandora_herbarium_object.fdggraph'); |
||||
$graphJson = $fdgGraph->parseData(); |
||||
print $graphJson; |
||||
exit(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,89 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\islandora_herbarium_object\Graph; |
||||
|
||||
use Drupal\Core\Url; |
||||
|
||||
/** |
||||
* Class FdgGraph. |
||||
* |
||||
* Retrieve and parse data from the triple store for force directed graph. |
||||
* |
||||
* @package Drupal\islandora_herbarium_object\Graph |
||||
*/ |
||||
class FdgGraph { |
||||
|
||||
protected $data; |
||||
|
||||
/** |
||||
* Constructor. |
||||
*/ |
||||
public function __construct() { |
||||
$this->getData(); |
||||
} |
||||
|
||||
/** |
||||
* Parse the data retrieved from the triple store for use in a d3 graph. |
||||
* |
||||
* @return string |
||||
* A json encode string. |
||||
*/ |
||||
public function parseData() { |
||||
$jsonArr = json_decode($this->data, TRUE); |
||||
$outputArr = []; |
||||
// $nodes = $links = new \Ds\Set(); |
||||
$outputArr['nodes'] = []; |
||||
$outputArr['links'] = []; |
||||
$idArr = []; |
||||
|
||||
foreach ($jsonArr['results']['bindings'] as $result) { |
||||
if (!array_key_exists($result['municipality']['value'], $idArr)) { |
||||
$outputArr['nodes'][] = [ |
||||
'id' => $result['municipality']['value'], |
||||
'name' => $result['municipality']['value'], |
||||
'label' => $result['municipalityIRI']['value'], |
||||
'group' => 1, |
||||
]; |
||||
$idArr[$result['municipality']['value']] = ''; |
||||
} |
||||
$outputArr['nodes'][] = [ |
||||
'id' => $result['name']['value'], |
||||
'name' => $result['name']['value'], |
||||
'label' => $result['scientificNameIRI']['value'], |
||||
'group' => 2, |
||||
]; |
||||
$outputArr['links'][] = [ |
||||
'target' => $result['municipality']['value'], |
||||
'source' => $result['name']['value'], |
||||
'type' => 'found', |
||||
]; |
||||
} |
||||
return json_encode($outputArr); |
||||
} |
||||
|
||||
/** |
||||
* Retrieve data from the triple store. |
||||
*/ |
||||
public function getData() { |
||||
$sparqlQuery = 'SELECT DISTINCT ?municipalityIRI ?scientificNameIRI ?name ?municipality where { ?s <dwciri:municipality> ?municipalityIRI . |
||||
?s <dwciri:scientificName> ?scientificNameIRI . |
||||
?s <dwc:scientificName> ?name. |
||||
?s <dwc:municipality> ?municipality |
||||
}'; |
||||
|
||||
$uri = 'http://localhost:8080/bigdata/namespace/islandora/sparql'; |
||||
$options = ['query' => ['query' => $sparqlQuery]]; |
||||
$url = Url::fromUri($uri, $options); |
||||
$httpClient = \Drupal::httpClient(); |
||||
$requestUrl = $url->toString(); |
||||
$request = $httpClient->get($requestUrl, [ |
||||
'headers' => ['Accept' => 'application/sparql-results+json'], |
||||
]); |
||||
$data = $request->getBody()->getContents(); |
||||
if (empty($data)) { |
||||
throw new Exception("Empty dataset"); |
||||
} |
||||
$this->data = $data; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue