Skip to content

Instantly share code, notes, and snippets.

@badosa
Last active March 16, 2021 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badosa/5e55d5a1996a263e26966436fe476128 to your computer and use it in GitHub Desktop.
Save badosa/5e55d5a1996a263e26966436fe476128 to your computer and use it in GitHub Desktop.
ISTAC Indicators

The Canary Islands Statistics Institute (ISTAC) has several APIs. Its Indicators API does not support JSON-stat but uses a format that it's very similar to JSON-stat, meaning: it can be translated to JSON-stat in a few lines of code (function can2jsonstat() in the example).

This example is a generalized version of Tourists in the Canary Islands.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="/d/ed9665503abec4f542d1/style.css">
<script src="https://cdn.jsdelivr.net/combine/npm/jsonstat@0.13.13,npm/jsonstat-utils@2.5.5"></script>
<style>
#indic {
background-color: #ccc;
padding: 8px;
}
</style>
</head>
<body>
<div id="indic">Retrieving indicators...</div>
<div id="tbrowser"></div>
<script>
var
tbr=document.getElementById("tbrowser"),
indic=document.getElementById("indic"),
tbrowser=function(event){
tbr.innerHTML="<p>Loading dataset...</p>";
fetch( event.target.value )
.then(function(resp) {
resp.json().then(function(json){
JSONstatUtils.tbrowser(
can2jsonstat(json),
tbr,
{
tblclass: "tbrowser"
}
);
});
})
;
},
can2jsonstat=function(data){
var
id=data.format,
dimension={},
size=id.map(function(e){
return data.dimension[e].representation.size;
}),
value=data.observation.map(function(e){
return e==="." ? null : parseFloat(e);
})
;
id.forEach(function(e){
dimension[e]={
"category": {
"index": data.dimension[e].representation.index
}
};
});
return {
"version": "2.0",
"class": "dataset",
"value": value,
"id": id,
"size": size,
"dimension": dimension
};
},
items=[],
getIndicators=function(url){
fetch( url )
.then(function(resp) {
resp.json().then(function(json){
var options="";
json.items.forEach(function(e){
items.push({
url: e.selfLink + "/data",
title: e.title.en
});
});
indic.innerHTML="Retrieved " + items.length + " indicators";
if(json.hasOwnProperty("lastLink") && json.hasOwnProperty("nextLink")){
getIndicators(json.nextLink);
}else{
items.forEach(function(e){
options+='<option value="' + e.url + '">' + e.title + '</option>';
});
indic.innerHTML="<select><option>Choose an indicator</option>"+options+"</select>";
document.querySelector("select").addEventListener(
"change",
tbrowser
);
}
});
})
;
}
;
getIndicators( "https://datos.canarias.es/api/estadisticas/indicators/v1.0/indicators" );
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment