Published
Edited
Dec 2, 2020
Importers
8 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mostChildren = queryDispatcher.query(query)
Insert cell
Insert cell
mostChildrenData = mostChildren.map(res => ({
name: res.parentLabel,
value: +res.count
}))
Insert cell
import { chart as barchart } with {
mostChildrenData as data
} from "@d3/bar-chart"
Insert cell
barchart
Insert cell
Insert cell
Insert cell
function fetchRetry(url, options = {}) {
return fetch(url, options).then(res => {
// handle rate limiting
if (res.status === 429) {
const retryAfter = res.headers.get("retry-after");
return sleep(+retryAfter * 1000).then(fetchRetry);
}
return res;
});
}
Insert cell
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
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 fetchRetry(fullUrl, { headers })
.then(body => body.json())
.then(res =>
res.results.bindings.map(binding => {
const simpleBinding = {};
for (let prop in binding) {
simpleBinding[prop] = binding[prop].value;
}
return simpleBinding;
})
);
}
}
Insert cell
Insert cell
queryDispatcher = new SPARQLQueryDispatcher(wikidataEndpoint)
Insert cell
Insert cell
// convert an entity url such as http://www.wikidata.org/entity/ID to ID
entityId = entityUrl => new URL(entityUrl).pathname.split("/").pop()
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