Published
Edited
Jun 20, 2021
2 stars
Insert cell
Insert cell
Insert cell
class SPARQLQueryDispatcher {
constructor( endpoint ) {
this.endpoint = endpoint;
}

query( sparqlQuery ) {
const fullUrl = this.endpoint + '?query=' + encodeURIComponent( sparqlQuery );
const headers = { 'Accept': 'application/sparql-results+json' };

return fetch( fullUrl, { headers } ).then( body => body.json() );
}
}
Insert cell
Insert cell
Insert cell
exampleQuery = `#Population in Europe after 1960
SELECT ?objectLabel (YEAR(?date) as ?year)
?population (?objectLabel as ?Location)
WHERE
{
wd:Q458 wdt:P150 ?object . # European Union contains administrative territorial entity
?object p:P1082 ?populationStatement .
?populationStatement ps:P1082 ?population
; pq:P585 ?date .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
FILTER (YEAR(?date) >= 1960)
}
ORDER BY ?objectLabel ?year`;
Insert cell
Insert cell
queryDispatcher = new SPARQLQueryDispatcher("https://query.wikidata.org/sparql");
Insert cell
Insert cell
rawData = queryDispatcher.query( exampleQuery )
Insert cell
Insert cell
Insert cell
data = rawData['results']['bindings'].map(function(d) {
let output = {
country: d.Location.value,
population: +d.population.value,
year: +d.year.value,
};
return output;
})
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more