Giter Site home page Giter Site logo

zazuko / d3-sparql Goto Github PK

View Code? Open in Web Editor NEW
115.0 9.0 11.0 104 KB

Query a SPARQL endpoint with a SELECT query and get the data ready to be used with d3js

License: BSD 3-Clause "New" or "Revised" License

JavaScript 100.00%
d3js sparql json rdf triplestore

d3-sparql's Introduction

d3-sparql

This module lets you request data from SPARQL endpoints in the vein of d3-csv and friends. It is generating a JSON structure from SPARQL query results, you can use that in any way you like in your code, with or without D3.

The access through a SPARQL endpoint allows a faster and more efficient data preparation (once you got the hang of SPARQL and the RDF data model). Ultimately it keeps visualizations up to date. Think of SPARQL endpoints as the most flexible API imaginable.

Define the SPARQL query and endpoint:

// Author of Q3011087 (D3.js)
var mikeQuery = `SELECT ?developerName WHERE {
  wd:Q3011087 wdt:P178 ?developer.
  ?developer rdfs:label ?developerName.
  FILTER(LANG(?developerName) = 'en')
}`

wikidataUrl = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql'

To query the endpoint and get the result:

d3.sparql(wikidataUrl, mikeQuery).then((data) => {
  console.log(data); // [{'developerName': 'Mike Bostock'}]
})

More examples are provided in the repository.

Features

  • Transformation of XSD Datatypes (e.g. xsd:dateTime, xsd:boolean, ...) to native JavaScript types.
  • Reformatting of the JSON Structure to a d3 style layout while using the provided variables names of the SPARQL Query.

Limitations

  • Only SELECT queries are supported. (This provides a projection of the graph data onto a table structure used by d3.)
  • Currently only supports endpoints which are able to respond with application/sparql-results+json.

Installing

Using NPM: npm install d3-sparql. You can also use a CDN, for instance https://www.jsdelivr.com.

See CHANGELOG for details about available versions.

API Reference

This package adds a sparql function to the global d3 object: d3.sparql(endpoint, query, options = {}).

# d3. sparql (endpoint, query[, options = {}]) <>

options is an optional object that will get merged with the second argument of fetch().

d3.sparql(endpoint, query)
  .then((data) => );

Acknowledgement

The initial development of this library by Zazuko was supported by the City of Zürich.

d3-sparql's People

Contributors

bergos avatar ktk avatar l00mi avatar vhf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

d3-sparql's Issues

POST method not supported properly

Hey, I can see that this was discussed already in the past, but I'm afraid POST is still not supported.
Even when passing an options document { method: 'POST' }, the query is still built with var url = endpoint + '?query=' + encodeURIComponent(query)such that parameters are still passed on the URL of the endpoint whereas they should be passed in the body of the HTTP request.
Am I missing something?
Thx.

a problem with dates coming from apache jena/fuseki

Hello
Thank's for the good job done in d3-sparql
I've a graph with literals like "2019-9-15T00:00:00Z"^^xsd:dateTime
When I query fuseki for a json format, I something like:
{
"head": {
"vars": [ "label" , "description" , "datedebut" , "datefin" , "musee" , "link" ]
} ,
"results": {
"bindings": [
{
"datedebut": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#dateTime" , "value": "2019-5-22T00:00:00Z" }
} ,
...
As you can see, the day is now 5 and not 05 and d3-sparql gives a result of null when parsing the date.
If i can disable the parsing and take the value as a string, I have a workaround
If I can force the parsing function, I have a workaround
But, now, I can't use d3-sparql with fuseki
Have you some idea or solution?
I will check if the output of fuseki is acceptable or erroneus in the context of json output of sparql endpoint.

Allowing also POST requests.

Currently only GET request can be issued which the majority of endpoints can handle. Some special use cases might still need POST requests.

Can't get the examples to work

Am using localhost.
Get the following error when running /examples/minimal.html :
Uncaught TypeError: d3Request.request is not a function
at Object.sparql (d3-sparql.js:26)
at minimal.html:21
This is the line giving the error:
var sparql = d3Request.request(url).mimeType('application/sparql-results+json').response(response);
d3Request seems to be a valid object.

Same happens with /examples/timeline.html
Can't figure out why - apologies if it's a newbie error :)

Introduce a d3 style SPARQL query constructor.

It might be possible to create a query constructor looking something like the following:

d3.sparql(endpoint)
  .prefix("vcard","http://www.w3.org/2006/vcard/ns#")
  .select("card")
  .select("name")
  .where("card", a, "vcard:Individual")
  .where("card", "vcard:name", "name")
  .filter("name", "=", "Michael")
  .limit(100)

to build the QUERY:

SELECT ?card ?name WHERE {
  ?card a vcard:Individual.
  ?card vcard:name ?name.
  FILTER( ?name = 'Michael')
}
LIMIT 100

Not sure about the potential benefits? Evt. automated query builders?

Add examples for use with d3

I think it’s a bit odd that the examples for a d3 support library stop at dumping the data to the console and don’t actually feed them into d3 :) an example that creates some chart would be very helpful IMHO.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.