diff --git a/css/fdg-graph.css b/css/fdg-graph.css new file mode 100644 index 0000000..90a9c7a --- /dev/null +++ b/css/fdg-graph.css @@ -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; +} diff --git a/islandora_herbarium_object.info.yml b/islandora_herbarium_object.info.yml index 15d8637..4126f24 100644 --- a/islandora_herbarium_object.info.yml +++ b/islandora_herbarium_object.info.yml @@ -14,3 +14,4 @@ dependencies: - user - geofield_map - openseadragon + - solr_facet_field_formatter diff --git a/islandora_herbarium_object.libraries.yml b/islandora_herbarium_object.libraries.yml new file mode 100644 index 0000000..612ebcb --- /dev/null +++ b/islandora_herbarium_object.libraries.yml @@ -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: {} diff --git a/islandora_herbarium_object.module b/islandora_herbarium_object.module index 3c5c197..8454c8e 100644 --- a/islandora_herbarium_object.module +++ b/islandora_herbarium_object.module @@ -35,6 +35,17 @@ function islandora_herbarium_object_rdf_namespaces() { ]; } +/** + * Implements hook_theme(). + */ +function islandora_herbarium_object_theme($existing, $type, $theme, $path) { + return [ + 'fdg_graph' => [ + 'variables' => [], + ], + ]; +} + /** * Implements hook_entity_presave(). */ diff --git a/islandora_herbarium_object.routing.yml b/islandora_herbarium_object.routing.yml new file mode 100644 index 0000000..2cbc51f --- /dev/null +++ b/islandora_herbarium_object.routing.yml @@ -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' diff --git a/islandora_herbarium_object.services.yml b/islandora_herbarium_object.services.yml index 66aaff8..2ce2947 100644 --- a/islandora_herbarium_object.services.yml +++ b/islandora_herbarium_object.services.yml @@ -3,3 +3,5 @@ services: class: Drupal\islandora_herbarium_object\DataParser\GbifParser islandora_herbarium_object.geoparser: class: Drupal\islandora_herbarium_object\DataParser\NrcanParser + islandora_herbarium_object.fdggraph: + class: Drupal\islandora_herbarium_object\Graph\FdgGraph diff --git a/js/fdg.js b/js/fdg.js new file mode 100644 index 0000000..844abc3 --- /dev/null +++ b/js/fdg.js @@ -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; +} diff --git a/src/Controller/GraphController.php b/src/Controller/GraphController.php new file mode 100644 index 0000000..8048775 --- /dev/null +++ b/src/Controller/GraphController.php @@ -0,0 +1,41 @@ + '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(); + } + +} diff --git a/src/Graph/FdgGraph.php b/src/Graph/FdgGraph.php new file mode 100644 index 0000000..ad036ea --- /dev/null +++ b/src/Graph/FdgGraph.php @@ -0,0 +1,89 @@ +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 ?municipalityIRI . + ?s ?scientificNameIRI . + ?s ?name. + ?s ?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; + } + +} diff --git a/templates/fdg-graph.html.twig b/templates/fdg-graph.html.twig new file mode 100644 index 0000000..5735f0f --- /dev/null +++ b/templates/fdg-graph.html.twig @@ -0,0 +1,8 @@ +
+ +
+ + + + +